473,837 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.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 5297
>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<cha r>::eof() == ret )
{
cout << "EOF returned";
}
else
{
cout << "Not EOF";
}

is.close();

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

123456789012345 67890
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.or g>
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.
12345678901234 567890
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(failbi t)"
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*******@disc ussions.microso ft.com> wrote:
[problems with 'getline(buff, sze)']
Did you have a look at
std::istream& std::getline(st d::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(failbi t)" ....
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
3199
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 overview... I have four sets of co-ordinates in each 'facetInfo', and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects that have y-values of zero (.v1y, .v2y and .v3y), and return a 'facetInfo' vector with the remaining...
31
14375
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? Basically: "Press any key to continue..." I beleive that I am looking for is something along the lines of a....
12
5071
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 it to clear the temp vector for each file. ********************* code *******************
44
919
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 put either tag inside an HTML <TD> and have the same kind of scrolling effect. This would allow me to fill the screen and have the size of the scrolling box change on resize. Thanks in advance.
3
2252
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 an array of pointer to Attore. I have written these overload in these ways. the overload of << work correctly the overload of >> I don't know. I compile the class correctly, when I insert a film through the operator >> I don't receive error, but...
6
4139
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
6967
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 list of ofstreams. I need to have them open simultaneously for writing -- I have millions of rows of data to write to them so opening and closing all the time will be unacceptable in efficiency. The following program compiles but failed to run....
3
1467
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
2898
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 take my mouse out nothing would appear. but i cannnot attach this code to my Outlook 2003 as this as javascript. can anyone help me to do the same in HTML. Outlook will accept only HTML based coding. Here is my code <HTML> <HEAD> <script...
0
9686
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10581
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10634
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9416
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7819
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5855
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4053
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3127
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.