从stdin读取未格式化的文本 [英] Reading unformatted text from stdin
问题描述
问候,
我需要从stdin到EOF读取(无格式文本)到char
缓冲区;当然我不能分配我的缓冲区,直到我知道有多少
文本可用,并且我不知道有多少文本可用,直到我
已经阅读它...这似乎意味着多次读取输入
流将是不可避免的。
现在我可以正确找到可用的字符数:
|
| #include< iostream>
|
| std :: cin.ignore(std :: numeric_limits< int> :: max());
| const int num_chars = std :: cin.gcount();
|
然后我想这样做:
|
| char * const text = new char [num_chars + 1];
| std :: cin.read(text,num_chars);
| text [num_chars] =''\''';
|
但是read()不起作用,因为据我所知,忽略()有(顾名思义
)`扔掉''流中的所有字符......'
我相信这一定是股票情况,并想知道是否有一个
(高效,优雅)的接近它的方式。
任何提示赞赏,
-
Lionel B
Greetings,
I need to read (unformatted text) from stdin up to EOF into a char
buffer; of course I cannot allocate my buffer until I know how much
text is available, and I do not know how much text is available until I
have read it... which seems to imply that multiple reads of the input
stream will be inevitable.
Now I can correctly find the number of characters available by:
|
| #include <iostream>
|
| std::cin.ignore(std::numeric_limits<int>::max());
| const int num_chars = std::cin.gcount();
|
Then I would like to do:
|
| char* const text = new char[num_chars+1];
| std::cin.read(text,num_chars);
| text[num_chars]=''\0'';
|
but the read() won''t work because, as I understand it, ignore() has (as
its name implies) `thrown away'' all characters in the stream...!
I am sure this must be a stock situation, and wonder if there is an
(efficient, elegant) stock way of approaching it.
Any tips appreciated,
--
Lionel B
推荐答案
" Lionel B" <去**** @ lionelb.com>在消息中写道
news:11 ********************** @ z14g2000cwz.googlegr oups.com ...
"Lionel B" <go****@lionelb.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
问候,
我需要从stdin到EOF读取(未格式化的文本)到char
缓冲区;当然我不能分配我的缓冲区,直到我知道有多少文本可用,并且我不知道有多少文本可用,直到我阅读它...这似乎意味着多次读取输入
流将是不可避免的。
现在我可以正确找到可用的字符数:
|
| #include< iostream>
|
| std :: cin.ignore(std :: numeric_limits< int> :: max());
| const int num_chars = std :: cin.gcount();
|
然后我想做:
|
| char * const text = new char [num_chars + 1];
| std :: cin.read(text,num_chars);
| text [num_chars] =''\''';
|
但是read()不起作用,因为据我所知,ignore()有(如
名字暗示)扔掉流中的所有人物......!
我相信这一定是股票情况,并想知道是否有一个
(高效,优雅)接近它的方式。
任何提示赞赏,
Greetings,
I need to read (unformatted text) from stdin up to EOF into a char
buffer; of course I cannot allocate my buffer until I know how much
text is available, and I do not know how much text is available until I
have read it... which seems to imply that multiple reads of the input
stream will be inevitable.
Now I can correctly find the number of characters available by:
|
| #include <iostream>
|
| std::cin.ignore(std::numeric_limits<int>::max());
| const int num_chars = std::cin.gcount();
|
Then I would like to do:
|
| char* const text = new char[num_chars+1];
| std::cin.read(text,num_chars);
| text[num_chars]=''\0'';
|
but the read() won''t work because, as I understand it, ignore() has (as
its name implies) `thrown away'' all characters in the stream...!
I am sure this must be a stock situation, and wonder if there is an
(efficient, elegant) stock way of approaching it.
Any tips appreciated,
#include< algorithm>
#include< iostream>
#include< string>
int main()
{
std :: cout<< 输入文字:";
std :: string line;
std :: getline(std :: cin,line);
>
if(!line.empty())
{
char * const text = new char [line.size()+ 1];
std :: copy(line.begin(),line.end(),text);
text [line.size()] = 0;
std :: cout<<文字<< ''\ n'';
删除[]文字;
}
返回0;
}
-Mike
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
std::cout << "Enter text: ";
std::string line;
std::getline(std::cin, line);
if(!line.empty())
{
char * const text = new char[line.size() + 1];
std::copy(line.begin(), line.end(), text);
text[line.size()] = 0;
std::cout << text << ''\n'';
delete[] text;
}
return 0;
}
-Mike
Lionel B写道:
Lionel B wrote:
问候,
我需要从stdin到EOF读取(未格式化的文本)到char
缓冲区;当然我不能分配我的缓冲区,直到我知道有多少文本可用,并且我不知道有多少文本可用,直到我阅读它...这似乎意味着多次读取输入
流将是不可避免的。
现在我可以正确找到可用的字符数:
|
| #include< iostream>
|
| std :: cin.ignore(std :: numeric_limits< int> :: max());
| const int num_chars = std :: cin.gcount();
|
然后我想做:
|
| char * const text = new char [num_chars + 1];
| std :: cin.read(text,num_chars);
| text [num_chars] =''\''';
|
但是read()不起作用,因为据我所知,ignore()有(如
名字暗示)'扔掉''流中的所有字符......!
我不明白。
为什么你在溪边做忽略()?
应该是什么它的目的是什么?
我相信这一定是一种库存情况,并想知道是否有一种高效,优雅的库存方式来接近它。
任何提示赞赏,
Greetings,
I need to read (unformatted text) from stdin up to EOF into a char
buffer; of course I cannot allocate my buffer until I know how much
text is available, and I do not know how much text is available until I
have read it... which seems to imply that multiple reads of the input
stream will be inevitable.
Now I can correctly find the number of characters available by:
|
| #include <iostream>
|
| std::cin.ignore(std::numeric_limits<int>::max());
| const int num_chars = std::cin.gcount();
|
Then I would like to do:
|
| char* const text = new char[num_chars+1];
| std::cin.read(text,num_chars);
| text[num_chars]=''\0'';
|
but the read() won''t work because, as I understand it, ignore() has (as
its name implies) `thrown away'' all characters in the stream...!
I don''t understand.
Why did you do ignore() at the stream?
What should be the purpose of it?
I am sure this must be a stock situation, and wonder if there is an
(efficient, elegant) stock way of approaching it.
Any tips appreciated,
好的提示是:如果你想阅读那么首先是不明智的。
扔掉你想读的所有东西: - )
另外:
你知道std :: string可以保存很长的文本吗?
你知道吗字符串有一个函数getline?
你知道你可以告诉getline()它应该用什么作为''lines''的
分隔符?
-
Karl Heinz Buchegger
kb *** ***@gascad.at
Lionel B写道:
Lionel B wrote:
我需要从stdin读取(未格式化的文本)到EOF成了一个cha r
缓冲区;
I need to read (unformatted text) from stdin up to EOF into a char
buffer;
这有什么问题?
| std :: ifstream in(some file",std :: ios_base :: binary);
| std :: ostringstream tmp;
| tmp<< in.rdbuf();
| std :: string const& contents = tmp.str();
....或:
| std :: ifstream in(some file,
std :: ios_base :: binary);
|的std :: istreambuf_iterator<炭>求(中),结束;
| std :: string内容(求,结束);
实际上,我更喜欢后者,但它可能比大多数人的第一个替代品要慢很多实现。
-
< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>
< http://www.contendix.com> - 软件开发&咨询
What''s wrong with this?
| std::ifstream in("some file", std::ios_base::binary);
| std::ostringstream tmp;
| tmp << in.rdbuf();
| std::string const& contents = tmp.str();
.... or:
| std::ifstream in("some file",
std::ios_base::binary);
| std::istreambuf_iterator<char> beg(in), end;
| std::string contents(beg, end);
Actually, I like the latter better but it is probably considerably
slower than the first alternative on most implementations.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting
这篇关于从stdin读取未格式化的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!