I am reading txt data in binary. But I am not sure how to break the lines up, based on "\n", after reading the file content into a buffer. Any hints?
Printable View
I am reading txt data in binary. But I am not sure how to break the lines up, based on "\n", after reading the file content into a buffer. Any hints?
use strchr to locate '\n' character ( do not forget to make buffer nul-terminated beforhead)
If you're reading the file in binary mode then '\n' isn't meaningful. You have to convert the computer's format of a new line yourself.
Code:for ( int i = 0; block[i] != '\0'; ++i )
{
// Windows new line conversion
if ( block[i] == 0xD && block[i + 1] == 0xA )
{
putchar( '\n' );
}
else
{
putchar( block[i] );
}
}
Oops.Quote:
Don't you note that your "conversion" code does not skips 0xA char?
Code:for ( int i = 0; block[i] != '\0'; ++i )
{
// Windows new line conversion
if ( block[i] == 0xD && block[i + 1] == 0xA )
{
++i;
putchar( '\n' );
}
else
{
putchar( block[i] );
}
}
Read the comment please. It's an example for Windows because Windows uses two characters to represent a new line. For Unix or any other OS you'd have to change the code because different OS's treat new lines differently.Quote:
Also for Unix formatted txt file this code has no meaning at all
so when somebody nead this conversion - he will just open file in a text mode and will get the conversion for free
If file is opened in binary - it is because no conversion needed, I suppose
That's fine I guess, if 911help can open the file in text mode instead of binary mode. If he can't, you're not really helping because you've dismissed the exact problem he's having by saying "If file is opened in binary - it is because no conversion needed".Quote:
so when somebody nead this conversion - he will just open file in a text mode and will get the conversion for free
If file is opened in binary - it is because no conversion needed, I suppose
He asked how to break buffer on '\n' not how to convert '\n' '\r' pair into '\n'...
I assume from the user's signature that he is on a Linux box, so his files will probably contain only one character encoded line seperator, the '\n' character. Still it is possible that the user might be reading a windows encoded text file.
The fact that the file is read in binary, burdens the programmer with the translation of 'special' characters.
To split the input from his buffer to where newlines appear he could alternatively use strtok. Except of course if replacement of the newline character by the '\0' character is not desirable behavior for the buffer. A traditional strchr call, or even a manual seek with a char * will also be useful, but i would prefer strtok, as every line i parse would be ready to use in whatever meaning i needed.
But binary mode doesn't convert an OS specific new line into '\n', and that's why I posted. Whatever. If he's on Linux it doesn't matter and you're right.Quote:
He asked how to break buffer on '\n' not how to convert '\n' '\r' pair into '\n'...
I guess the obvious realization that breaking on '\n' works equally correctly, whether the line terminator is '\n' or '\r\n', did not occur to you? There's a reason things are made this way.
Old Mac OS 9 and prior was the oddball, using just '\r'. Finally they woke up.