C ++ fstream I / O. [英] C++ fstream I/O

查看:61
本文介绍了C ++ fstream I / O.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将当前行和文件的下一行存储到字符串中以进行它们之间的比较。但是,我不能也不知道如何将文件中的下一行放入字符串next_line。有什么建议?谢谢。

I am trying to store the current line and the next line of a file into strings to do comparisons between them. However, I can''t and don''t know how to get the next line from the file into the string next_line. Any suggestions? Thanks.

展开 | 选择 < span class =codeDivider> | Wrap | 行号

推荐答案

你可以重复一遍代码
can you just repeat the code
展开 | 选择 | Wrap | 行号


不行,因为它打印出相同的行。
No that doesn''t work because it prints out the same line.


只需使用带有字符串对象的getline:


int main()

{

string line,cur_line,next_line ;

ifstream INFILE(" input.txt");


getline(INFILE,cur_line);

getline(INFILE ,next_line);


cout<< cur_line<< ''\ n''<< next_line<< endl;


返回0;

}
Just use the getline with string objects:

int main()
{
string line, cur_line, next_line ;
ifstream INFILE("input.txt");

getline(INFILE, cur_line);
getline(INFILE, next_line);

cout << cur_line << ''\n'' << next_line << endl;

return 0;
}


这篇关于C ++ fstream I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆