1. 程式人生 > >C++ 一行一行的讀檔案

C++ 一行一行的讀檔案

#include<iostream>
#include<fstream>
#include<string>
#include <vector>


using namespace std;
int main(int argv, char *arg[])
{


fstream f("file.txt");
vector<string> words;
string line; 
while (getline(f, line))
{
words.push_back(line);
}
//dictionary.txt在csdn裡面可以下載,裡面有4萬多個單詞,相當於一個字典
cout << "共有單詞數目:" << words.size() << endl;


for (int i = 0; i < words.size(); i++)
{
cout << words[i] << endl;
}
return  0;


}