c++从文件中读取特定字符串问题的总结

1、每次从文件中读出一行作为一个字符串

可以用ifstream()函数来打开一个文件,然后用while加getline()函数即可每次读一行文件,直到文件结束

#include<unistd.h>
#include<fstream>
#include<sstream>
#include<string>
#include<iostream>
#include<stdio.h>
#include<cstdlib>
using namespace std;
********************以上是要包含的头文件
ifstream("要打开的文件及路径");
string input;
while(getline(file,input))
{
       要对字符串执行的操作;
       :
       :
}

2、对文件中有空格的各个字符串进行分解提取

如文本文件为:

kasjaslkfj    fsdasfas    fasf   fasf 
sa  f  fafsfsa   fsafas f    fsadfas

则可以使用file>>input 这样的方式进行读取分解,知道文件结束为止

#include<unistd.h>
#include<fstream>
#include<sstream>
#include<string>
#include<string.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
***************************要包含的头文件
ifstream  file("要读取的文件及其路径");
strint input;
while(file>>input)
{
      要对文件进行的操作
      :
      :   
}
原文地址:https://www.cnblogs.com/zlgxzswjy/p/5384081.html