惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Privacy & Cybersecurity Law Blog
Vercel News
Vercel News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
N
Netflix TechBlog - Medium
罗磊的独立博客
F
Fortinet All Blogs
T
Threatpost
Y
Y Combinator Blog
博客园_首页
美团技术团队
Security Latest
Security Latest
博客园 - 三生石上(FineUI控件)
T
Tailwind CSS Blog
V
V2EX - 技术
The Cloudflare Blog
L
LINUX DO - 热门话题
博客园 - 司徒正美
Jina AI
Jina AI
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
The Hacker News
The Hacker News
P
Privacy International News Feed
T
The Exploit Database - CXSecurity.com
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
Latest news
Latest news
NISL@THU
NISL@THU
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
雷峰网
雷峰网
Application and Cybersecurity Blog
Application and Cybersecurity Blog
B
Blog RSS Feed
W
WeLiveSecurity
D
DataBreaches.Net
G
Google Developers Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
G
GRAHAM CLULEY
Spread Privacy
Spread Privacy
Know Your Adversary
Know Your Adversary
TaoSecurity Blog
TaoSecurity Blog
S
Securelist
Help Net Security
Help Net Security

博客园 - Jaypei

archlinux中启用sshd [转] AT89C52资料 VirtualBox中安装PuppyLinux4 [转]比较全的Vim 中的正则表达式 luacurl的一个获得html的函数 [转]使用Mac OS X系统必须了解的10条命令 [原]Qt4.5中Plugins使用方法 用nuSOAP传递对象数组的问题终于解决 粗心导致的gsoap一个错误 MinGW环境使用gSOAP rdesktop中剪切板共享 [转]在Windows XP下用GCC 4.3.2编译Qt 4.4.3实战 Ubuntu下安装Postgresql 8.3 [原创]关于python的Singleton 简单的python读写windows剪切板 关于SOAPpy传递对象参数调用WebService的问题总结 LinkedServer的用法 解决Smarty中trancate使用UTF8时中文乱码问题 关于wxPython中多线程修改主界面 - Jaypei
[转]如何讀取文字檔? (C/C++) (STL)
Jaypei · 2008-12-31 · via 博客园 - Jaypei

2008-12-31 14:21  Jaypei  阅读(946)  评论()    收藏  举报

转一篇很有用的文章~

讀取文字檔有很多方式,在此歸納出最精簡的程式寫法。


若要一行一行的讀取文字檔,可使用以下寫法。

/**//* 
(C) OOMusou 2006 
http://oomusou.cnblogs.com

Filename    : ReadTextFilePerLine.cpp
Compiler    : Visual C++ 8.0 / ISO C++
Description : Demo how to read text file per line
Release     : 10/15/2006

*/

#include 

<iostream>
#include 
<fstream>
#include 
<string>using namespace std;int main() {
  ifstream inFile(
"books.txt");
  
string line;while(getline(inFile,line)) {
    cout 
<< line << endl;
  }

  inFile.close();

return 0;
}

 執行結果

this is a book
a book a book
book
請按任意鍵繼續 . . .

若在一行一行讀取文字檔的同時,還想同時讀出每一個字串,可用以下寫法。

/**//* 
(C) OOMusou 2006 
http://oomusou.cnblogs.com

Filename    : ReadTextFilePerLineWord.cpp
Compiler    : Visual C++ 8.0 / ISO C++
Description : Demo how to read text file per line
Release     : 10/15/2006

*/
#include 
<iostream>
#include 
<fstream>
#include 
<string>
#include 
<sstream>using namespace std;int main() {
  ifstream inFile(
"books.txt");
  
string line;while(getline(inFile,line)) {
    cout 
<< line << endl;
    istringstream ss(line);
    
string word;
    
while(ss >> word) {
      cout 
<< word << endl;
    }
    cout 
<< endl;
  }

  inFile.close();

return 0;
}

 執行結果

this is a book

this

is

a
book

a book a book
a
book
a
book

book
book

請按任意鍵繼續 . . .

 若只要讀取文字檔中的每個字,使用while()的方式,可直接處理字串。

/**//* 
(C) OOMusou 2006 
http://oomusou.cnblogs.com

Filename    : ReadTextFilePerWord.cpp
Compiler    : Visual C++ 8.0 / ISO C++
Description : Demo how to read text file per word
Release     : 12/07/2006

*/

#include 

<iostream>
#include 
<fstream>
#include 
<string>using namespace std;int main() {
  ifstream inFile(
"books.txt");
  
string str;
  
  
while(infile >> str) 
    cout 
<< str << endl;
  
  inFile.close();
return 0;
}

 另外一種方式,使用copy() algorithm將文字都讀到vector中,再做後續的加工處理,優點是程式超短,缺點是要多浪費一個vector。

/**//* 
(C) OOMusou 2006 
http://oomusou.cnblogs.com

Filename    : ReadTextByCopy.cpp
Compiler    : Visual C++ 8.0 / ISO C++
Description : Demo how to read file per string by copy() algorithm
Release     : 12/17/2006 1.0

*/
#include 
<iostream>
#include 
<fstream>
#include 
<vector>
#include 
<string>
#include 
<algorithm>using namespace std;int main() {
  ifstream inFile(
"books.txt");
  vector
<string> svec;
  copy(istream_iterator
<string>(inFile), istream_iterator<string>(), back_inserter(svec));
  copy(svec.begin(), svec.end(), ostream_iterator
<string>(cout,"\n"));

  inFile.close();

return 0;
}

 執行結果

this
is
a
book
a
book
a
book
book
請按任意鍵繼續 . . .

原文地址:http://www.cnblogs.com/oomusou/archive/2006/12/17/529310.html