473,386 Members | 1,752 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,386 software developers and data experts.

ifstream/getline

The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
15 2789
On Thu, 1 Apr 2004 14:57:37 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}


Is this an interview question? ;-)
Why don't you tell us why you think there's something wrong with it. I
could enumerate all the reasons it doesn't compile, but that would just be
a waste of both of our time...
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Christopher Benson-Manica wrote:
The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
std::string s;
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}


return 0;
}

Now it should compile.
Jul 22 '05 #3
Leor Zolman <le**@bdsoft.com> spoke thus:
(I wrote)
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}
Is this an interview question? ;-)
Nah, just an "I'm dumb today!" question, as I indicated.
Why don't you tell us why you think there's something wrong with it.
Because nothing gets printed? I know the file exists, and I know I
can open it. I don't see what I'm doing wrong (obviously *sigh*). I
create an ifstream, see whether it's open and then read from it.
getline(f,s) returns f, and that's true unless end of file has been
reached, right?
I
could enumerate all the reasons it doesn't compile,
If it isn't supposed to compile, I've got problems, because it
compiles cleanly.
but that would just be a waste of both of our time...


I'm amazed you're still answering my posts, quite frankly ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #4
Christopher Benson-Manica wrote:
If it isn't supposed to compile, I've got problems, because it
compiles cleanly.


Then you really got problems or you didn't post the full code.
Jul 22 '05 #5
Aggro <sp**********@yahoo.com> spoke thus:
Then you really got problems or you didn't post the full code.


Well, clearly it's a snippet.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #6
Christopher Benson-Manica wrote:
Well, clearly it's a snippet.


Please read:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Jul 22 '05 #7
Aggro <sp**********@yahoo.com> spoke thus:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


FAQ or no, not every single post is, or needs to be, a complete C++
program.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #8
On Thu, 1 Apr 2004 16:05:36 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Leor Zolman <le**@bdsoft.com> spoke thus:
(I wrote)
std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}
Is this an interview question? ;-)
Nah, just an "I'm dumb today!" question, as I indicated.


I know, that's why I had the smiley. I was giving you a bit of grief
because I expected you to know, by now, to provide a more complete code
example and explain specifically what the problem you were having was. If
someone stuck a code fragment in front of you and just said, "What's wrong
with this?", how would /you/ react?
Why don't you tell us why you think there's something wrong with it.


Because nothing gets printed? I know the file exists, and I know I
can open it. I don't see what I'm doing wrong (obviously *sigh*). I
create an ifstream, see whether it's open and then read from it.
getline(f,s) returns f, and that's true unless end of file has been
reached, right?


What are the contents of the file? I actually tried this and it worked for
me. I defined s as a string. (And I found a minor bug in the C++ standard
while looking for whether or not getline as you wrote it would work with s
defined as a char[]... something I wouldn't have needed to check, BTW, if
you'd shown how s was defined)
I
could enumerate all the reasons it doesn't compile,


If it isn't supposed to compile, I've got problems, because it
compiles cleanly.
but that would just be a waste of both of our time...


I'm amazed you're still answering my posts, quite frankly ;)


If you're hoping to earn the "Most Annoying Poster" crown, you haven't got
a prayer ;-)
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #9
Leor Zolman <le**@bdsoft.com> spoke thus:
I know, that's why I had the smiley. I was giving you a bit of grief
because I expected you to know, by now, to provide a more complete code
example and explain specifically what the problem you were having was. If
someone stuck a code fragment in front of you and just said, "What's wrong
with this?", how would /you/ react?
Well, I figured it was simple enough to get away with - I included "if
anything" because I didn't see any reason for it not to work... I
guess I should have explained better. Sorry.
What are the contents of the file? I actually tried this and it worked for
me. I defined s as a string. (And I found a minor bug in the C++ standard
while looking for whether or not getline as you wrote it would work with s
defined as a char[]... something I wouldn't have needed to check, BTW, if
you'd shown how s was defined)
I didn't show a definition for s, did I...? Um, well, I did say I was
feeling stupid today :-< My apologies. s is a std::string, FWIW.
I'm leaning toward blaming my implementation again, actually... oh
well.
If you're hoping to earn the "Most Annoying Poster" crown, you haven't got
a prayer ;-)


Well, that's certainly heartbreaking - I'll have to take the rest of
the day off to mope and brood over that one ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #10

"Christopher Benson-Manica" <at***@nospam.cyberspace.org> wrote in message
news:c4**********@chessie.cirr.com...
Aggro <sp**********@yahoo.com> spoke thus:
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8


FAQ or no, not every single post is, or needs to be, a complete C++
program.


Complete programs are far more likely to engender
useful diagnosis.

-Mike
Jul 22 '05 #11
Mike Wahler <mk******@mkwahler.net> spoke thus:
Complete programs are far more likely to engender
useful diagnosis.


Agreed, I'm just making the point that some code snippets manage to
get useful commentary in spite of their snippiness (if you will). The
expected diagnosis here, of course, is the same as always - that I'm
dumb :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #12
Christopher Benson-Manica wrote:
The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstream f( "myfile.txt" );
if( !f ) {
cerr << "Couldn't open file\n";
}
while( getline(f,s) ) {
cout << s << '\n';
}


I'd go paranoid and try

std::ifstream f( "myfile.txt",std::ios::in);
if( !f ) {
cerr << "Couldn't open file" << endl;
}
while( getline(f,s) ) {
cout << s << endl;
}

if it still doesn't work, then my crystall ball thinks
a) you have a serious flaw in some beyond-the-snippet code,
like a memory problem or, generally, an UB somewhere else.
b) you have cout/cerr redirected :) or their goodbit is cleared.
c) myfile.txt is malformed (like by odd end-of-line translation, etc.)
d) try waiting until April the 2nd and see if it does better :)

if neither, then I'm stumped.

You could try inserting an infinite loop in either if or while
to see if it indeed starts to loop infinitely -- that way you'll
know it's your output that's broken, not the stream stuff. Or the
other way round, of course.

- J.
Jul 22 '05 #13
Jacek Dziedzic wrote:
b) you have cout/cerr redirected :) or their goodbit is cleared.


In reality there is no goodbit, is there? goodbit is considered "set"
when all the error bits are cleared, unless I'm mistaken.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #14
Kevin Goodsell wrote:
Jacek Dziedzic wrote:
b) you have cout/cerr redirected :) or their goodbit is cleared.

In reality there is no goodbit, is there? goodbit is considered "set"
when all the error bits are cleared, unless I'm mistaken.


Yes, std::ios_base::goodbit is defined to be 0 in the standard, e.g.
at 27.4.2.1.3/2.

Regards,
Buster.
Jul 22 '05 #15
On Thu, 1 Apr 2004 17:08:54 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.cyberspace.org> wrote:
Mike Wahler <mk******@mkwahler.net> spoke thus:
Complete programs are far more likely to engender
useful diagnosis.


Agreed, I'm just making the point that some code snippets manage to
get useful commentary in spite of their snippiness (if you will). The
expected diagnosis here, of course, is the same as always - that I'm
dumb :)


I think it was Mike who recently made the point "If you think it, it will
be true".

I think I know where you're coming from re. the "snippet" issue, but given
that you've seen already in this very thread how omission of pieces just
makes it more difficult for us to assist you, I'm not sure why you were
giving Mike grief here. Get over it, write some code, and most importantly,
/please/ stop trying to convince yourself and tens of thousands of
newsgroup readers of your inferiority! (I don't know if tens of thousands
is the right number or not, but stop doing it anyway)
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #16

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

Similar topics

11
by: John | last post by:
Hello all, I am trying to read in lines into a buffer from a file. Normally I would do this very low-level, but I have come to the conclusion I must stop doing everything the hard way. So, I...
4
by: Joe | last post by:
Hello - I wrote a program that uses ifstream to open an ASCII file and getline() to read in the lines. The problem is when I try to open the same file again later in the code. I used close()...
15
by: Christopher Benson-Manica | last post by:
The dumb-o-meter's pegging out today... What, if anything, is wrong with the following code? std::ifstream f( "myfile.txt" ); if( !f ) { cerr << "Couldn't open file\n"; } while( getline(f,s)...
1
by: tinks | last post by:
I am getting a linking error when I do something like this: ifstream dataFile; dataFile.open(dataFileName_, ios::in); while(dataFile) { dataFile.getline(buffer, MAX_DATA_FILE_LINE_LEN); //...
6
by: Dave | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...

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.