[转]如何讀取文字檔? (C/C++) (STL)
2008-12-31 14:21 Jaypei 阅读(931) 评论(0) 编辑 收藏 举报转一篇很有用的文章~
讀取文字檔有很多方式,在此歸納出最精簡的程式寫法。
若要一行一行的讀取文字檔,可使用以下寫法。
/**//*
(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;
}
(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
請按任意鍵繼續 . . .
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;
}
(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
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;
}
(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;
}
(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
請按任意鍵繼續 . . .
is
a
book
a
book
a
book
book
請按任意鍵繼續 . . .
原文地址:http://www.cnblogs.com/oomusou/archive/2006/12/17/529310.html