Testing for newline and end-of-file chars??

Started by
4 comments, last by paulcoz 22 years, 8 months ago
I'm using fgets to read the lines in a text file into a char buffer (chars[100]). Once I have a line in the buffer, I want to remove the end-of-line and end-of-file chars if possible: char cchar; int cnt = 0; cchar = chars[cnt]; while (cchar != '\n' && cchar != '\0') { // char is valid - add to final string cnt++; // increment final string length counter cchar = chars[cnt]; } (1) Can you use \n and \0 like that? \n and \0 are the values for the newline and end-of file chars, right? (2) Are there any other formatting characters I need to remove (maybe carriage-return?)? (3) If you know that you have read in all the values for the current line in a delimited file, how can you tell the data pointer to start reading at the next line (eg. skip any end-of-line / carriage-return characters on the current line)? Paulcoz. Edited by - paulcoz on July 31, 2001 8:28:47 PM
Advertisement
It''s easy to do. We know that the last character will be the NULL terminator, and the second-to-last character will be the newline, so what we can do is replace the newline with a NULL terminator, thereby ending the string sooner and eliminating the newline!

  string[ strlen(string) - 2 ] = ''\0'';  
quote:(1) Can you use \n and \0 like that? \n and \0 are the values for the newline and end-of file chars, right?

Yes, you can, and no, they're not. Yes, \n is newline, but \0 is just the end-of-string delimiter. If you want to see if you hit the end of the file, you need to check the return value of fgets. I have no idea whether it guarantees the terminating '\0' if it returns NULL. Try it and see, perhaps.

quote:(2) Are there any other formatting characters I need to remove (maybe carriage-return?)?

That all depends what has been put in the text file in the first place. And what you want to do with it. If it's UNIX format, you won't see any carriage returns. And I believe if it's Mac format, you won't see any newlines. I think Windows format does both ("\r\n"). That's not to say that every Windows program saves in that format, though. This is something that you have to ascertain for your own situation.

quote:(3) If you know that you have read in all the values for the current line in a delimited file, how can you tell the data pointer to start reading at the next line (eg. skip any end-of-line / carriage-return characters on the current line)?

Read (and ignore) characters until you find an end-of-line character.
Then skip the end-of-line character. You might have to skip more than one if it's a carriage return/new line pair.

Edited by - Kylotan on July 31, 2001 11:41:58 PM
One last question - do these new-line and end-of-file characters (escape codes?) take up one character space or two? Is it like ''\'' then ''n'' in the file, or is the compare ''\n'' converted to a value for comparison purposes?

Does skipping a new-line code for example involve skipping two chars or one?

Paulcoz.
The \n is one character. The slash is just a code that tells the compiler that the next character is not to be taken literally, but represents a control code. ''\n'' is character 10, I believe, ''\r'' is 13, etc. The printable characters start at 32.
Thanks for your help - I understand how the codes work now.

Paulcoz.

This topic is closed to new replies.

Advertisement