Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifstream/getline

Reply
Thread Tools

ifstream/getline

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-01-2004
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.
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-01-2004
On Thu, 1 Apr 2004 14:57:37 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Aggro
Guest
Posts: n/a
 
      04-01-2004
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.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-01-2004
Leor Zolman <(E-Mail Removed)> 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.
 
Reply With Quote
 
Aggro
Guest
Posts: n/a
 
      04-01-2004
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.
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-01-2004
Aggro <(E-Mail Removed)> 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.
 
Reply With Quote
 
Aggro
Guest
Posts: n/a
 
      04-01-2004
Christopher Benson-Manica wrote:

> Well, clearly it's a snippet.


Please read:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-01-2004
Aggro <(E-Mail Removed)> 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.
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      04-01-2004
On Thu, 1 Apr 2004 16:05:36 +0000 (UTC), Christopher Benson-Manica
<(E-Mail Removed)> wrote:

>Leor Zolman <(E-Mail Removed)> 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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      04-01-2004
Leor Zolman <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments