(原創) 如何讀取文字檔? (C/C++) (STL)

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

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

 1/* 
 2(C) OOMusou 2006 http://oomusou.cnblogs.com
 3
 4Filename    : ReadTextFilePerLine.cpp
 5Compiler    : Visual C++ 8.0 / ISO C++
 6Description : Demo how to read text file per line
 7Release     : 10/15/2006
 8*/

 9
10#include <iostream>
11#include <fstream>
12#include <string>
13
14using namespace std;
15
16int main() {
17  ifstream inFile("books.txt");
18  string line;
19
20  while(getline(inFile,line)) {
21    cout << line << endl;
22  }

23
24  inFile.close();
25
26  return 0;
27}


執行結果

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


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

 1/* 
 2(C) OOMusou 2006 http://oomusou.cnblogs.com
 3
 4Filename    : ReadTextFilePerLineWord.cpp
 5Compiler    : Visual C++ 8.0 / ISO C++
 6Description : Demo how to read text file per line
 7Release     : 10/15/2006
 8*/

 9#include <iostream>
10#include <fstream>
11#include <string>
12#include <sstream>
13
14using namespace std;
15
16int main() {
17  ifstream inFile("books.txt");
18  string line;
19
20  while(getline(inFile,line)) {
21    cout << line << endl;
22    istringstream ss(line);
23    string word;
24    while(ss >> word) {
25      cout << word << endl;
26    }

27    cout << endl;
28  }

29
30  inFile.close();
31
32  return 0;
33}


執行結果

this is a book
this
is
a
book

a book a book
a
book
a
book

book
book

請按任意鍵繼續 . . .


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

 1/* 
 2(C) OOMusou 2006 http://oomusou.cnblogs.com
 3
 4Filename    : ReadTextFilePerWord.cpp
 5Compiler    : Visual C++ 8.0 / ISO C++
 6Description : Demo how to read text file per word
 7Release     : 12/07/2006
 8*/

 9
10#include <iostream>
11#include <fstream>
12#include <string>
13
14using namespace std;
15
16int main() {
17  ifstream inFile("books.txt");
18  string str;
19  
20  while(infile >> str) 
21    cout << str << endl;
22  
23  inFile.close();
24
25  return 0;
26}


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

 1/* 
 2(C) OOMusou 2006 http://oomusou.cnblogs.com
 3
 4Filename    : ReadTextByCopy.cpp
 5Compiler    : Visual C++ 8.0 / ISO C++
 6Description : Demo how to read file per string by copy() algorithm
 7Release     : 12/17/2006 1.0
 8*/

 9#include <iostream>
10#include <fstream>
11#include <vector>
12#include <string>
13#include <algorithm>
14
15using namespace std;
16
17int main() {
18  ifstream inFile("books.txt");
19  vector<string> svec;
20  copy(istream_iterator<string>(inFile), istream_iterator<string>(), back_inserter(svec));
21  copy(svec.begin(), svec.end(), ostream_iterator<string>(cout,"\n"));
22
23  inFile.close();
24
25  return 0;
26}


執行結果

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

(02/20/2007 更新) 有網友問我怎麼將文字檔讀到二維陣列處理,以下是處理的方式
文字檔
00001 Peter Hsiao 555.55
00002 John  Lin   222.12

 1/* 
 2(C) OOMusou 2007 http://oomusou.cnblogs.com
 3
 4Filename    : ReadTextFilePerLineWordToArray.cpp
 5Compiler    : Visual C++ 8.0 / gcc 3.4.2 / ISO C++
 6Description : Demo how to read text file to 2 dim array
 7Release     : 02/20/2007 1.0
 8*/

 9#include <iostream>
10#include <fstream>
11#include <string>
12#include <sstream>
13
14using namespace std;
15
16int main() {
17  ifstream inFile("source.txt");
18  const int xsize = 5;
19  const int ysize = 2;
20  string (*arr)[xsize] = new string[ysize][xsize];
21  
22  string line;
23  int y = 0;
24  while(getline(inFile,line)) {
25    istringstream ss(line);
26    string word;
27    int x = 0;
28    while(ss >> word) {
29      arr[y][x] = word;
30      ++x;
31    }

32    ++y;
33  }

34
35  inFile.close();
36  
37  for(int y = 0; y != ysize; ++y) {
38    for(int x = 0; x != xsize; ++x) {
39      cout << arr[y][x] << " ";
40    }

41    cout << endl;
42  }

43  
44  delete []arr;
45
46  return 0;
47}

執行結果
00001 Peter Hsiao 555.55
00002 John Lin 222.12

posted on 2006-12-17 20:09  真 OO无双  阅读(33928)  评论(1编辑  收藏  举报

导航