File Read /fstream queries

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1139 Views - Last Post: 27 June 2011 - 08:18 AM Rate Topic: -----

#1 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

File Read /fstream queries

Posted 26 June 2011 - 12:48 PM

Hi again,

Recently while I was studying a C++ tutorial, I wrote this piece of code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
	string text;
	ifstream myFile ("data.txt");
		while (!myFile.eof())
		{
		getline(myFile, text);
		cout << text << endl;
	    }
		cin.get();
}


Now the problem is I really don't understand this statement:

while (!myFile.eof())


Please explain me ! I am just a beginner, so the more lucid the explanation, the better !

If you feel there's something I need to learn beforehand/any enhancement to the code, please convey it to me ! I am ready to learn. Also how to check if the file already exists or not. Also please explain the ! operator.

Thanks :).

This post has been edited by Jeet.in: 26 June 2011 - 12:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: File Read /fstream queries

#2 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1645
  • View blog
  • Posts: 3,121
  • Joined: 30-May 10

Re: File Read /fstream queries

Posted 26 June 2011 - 12:55 PM

It's depressing that after 50 posts, you're still using void main.

You need to start learning what the language allows you to use, not what your current compiler will let you get away with. At least turn up the warning level to is maximum value.

As for the feof() thing, read this
http://sourceforge.n....php?title=Feof
Was This Post Helpful? 0
  • +
  • -

#3 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read /fstream queries

Posted 26 June 2011 - 01:41 PM

Quote

It's depressing that after 50 posts, you're still using void main.


I don't want to start a debate, I am too novice to do that, but would like to quote a word or two from NickDMax:

Quote

void main() is not evil either. I agree that it should be avoided because it hides something from the programmer and is "non-standard" but it will not blow up the OS or fry hard drives and cause war in the middle-east. What it WILL do is get you pounced on by know-it-all forum posters.

You know I actually get mad when someone posts a question and all they get back is: Don't use void main() -- WTF. Honestly the only real problem with using void main() is that it gets you harassed on forums. Other than that the compiler slips in the returned int for you and all is well. It does not cause children to starve or rain not to fall. I suppose it *could* cause problems if you called main() recursively but that is entirely different subject.


Quote

You need to start learning what the language allows you to use, not what your current compiler will let you get away with. At least turn up the warning level to is maximum value.


Doesn't help at all.

Please be a bit more lucid. Just pointing to a link hardly helps a beginner.

This post has been edited by Jeet.in: 26 June 2011 - 01:50 PM

Was This Post Helpful? -2
  • +
  • -

#4 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: File Read /fstream queries

Posted 26 June 2011 - 02:19 PM

( !myFile.eof() ) is a shorthand for ( myFile.eof() == false ).
Conversely, ( myFile.eof() ) will be a shorthand for ( myFile.eof() == true ).


eof() returns TRUE if End-of-File is reached, and FALSE if it is not reached yet. Therefore, the statement is saying, "loop while myFile.eof returns false (which means that the end of file has NOT reached yet), and exit loop if it returns true (which means that the end of file has been reached).


I hope that was "lucid" enough for you. If you need an even more in-depth explanation, you can ask again.




Also, my father used to say that when someone senior to you scolds you, no matter how bad it is or how demeaning, just listen, since you are the one who is learning. This is some Asian mentality (hence, my name), but I think it's good.

This post has been edited by sungchoiok: 26 June 2011 - 02:27 PM

Was This Post Helpful? 2
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,495
  • Joined: 23-August 08

Re: File Read /fstream queries

Posted 26 June 2011 - 02:19 PM

It doesn't matter what NickDMax says where C++ and void main() is concerned. What matters is what Bjarne Stroustrop, the creator of C++ says:

Quote

The definition

void main() { /* ... */ }

is not and never has been C++, nor has it even been C. See the ISO C++ standard 3.6.1[2] or the ISO C standard 5.1.2.2.1. A conforming implementation accepts

int main() { /* ... */ }

and

int main(int argc, char* argv[]) { /* ... */ }

A conforming implementation may provide more versions of main(), but they must all have return type int.

Was This Post Helpful? 3
  • +
  • -

#6 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read /fstream queries

Posted 26 June 2011 - 09:48 PM

I am extremely sorry if my post showed misbehavior. But if void main is so bad, why the compiler accepts it in the first place? If it's so evil why not kill it? And thanks all of you for explaining.


@sungchoiok: I am also an Asian. However scolding should take place after explaining. It's frustrating to get scolding before the "scolded" knows why he\she is being scolded.
Was This Post Helpful? 0
  • +
  • -

#7 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: File Read /fstream queries

Posted 26 June 2011 - 10:02 PM

View PostJeet.in, on 26 June 2011 - 11:48 PM, said:

I am extremely sorry if my post showed misbehavior. But if void main is so bad, why the compiler accepts it in the first place? If it's so evil why not kill it? And thanks all of you for explaining.


@sungchoiok: I am also an Asian. However scolding should take place after explaining. It's frustrating to get scolding before the "scolded" knows why he\she is being scolded.



haha. You're Asian too?
The point was that if somebody who knows more than you do on a particular subject gives you criticism, at least, you shouldn't talk back, &c; and you are right that it is frustrating when somebody tells you to don't do something without giving you the reason why you shouldn't; but nobody is perfect and is going to give you the explanations every time. Besides, as the old saying goes, "beggars can't be choosers".
Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,400
  • Joined: 25-December 09

Re: File Read /fstream queries

Posted 26 June 2011 - 10:11 PM

The biggest problem with using eof() in a loop control is that the eof() flag is not set until after it fails to read a value. Therefore the following loop:

	while (!myFile.eof())
		{
		getline(myFile, text);
		cout << text << endl;
	    }


Will produce one extra entry. Because you did not check eof() after you tried to read the line. The proper way to use eof() is:
getline(myFile,text);
while (!myFile.eof())
{
   cout << text << endl;
   getline(myFile, text);
}


Notice the getline before your loop and that the next getline is at the very end of your loop.

The best way to would be to use your getline() in the while:
while (getline(myFile, text))
{
   cout << text << endl;
}

As this will test for all fail conditions when reading the file, not just eof().

Jim

This post has been edited by jimblumberg: 26 June 2011 - 10:13 PM

Was This Post Helpful? 3
  • +
  • -

#9 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read /fstream queries

Posted 26 June 2011 - 10:27 PM

@sungchoiok :) Yup, am an Indian ! And you are definitely right, beggars cannot be choosers.

@jimblumberg Superb explanation ! sungchoiok already hepled me grasp the code, now it's all clear ! ! ! Thank you so much !!! Really love DIC for this reason, everybody is willing to help !!!
Was This Post Helpful? 0
  • +
  • -

#10 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1645
  • View blog
  • Posts: 3,121
  • Joined: 30-May 10

Re: File Read /fstream queries

Posted 26 June 2011 - 11:00 PM

> @jimblumberg Superb explanation !
Funny - that's what the FAQ entry I posted basically said.

> Just pointing to a link hardly helps a beginner.
Did you actually read it, or did you just sound off because the answer wasn't inline for you to read without having to move the mouse?

A better come-back would have been "I read the link, tried the code and I'm still confused about..."
Right now, I'm wondering if it's even worth the effort responding to any more of your posts.
Was This Post Helpful? 0
  • +
  • -

#11 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read /fstream queries

Posted 26 June 2011 - 11:22 PM

View PostSalem_c, on 26 June 2011 - 11:00 PM, said:

> @jimblumberg Superb explanation !
Funny - that's what the FAQ entry I posted basically said.

> Just pointing to a link hardly helps a beginner.
Did you actually read it, or did you just sound off because the answer wasn't inline for you to read without having to move the mouse?

A better come-back would have been "I read the link, tried the code and I'm still confused about..."
Right now, I'm wondering if it's even worth the effort responding to any more of your posts.


Look Salem_c I have got no problems with your post. Maybe I found the link ( I read it ) unhelpful due to the fact that it was way too perplexing for a beginner. Our intelligence level vary, and :stupid: . However please do reply to my other queries. I cannot afford to miss your replies. Life is a big game so please don't take my previous post badly,. I am sorry if it hurt you. I really believe you are an expert in this field and respect you too.

I AM SORRY :sad3: !

This post has been edited by Jeet.in: 27 June 2011 - 12:07 AM

Was This Post Helpful? 0
  • +
  • -

#12 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 768
  • View blog
  • Posts: 2,241
  • Joined: 20-March 10

Re: File Read /fstream queries

Posted 27 June 2011 - 12:59 AM

I think what NickDMax was saying he was fed up
with posters just saying void main is bad while giving nothing else.

Clearly Salem_c did not do this...

and nor would he is an expert on this and other forums for a reason.

he gave you a direct link to the answer and asked you to figure it out
yourself.

In fact he wasnt just giving you a fish he was teaching you to fish.

and that is what wise men do.

If your going to grow as a programmer then you will need to use
the internet as a programming reference tool.

This forum is great for finding answers but you should also try to do things for yourself its the only way you are going to grow as a programmer and get better as a programmer which is what we all want you to become and that is why you have been scolded so badly.

Take your licks learn from that and keep smiling. :)

Best Wishes Snoopy.
Was This Post Helpful? 1
  • +
  • -

#13 Jeet.in  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 304
  • Joined: 30-May 11

Re: File Read /fstream queries

Posted 27 June 2011 - 01:30 AM

snoopy11 it's OK, just a thought, how can you fish without the fishing rod? I wanted that rod that's all. I am really a beginner, not even an intermediate, with no prior knowledge of programming whatsoever. And I do use the internet a lot. The questions I put in this forum is a small subset of what I do to learn. I ask my seniors, rendezvous with my instructor etc. I know it's going to be my career and I am serious about it.
What Salem_c wrote/the link looked completely meaningless to me, I admit it's my lack of knowledge, he was trying to help, but how do I know that he WAS trying to help in the first place?

sungchoiok was actually right. Beggars cannot be choosers. Even if I don't understand something, I guess I need to write that I understood & not my own views on the post, else get negative reps/get scolded. :surrender: I apologized to Salem_c as a gesture of goodwill, but the scolding/reps hardly matters to me, my digital life dies as soon as I switch off my PC, but learning does matter.

And now I feel sorry for asking this question here in the first place :( .

This post has been edited by Jeet.in: 27 June 2011 - 01:55 AM

Was This Post Helpful? 0
  • +
  • -

#14 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1645
  • View blog
  • Posts: 3,121
  • Joined: 30-May 10

Re: File Read /fstream queries

Posted 27 June 2011 - 07:56 AM

> But if void main is so bad, why the compiler accepts it in the first place?
Because they're a commercial vendor keen to fill the compiler with as many bells and whistles to satisfy the widest possible crowd of programmers. In this instance, all the extant DOS programmers who grew up on a diet of void main, because there wasn't much you could do about it if the program failed in any way. Chances are, if your program failed badly enough in DOS, it took the OS with it, so the whole idea of an exit status was a moot point.

To begin with, turn up your compiler warning level to maximum (-Wall -ansi for gcc, /W4 /Za for Microsoft).

See, this is why I said you needed to learn the language, NOT the compiler. Getting code to compile on a single compiler is like falling off a log - it's piss easy. Now getting code to compile on many compilers, that takes a bit of skill.

Why is this important? Well do you really want to spend your time re-learning a whole bunch of vendor specific magic tricks, every time there is a new version / update / patch? You'll use dozens of compilers in your career, sometimes several at once. You really don't have the time to mess about with learning the minutiae.

Here is a simple (but flawed) program compiled with MANY different compilers. Just look at the chaos in the results.

Also consider that on forums like this, we get a regular helping of "I upgraded my compiler and now it doesn't...." all because the original author played fast and loose and took whatever the old compiler gave them (bugs and all).

Yes you will need to use compiler and OS specific things down the line, but there is little (if any) need to do so while you're learning. When that time comes, you'll at least be making an informed choice.
Was This Post Helpful? 3
  • +
  • -

#15 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 140
  • Joined: 18-July 10

Re: File Read /fstream queries

Posted 27 June 2011 - 08:13 AM

View PostJeet.in, on 27 June 2011 - 03:30 AM, said:

And now I feel sorry for asking this question here in the first place :( .


Don't be; we all make mistakes and we all learn from it..


And I noticed that nobody explicitly explained why you shouldn't use "void main()". (that's the reason for all this drama afterall) EDIT: Salem beat me to it

The standard in c++ says that every program should return an integer indicating its success. If the execution was successful, it should return 0 or EXIT_SUCCESS. If it failed, it returns non-zero value--perhaps, some kind of error code along with it. If you declare your main as a void, then obviously, it's not going to return any value and the Operating System is going to think that your program failed. Now, that doesn't matter when you are writing 10-line codes, but in more complex projects, one day, you might run your program and it randomly dies, does strange things, (blow your computer up), or not compile at all. Also, there's no guarantee that the next update for your compiler will accept void main() as a valid entry point.

That's a nutshell of why you technically shouldn't use void main().

This post has been edited by sungchoiok: 27 June 2011 - 08:17 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2