I / O getline和小缓冲区 [英] I/O getline and small buffer

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

问题描述



下面的程序中small_buffer有什么问题?

I / O getline没有从文件读取数据到小(相对于文件行大小)缓冲区。


====== foo.cpp ======

#include< cassert>

#include< iostream>

#include< fstream>

使用命名空间std;


void read_using_io_getline()

{

char small_buffer [4];

char big_buffer [512];


ifstream infile( " foo.in");

断言(infile.is_open());


cout<< infile.rdbuf()<< endl;


// --------------------------

infile.clear();

infile.seekg(0,ios :: beg);


cout<< endl;

cout<< [small_buffer] Start:sizeof(small_buffer)=" << sizeof(small_buffer)<< endl;

cout<< [small_buffer]开始:rdstate =" << infile.rdstate()<< endl;

while(infile.getline(small_buffer,sizeof(small_buffer)))

{

cout<< [small_buffer]阅读:" << small_buffer<< ''\ n'';

}

cout<< [small_buffer]结束:rdstate =" << infile.rdstate()<< endl;

// --------------------------

infile.clear( );

infile.seekg(0,ios :: beg);


cout<< endl;

cout<< [big_buffer]开始:sizeof(big_buffer)=" << sizeof(big_buffer)<< endl;

cout<< [big_buffer]开始:rdstate =" << infile.rdstate()<< endl;

while(infile.getline(big_buffer,sizeof(big_buffer)))

{

cout<< [big_buffer]阅读:" << big_buffer<< ''\ n'';

}

cout<< [big_buffer]结束:rdstate =" << infile.rdstate()<<结束;

}

int main()

{

read_using_io_getline();

返回0;


}

=====================


======编译&运行======


// gpp.exe(GCC)3.4.1


$ gpp foo.cpp
//没有错误/警告


$ ./a


1234567890

ABCDEGHIJKL < br $>
XYZUWT

[small_buffer]开始:sizeof(small_buffer)= 4

[small_buffer]开始:rdstate = 0

[small_buffer]结束:rdstate = 4


[big_buffer]开始:sizeof(big_buffer)= 512

[big_buffer]开始:rdstate = 0

[big_buffer]阅读:1234567890

[big_buffer]阅读:ABCDEGHIJKL

[big_buffer]阅读:XYZUWT

[big_buffer] ]完成:rdstate = 6


================================


-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


What is wrong with small_buffer in program below?
I/O getline doesn''t read data from file into small (relative to file line size) buffer.

====== foo.cpp ======
#include <cassert>
#include <iostream>
#include <fstream>
using namespace std;

void read_using_io_getline ()
{
char small_buffer[4];
char big_buffer[512];

ifstream infile ("foo.in");
assert (infile.is_open());

cout << infile.rdbuf() << endl;

// --------------------------
infile.clear ();
infile.seekg (0, ios::beg);

cout << endl;
cout << "[small_buffer] Start: sizeof(small_buffer) = " << sizeof(small_buffer) << endl;
cout << "[small_buffer] Start: rdstate = " << infile.rdstate() << endl;
while (infile.getline (small_buffer, sizeof(small_buffer)))
{
cout << "[small_buffer] Read: " << small_buffer << ''\n'';
}
cout << "[small_buffer] Finish: rdstate = " << infile.rdstate() << endl;
// --------------------------
infile.clear ();
infile.seekg (0, ios::beg);

cout << endl;
cout << "[big_buffer] Start: sizeof(big_buffer) = " << sizeof(big_buffer) << endl;
cout << "[big_buffer] Start: rdstate = " << infile.rdstate() << endl;
while (infile.getline (big_buffer, sizeof(big_buffer)))
{
cout << "[big_buffer] Read: " << big_buffer << ''\n'';
}
cout << "[big_buffer] Finish: rdstate = " << infile.rdstate() << endl;
}
int main()
{
read_using_io_getline ();
return 0;

}
=====================

====== Compilation & Run ======

// gpp.exe (GCC) 3.4.1

$ gpp foo.cpp
// No errors/warnings

$ ./a

1234567890
ABCDEGHIJKL
XYZUWT
[small_buffer] Start: sizeof(small_buffer) = 4
[small_buffer] Start: rdstate = 0
[small_buffer] Finish: rdstate = 4

[big_buffer] Start: sizeof(big_buffer) = 512
[big_buffer] Start: rdstate = 0
[big_buffer] Read: 1234567890
[big_buffer] Read: ABCDEGHIJKL
[big_buffer] Read: XYZUWT
[big_buffer] Finish: rdstate = 6

================================

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

推荐答案

gpp foo.cpp
//没有错误/警告

gpp foo.cpp
// No errors/warnings


./ a


1234567890

ABCDEGHIJKL

XYZUWT

[small_buffer]开始:sizeof(small_buffer)= 4

[small_buffer]开始:rdstate = 0

[small_buffer]完成:rdstate = 4

[big_buffer]开始:sizeof(big_buffer)= 512

[big_buffer]开始:rdstate = 0

[big_buffer]阅读:1234567890

[big_buffer]阅读:ABCDEGHIJKL

[big_buffer]阅读:XYZUWT

[big_buffer]完成:rdstate = 6


========================== ======


-

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

./a

1234567890
ABCDEGHIJKL
XYZUWT
[small_buffer] Start: sizeof(small_buffer) = 4
[small_buffer] Start: rdstate = 0
[small_buffer] Finish: rdstate = 4

[big_buffer] Start: sizeof(big_buffer) = 512
[big_buffer] Start: rdstate = 0
[big_buffer] Read: 1234567890
[big_buffer] Read: ABCDEGHIJKL
[big_buffer] Read: XYZUWT
[big_buffer] Finish: rdstate = 6

================================

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Alex Vinokur写道:
Alex Vinokur wrote:
下面的程序中small_buffer有什么问题?
I / O getline没有从文件中读取数据到小(相对于文件行大小) )缓冲区。
What is wrong with small_buffer in program below?
I/O getline doesn''t read data from file into small (relative to file line size) buffer.




确实如此。如果您在''getline''上仔细阅读RTFM,您可能会看到它

如果缓冲区太小而无法读取,则会在流中设置错误条件

整条线。这是唯一的迹象表明你知道

仍有一些东西在同一行中被阅读。我想你需要

来改进你的代码以处理这个错误情况。基本上,如果在''getline'之后

流不好,那么一般来说*并不一定意味着错误* b $ b读*。它可能只是一个标志,告诉你嘿,你

要求一条线,你的缓冲区不够大,整条线,只是为了
让你知道。


V



It does. If you RTFM carefully on ''getline'', you''ll probably see that it
sets the error condition in the stream if the buffer is too small to read
the entire line. That''s the only indication it has for you to know that
there is still some stuff in the same line to be read. I guess you need
to improve your code to work on that error condition. Essentially, if the
stream is not good after ''getline'' it doesn''t necessarily mean error in
reading *in general*. It could be just a flag that tells you "hey, you
asked for a line, your buffer isn''t big enough for the whole line, just to
let you know".

V


这篇关于I / O getline和小缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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