Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 读字符串直到行尾_C++_File_Ifstream - Fatal编程技术网

C++ 读字符串直到行尾

C++ 读字符串直到行尾,c++,file,ifstream,C++,File,Ifstream,为了理解如何正确阅读,如果我想阅读每行中不同的字符串,我如何从文件中读取下一个文本。每行可以有不同的大小(第一行可以有3个字符串,第二行可以有100个字符串) 我在我的代码中尝试了类似的东西,但我不知道如何检查程序是否在最后一行 ifstream fich("thefile.txt"); fich >> aux; //Contain number of line for(int i=0;i<aux;i++){ //For each line string line;

为了理解如何正确阅读,如果我想阅读每行中不同的字符串,我如何从文件中读取下一个文本。每行可以有不同的大小(第一行可以有3个字符串,第二行可以有100个字符串)

我在我的代码中尝试了类似的东西,但我不知道如何检查程序是否在最后一行

ifstream fich("thefile.txt");

fich >> aux; //Contain number of line

for(int i=0;i<aux;i++){  //For each line
   string line;
   getline(fich, line);

   char nt;    //First in line it's always a char
   fich >> nt;

   string aux;

   while(line != "\n"){   //This is wrong, what expression should i use to check?
      fich >> aux;
     //In each read i'll save the string in set
   }
}
ifstream-fich(“thefile.txt”);
fich>>辅助//包含行数
对于(int i=0;i
是,因为
'\n'
已被
getline()
函数删除

使用它可以很容易地解析任意数量的单词,直到当前
行的末尾

string aux;
std::istringstream iss(line);
while(iss >> aux) {
    // ...
}

另请注意:

fich >> aux; //Contain number of line
将使用
std::getline()
读取空行,因为在这种情况下,
'\n'
将是该操作的剩余部分(有关详细信息,请参阅)。

用于分析该行。有用的阅读:请参阅选项2。
string aux;
std::istringstream iss(line);
while(iss >> aux) {
    // ...
}
fich >> aux; //Contain number of line