Well good for you. You go on doing what you're doing incorrectly. Personally I don't care that you do things wrong. After all, you're a professional. I however will attempt to show people the correct way to do things.
Quzah.
Well good for you. You go on doing what you're doing incorrectly. Personally I don't care that you do things wrong. After all, you're a professional. I however will attempt to show people the correct way to do things.
Quzah.
Hope is the first step on the road to disappointment.
So show me what is wrong, oh guru of gurus! Show me how smart you are! Your FAQ tells me nothing about why my code is wrong.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
Ehm... what's wrong with char c ?;
do u want an unsigned char ? :O
actually u continued to say that the code is wrong.. can u write the best code ever for this kind of problem pls ?Originally Posted by quzah
garrr -please delete this post mr admin
Last edited by Giant; 07-14-2005 at 07:22 AM.
use int, that's the right way.
cause char cannot handle EOF
fgetc() returns an INT and EOF is an INT which means that when truncating it to a char you will never see EOF.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
Ahh..... ok
But... so it works only because ctrl Z (windows) can be contained in a char
I mean.. if i run it on unix, where EOF is return ctrl Z, it should'n work.. right ?
I think the idea is this:Your FAQ tells me nothing about why my code is wrong.
1. feof() tests for EOF, but doesnt set it.
2. fgetc() sets EOF.
3. EOF is only set when fgetc() is called after all the data has been read.
So if one were to use a loop that tests with feof(), the loop will run one more time than intended, i.e. extra iteration is when fgetc() sets and returns EOF.
I dont think this is a problem with your code though, since you do check to return value of fgetc() within the loop.
Read Definition of EOF and how to use it effectively.Ehm... what's wrong with char c ?;
do u want an unsigned char ? :O
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
hehe lol :POriginally Posted by From EOF FAQ
Actually i've already read that faq some months ago but i didn't remember ..
hehe sorry :P
if the last line of the file have no '\n', then your program will miscount the sum of lines.Originally Posted by mitakeet
your program will count it as it has only 2 lines. in fact, it has 3 lines.fafa
aaa
afadf//no newline here
it's ok to use feof() here, but feof() isn't always right, it can cause problem easily.
given a program like this
and the file a like thisCode:#include <stdio.h> int main( void ) { int i = 0; char buf[100]; FILE *fp = fopen( "a", "r" ); while ( !feof(fp) ) { fgets( buf, sizeof(buf), fp ); printf( "Line %4d: %s", i, buf ); i++; } return 0; }
then the out put will be245a6
a12
ffff
f221
21aa//a newline here
see what's wrong?Line 0: 245a6
Line 1: a12
Line 2: ffff
Line 3: f221
Line 4: 21aa
Line 5: 21aa
so the use of feof() is discourage
While you certainly have an argument for a logical flaw in counting the number of lines, I still fail to see how my code is incorrect if you were to look at it in isolation. If you simply want to count the number of linefeeds (or any other character), this code should work perfectly without any bugs.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw
Yes your code is correct.
What I want to say is that feof() can easily cause problems.
I agree with your statement and the information in the FAQ, what I don't agree with is Quzah telling me I am wrong for using feof() and failing to back that statement up. If he is so sure I am wrong, where is the explanation? He couldn't even provide your explanation (which is perfectly correct given the OPs original question), just snide comments.
Free code: http://sol-biotech.com/code/.
It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up
The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw