I know this is a simple question, but I have tried something similar in a program and doesn't work, only giving me an infinite loop. Is this expression a valid one for controlling loops?
Code:while((c=fgetc(fp)) != '\n')
I know this is a simple question, but I have tried something similar in a program and doesn't work, only giving me an infinite loop. Is this expression a valid one for controlling loops?
Code:while((c=fgetc(fp)) != '\n')
If memory serves me correctly \n is not one character but two.
\n executes both a line feed and carriage return (10 and 13 I think).
Edit: Looked at a file, its 13 and 10. And to answer directly no its not a valid control
would there be a way to test for reading a file to the end of a line character by character?
off the top of my head:
There are better ways to check for the end of a line but if you are set to do it by getting one character then that should work.Code:while ( (c=fgetc(fp)) != 10)
Why not just read in the line using fgets( ) and then go through it character by character.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
No. It's one. \ is an escape character which doesn't actually count. You treat escaped character combos as a single character.Originally posted by Thantos
If memory serves me correctly \n is not one character but two.
Quzah.
Hope is the first step on the road to disappointment.
\n is two characters on a Windows system. With Windows, '\n' is a carriage return followed by a line feed.
Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah
You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie
Arg! No it isn't! When you use '\n' in C, it is a single character. The only time it's "two characters" is on disk, which is nothing you ever check for anyway. And even then, if you open in binary mode, you can read them as single characters anyway. Once it is read, it is simply treated as a single character. It is a single character, which is why you can stick it in a char. If it wasn't, you couldn't.Originally posted by XSquared
\n is two characters on a Windows system. With Windows, '\n' is a carriage return followed by a line feed.
[edit]
To clarify, in Windows, if you write in text mode, it writes a \n\r pair. However, \n is its own character, and \r is another. \n itself is never more than one character.
[/edit]
Quzah.
Last edited by quzah; 08-06-2003 at 07:32 PM.
Hope is the first step on the road to disappointment.
I don't understand. Why isn't
valid for controlling loops, yetCode:while((c=fgetc(fp)) != '\n')
is?Code:while ( (c=fgetc(fp)) != 10)
Aren't they exactly the same to the compiler? Also, I can't see anything wrong with the loop (provided that c is declared as an int and not a char, and that EOF is being checked within the loop body at the beginning)
Cheers,
Z.
Beware the fury of a patient man.
thanks for the clarification. Here's my final, complete code segment. It looks to me like it's ok, but I don't want to run into any more infinite loops and gigabyte size text files :-D.
Code:while(check=fread(&buf,sizeof(char),3,fpin)!=EOF || 0) { while(buf=fgetc(fpin) != '\n'|| EOF) { if(fwrite(&buf,sizeof(char),1,fpout)==0) { printf("Disk write error"); exit(EXIT_FAILURE); } } }
That's wrong. It should be:while(check=fread(&buf,sizeof(char),3,fpin)!=EOF || 0)
while( fread(...) == 3 )
First off, you don't actually do anything with 'check', so why bother using it?
Second, "|| 0" will never evaluate to true, so you don't want that.
[edit]
The second loop check is wrong also for the same reason. The logic of the check is wrong.
[/edit]
Quzah.
Last edited by quzah; 08-07-2003 at 05:13 PM.
Hope is the first step on the road to disappointment.