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.

Re: File Read /fstream queries

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.

Re: File Read /fstream queries

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.

Re: File Read /fstream queries

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".

Re: File Read /fstream queries

> 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 . 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.

Re: File Read /fstream queries

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.

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. 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 .

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.

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.

Re: File Read /fstream queries

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.