【发布时间】:2020-09-05 11:54:31
【问题描述】:
我只想读取文件第一行的字符然后停止。 以下代码在 Codeblocks 中不起作用,因为输出文件为空。 我做错了什么?
#include <fstream>
using namespace std;
ifstream cin ("test.in");
ofstream cout ("test.out");
char s;
int main()
{
while (cin>>s)
if (s=='\n')
{
cout<<"end of line"; return 0;
}
return 0;
}
【问题讨论】:
-
你不能像这样抓住行尾。看看
getline -
试试
std::string line; std::getline(std::cin, line);这是一个完整的例子:https://en.cppreference.com/w/cpp/string/basic_string/getline -
谢谢。 Getline 不工作,因为没有。该行上的字符数太高,我的代码运行时间已用完。
-
你的线路尺寸是多少?
-
1000000 个字符
标签: c++ end-of-line