473,698 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #1
15 2828
On Thu, 1 Apr 2004 14:57:37 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote:
The dumb-o-meter's pegging out today... What, if anything, is wrong
with the following code?

std::ifstrea m 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.co m> spoke thus:
(I wrote)
std::ifstre am 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)cybers pace.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**********@y ahoo.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)cybers pace.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**********@y ahoo.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)cybers pace.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.c yberspace.org> wrote:
Leor Zolman <le**@bdsoft.co m> spoke thus:
(I wrote)
std::ifstrea m 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.co m> 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)cybers pace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #10

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

Similar topics

11
37907
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 thought I would try <fstream>. The code below causes an infinite loop in the while() statement. Debugging, I see that the buffer just has a 0 in the first position, and nothing else.
4
3686
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() to close the file but the next open() fails. If I comment out the getline() then I can open it a second time without a problem. Here are parts of the code: ifstream InputFile; char Line; // MAXcharLine = 512
15
8421
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) ) { cout << s << '\n'; }
1
2878
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); // This line creates linking issue on solaris }
6
5289
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 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>
0
9031
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
8901
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
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
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
5862
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
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
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
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.