C++ 读字符串直到行尾
为了理解如何正确阅读,如果我想阅读每行中不同的字符串,我如何从文件中读取下一个文本。每行可以有不同的大小(第一行可以有3个字符串,第二行可以有100个字符串) 我在我的代码中尝试了类似的东西,但我不知道如何检查程序是否在最后一行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;
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