c中cin对应java中,输入 - 如何在C ++中从cin读取EOF

输入 - 如何在C ++中从cin读取EOF

我正在编写一个程序,直接从用户输入读取数据,并想知道我怎么能(没有循环)读取所有数据,直到标准输入EOF。 我正在考虑使用cin.get( input, '\0' ),但'\0'并不是真正的EOF角色,只读到EOF或'\0',以先到者为准。

或者使用循环是唯一的方法吗? 如果是这样,最好的方法是什么?

10个解决方案

59 votes

从stdin读取可变数据量的唯一方法是使用循环。 我总是发现std::getline()功能非常好用:

std::string line;

while (std::getline(std::cin, line))

{

std::cout << line << std::endl;

}

默认情况下,getline()会读取直到换行符。 您可以指定另一个终止字符,但EOF本身不是一个字符,因此您不能简单地调用getline()。

trotterdylan answered 2019-06-23T08:12:41Z

44 votes

您可以使用流迭代器在没有显式循环的情况下执行此操作。 我确信它在内部使用某种循环。

#include

#include

#include

#include

#include

int main()

{

// don't skip the whitespace while reading

std::cin >> std::noskipws;

// use stream iterators to copy the stream to a string

std::istream_iterator it(std::cin);

std::istream_iterator end;

std::string results(it, end);

std::cout << results;

}

KeithB answered 2019-06-23T08:13:06Z

38 votes

使用循环:

#include

using namespace std;

...

// numbers

int n;

while (cin >> n)

{

...

}

// lines

string line;

while (getline(cin, line))

{

...

}

// characters

char c;

while (cin.get(c))

{

...

}

资源

Degvik answered 2019-06-23T08:13:38Z

20 votes

在使用std:istreambuf_iterator研究KeithB的解决方案后,我发现了std:istreambuf_iterator。

测试程序将所有管道输入读入字符串,然后再将其写出:

#include

#include

#include

int main()

{

std::istreambuf_iterator begin(std::cin), end;

std::string s(begin, end);

std::cout << s;

}

Richard Smith answered 2019-06-23T08:14:12Z

5 votes

可能最简单,效率最高:

#include

int main()

{

std::cout << std::cin.rdbuf();

}

如果需要,请在此处使用其他类型的流(如std::ostringstream)作为缓冲区而不是标准输出流。

FrankHB answered 2019-06-23T08:14:47Z

2 votes

您可以使用std :: istream :: getline()(或者最好是在std :: string上运行的版本)函数来获取整行。 两者都有允许您指定分隔符(行尾字符)的版本。 字符串版本的默认值为'\ n'。

luke answered 2019-06-23T08:15:14Z

2 votes

遗憾的是:我决定使用C ++ IO与基于boost的代码保持一致。 从这个问题的答案中我选择了while (fgets(buf, 100, stdin))。在cygwin(mintty)中使用g ++版本4.5.3(-O3)我获得了2 MB / s的吞吐量。 Microsoft Visual C ++ 2010(/ O2)使相同的代码为40 MB / s。

在将IO重写为纯C while (fgets(buf, 100, stdin))之后,两个经过测试的编译器的吞吐量都跃升至90 MB / s。 这对任何大于10 MB的输入都有所不同......

liborm answered 2019-06-23T08:15:50Z

0 votes

while(std::cin) {

// do something

}

Bryan answered 2019-06-23T08:16:09Z

0 votes

等等,我理解你了吗? 您正在使用cin进行键盘输入,并且您想在用户输入EOF字符时停止读取输入? 为什么用户会输入EOF字符? 或者你的意思是你想停止阅读EOF中的文件?

如果您实际上尝试使用cin来读取EOF字符,那么为什么不将EOF指定为分隔符?

// Needed headers: iostream

char buffer[256];

cin.get( buffer, '\x1A' );

如果你想停止从EOF中的文件读取,那么只需使用getline并再次指定EOF作为分隔符。

// Needed headers: iostream, string, and fstream

string buffer;

ifstream fin;

fin.open("test.txt");

if(fin.is_open()) {

getline(fin,buffer,'\x1A');

fin.close();

}

uberwulu answered 2019-06-23T08:16:51Z

0 votes

一种选择是使用容器,例如容器。

std::vector data;

并将所有输入重定向到此集合,直到收到std::bad_alloc,即

std::copy(std::istream_iterator(std::cin),

std::istream_iterator(),

std::back_inserter(data));

但是,使用过的容器可能需要过于频繁地重新分配内存,否则当系统内存不足时,您将以std::bad_alloc异常结束。 为了解决这些问题,您可以保留固定金额的元素N并单独处理这些元素,即

data.reserve(N);

while (/*some condition is met*/)

{

std::copy_n(std::istream_iterator(std::cin),

N,

std::back_inserter(data));

/* process data */

data.clear();

}

0xbadf00d answered 2019-06-23T08:17:34Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值