C++实现一行一行读取文本的方法

时间:2021-11-08 08:01:01

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<fstream>
#include<string>
int main(int argv,char *arg[])
{
 
 fstream  f("dictionary.txt");//创建一个fstream文件流对象
 vector<string> words; //创建一个vector<string>对象
 string  line; //保存读入的每一行
 while(getline(f,line))//会自动把\n换行符去掉
 {
  words.push_back(line);
 }
 //dictionary.txt在csdn里面可以下载,里面有4万多个单词,相当于一个字典
 cout << "共有单词数目:" << words.size() << endl;
 return 0;
 
}

以上这篇C++实现一行一行读取文本的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/CosmopolitanMe/article/details/70879894