473,396 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

.Net 2003 ifstream getline(buff, sze) jumps to EOF if line>size

In .Net 2003 if a line, read from a text file is larger
than a size parameter, the ifstream getline(buff, sze) put
the file pointer to the EOF, so next peek() returns EOF.

I saw this problem also when size was 2000 but line was
1200 bytes long.
There is no such problem with .Net 2002
For .Net 2003 I used :
#include <string>
#include <fstream>
using namespace std;

The same function in .Net 2002 works fine.
Thanks,
Dave

Nov 17 '05 #1
6 5262
>In .Net 2003 if a line, read from a text file is larger
than a size parameter, the ifstream getline(buff, sze) put
the file pointer to the EOF, so next peek() returns EOF.

I saw this problem also when size was 2000 but line was
1200 bytes long.
There is no such problem with .Net 2002
The same function in .Net 2002 works fine.


Dave,

Do you have a simple example (and a simple file that illustrates the
difference)?

I think I can reproduce what you're seeing, but I get consistent
results with VS2003 & VC6 (I don't have VS2002 installed to check what
it does).

Here's the code I've tried if you want to check how this behaves for
you with VS2002 & VS2003:

#include <string>
#include <fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
ifstream is;

is.open( argv[1], ios_base::in );

char buffer[10];

is.getline( buffer, 5 );

int ret = is.peek();
if ( char_traits<char>::eof() == ret )
{
cout << "EOF returned";
}
else
{
cout << "Not EOF";
}

is.close();

return 0;
}
and the sample file is a text file containing:

12345678901234567890
987654321

After calling getline, the buffer contains "1234" (and a null
character), and the following call to peek always (it seems incorrect
to me) returns eof.

Stepping through the code, it appears that peek fails because the
getline method sets the state of the stream to a failure case because
the buffer end is reached. Skipping past this check does return the
next character in the file, so the file pointer would appear to be
correct.

I don't usually use with these stream methods, so I'm not sure what
the expected behaviour should be.

Dave Lowndes
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
On Mon, 22 Dec 2003 11:24:41 +0000, David Lowndes <da****@mvps.org>
wrote:
In .Net 2003 if a line, read from a text file is larger
than a size parameter, the ifstream getline(buff, sze) put
the file pointer to the EOF, so next peek() returns EOF.

I saw this problem also when size was 2000 but line was
1200 bytes long.
There is no such problem with .Net 2002
The same function in .Net 2002 works fine.
12345678901234567890
987654321

After calling getline, the buffer contains "1234" (and a null
character), and the following call to peek always (it seems incorrect
to me) returns eof.
Once a stream is in an error state, (almost) all functions will fail.
To reset the error state, call "stream.reset()".

int_type peek();
27 Returns: traits::eof() if good() is false. Otherwise, returns
rdbuf()*>sgetc().

That's clear enough - after trying to read a line into a buffer that
is too small, the "fail" bit of the stream is set (note that eof()
will return false still though, since eof hasn't been hit). With the
fail bit set, good() is false and hence peek() returns EOF.
Stepping through the code, it appears that peek fails because the
getline method sets the state of the stream to a failure case because
the buffer end is reached. Skipping past this check does return the
next character in the file, so the file pointer would appear to be
correct.

I don't usually use with these stream methods, so I'm not sure what
the expected behaviour should be.


The behaviour is correct. The standard library has been tested with
several different proofing tools and is therefore unlikely to contain
such trivial bugs.

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Nov 17 '05 #3
>Once a stream is in an error state, (almost) all functions will fail.

That's reasonable, I guess the issue is should using getline in that
manner put the stream into an error state? I think that's perhaps the
counter-intuitive aspect - though it's (sort of) documented to do that
in the latest docs:

"If the function extracts no elements or _Count - 1 elements, it calls
setstate(failbit)"
To reset the error state, call "stream.reset()".
Err, how?

Aha, it seems to be called the clear() method.
The behaviour is correct. The standard library has been tested with
several different proofing tools and is therefore unlikely to contain
such trivial bugs.


Perhaps it should be tested on users though - to see what percentage
of real programmers find it intuitive/straightforward to use. I
suspect that's a much harder test, and one that I think much of STL
and its documentation will fail miserably at. :)

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #4
>Aha, it seems to be called the clear() method.

I should have mentioned before I got carried away, calling clear()
does resolve the issue.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #5
Dave <an*******@discussions.microsoft.com> wrote:
[problems with 'getline(buff, sze)']
Did you have a look at
std::istream& std::getline(std::istream&,std::string&)
generally, it is a lot easier to use.
Thanks,
Dave


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Nov 17 '05 #6
David Lowndes wrote:

That's reasonable, I guess the issue is should using getline in that
manner put the stream into an error state? I think that's perhaps the
counter-intuitive aspect - though it's (sort of) documented to do that
in the latest docs:

"If the function extracts no elements or _Count - 1 elements, it calls
setstate(failbit)" ....
The behaviour is correct. The standard library has been tested with
several different proofing tools and is therefore unlikely to contain
such trivial bugs.


Perhaps it should be tested on users though - to see what percentage
of real programmers find it intuitive/straightforward to use. I
suspect that's a much harder test, and one that I think much of STL
and its documentation will fail miserably at. :)


I think the important distinction is that you're calling getline and not
get. The point of getline is to read a line of data (ending with the
terminating char) into a buffer. In your example the full line would
not fit into the buffer so the operation was considered to have failed,
ie. getline basically requires that the delim be seen before count is
reached in order to succeed. If it seems counter-intuitive I'd argue
that it's because the MSDN docs aren't clear enough on what the function
is supposed to accomplish.
Perhaps it should be tested on users though - to see what percentage
of real programmers find it intuitive/straightforward to use. I
suspect that's a much harder test, and one that I think much of STL
and its documentation will fail miserably at. :)


It's probably a lot to expect from the average programmer, but this is
exactly the reason I keep a copy of the standard handy. When other docs
are unclear I still have an authoratative source to turn to.
Sean

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick...
31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
12
by: Gaurav | last post by:
Hello I have a program that basically inverts the contents of files except first line. It compiles fine but gives me core dump on running. If i comment temp.clear() it runs fine, but i need...
44
by: Jim M | last post by:
I have had great success with using <iframe> with overflow-y set to auto. I can get a similar look with the <iframe> tag. BUT... In all cases I need to have fixed heights. Is there a way to...
3
by: Piotre Ugrumov | last post by:
I have done the overload on the operator >> and << in the class Attore. These 2 overload work correctly. I have done the overload of the same overload in the class Film. The class film ha inside...
6
by: me | last post by:
Hi guys - the question is in the subject line. I thought of one quick way: std::ifstream input("myfile.dat") ; std::istreambuf_iterator beg(input), end ; std::vector DataFile(beg,end) ;
15
by: keweiming | last post by:
I have a project which needs to open hundreds to thousands of files for writing. The following is a simplified test program I wrote to see if I can use a map<string, ofstream> object to keep the...
3
by: Lambda | last post by:
I'd like to load a text file to memory. The code is like: void load_dict() { static const string DICT("D:\\README.txt"); ifstream dict(DICT.c_str(), std::ios::in); if (!dict) {
6
by: chesshan | last post by:
Hi Everyone, I am very new to this forum. i have a doubt in HTML where i have designed a Javascript, as signature in which if you drag ur mouse over my name it wil give u my complete details and if...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.