Vic <ve**********@gmail.comwrites:
I have a c program which writes mac address entries onto a text file.
Text file when opened in vim looks like this
index mac
1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01)
2 ^@^@^Q^@^@^B ascii of (00.00.11.00.00.02)
. ........
Likewise
I read the mac entries from the text file using a perl script and do
some processing
The problem occurs when i have a mac entry with last octet 0x0A, the
entry looks like this
idx ^@^@^Q^@^@ ascii of (00.00.11.00.00.0A)
This happens because ascii for 0x0A ('^J') is line feed for Unix text
editors. Hence the mac read using perl script is incorrect.
Can i get a solution for this so as to
1) Avoid a new line in text file
2) Get correct value(0x0A) read in the perl script
Writing non-printable characters to a text file is hazardous, as
you've seen.
If you read a file in text mode in a C program, using, say, fgets(),
and some of the lines contain null characters, you'll get the data but
it will appear to be truncated whenever you do anything that treats it
as a string. <OT>Perl doesn't use '\0' as a string terminator, so
this probably isn't an issue for what you're doing.</OT But, as
you've seen, whenever you write binary data that happens to match the
representation for new-line, you'll get a genuine new-line in the
file.
You have (at least) two choices. You can use a binary file, which
means you'll have to open it in binary mode (fopen("name", "wb")) in
your C program, *and* you'll have to rigorously define your output
format (e.g., you'll need an unambiguous way of determining the end of
each record, either by a marker, by a byte count, or something else).
Or you can use text, which means you'll need some scheme for encoding
non-printable characters as printable characters. The
"quoted-printable" format is one possibility. It's likely that you
can get away with writing *some* non-printable characters to a text
file, but it's easier and safer to encode everything in printable
form.
<OT>Perl is cabable of dealing with either kind of input file.</OT>
--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"