且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++匹配文件中的字符串并获取行号

更新时间:2022-02-13 22:43:48

首先,您似乎正在尝试打开富文本格式文件(.rtf).这将不起作用,因为文件不仅包含文本,还包含其他数据( http://en .wikipedia.org/wiki/Rich_Text_Format ).

First off, looks like you're trying to open a rich text format file (.rtf). This won't work, as the file doesn't contains only text but also other data (http://en.wikipedia.org/wiki/Rich_Text_Format).

然后在您的代码中:while(getline(input,line1))每次迭代读取一行.很好,但是在循环中执行input >> rank >> boy_name >> girl_name;,它会继续在下一行中读取

Then in your code: while(getline(input,line1)) reads a line every iteration. That's fine, but inside the loop yo do input >> rank >> boy_name >> girl_name; which continues to read in next line

您想使用line1.您可以从第1行构造一个字符串流,然后从中读取名称:

You want to work with line1. You can construct a stringstream from line1, then read the names from it:

stringstream ss(line1):
ss >> rank >> boy_name >> girl_name;

那和Beta在他的回答中写的一样;您会在名称不匹配的每一行中放弃".

That and what Beta wrote in his answer; you "give up" in each line where names don't match.