Home > Archive > Unix Programming > December 2006 > writing out floats ??? and strange display sign after date
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
writing out floats ??? and strange display sign after date
|
|
|
| Hi,
I was hoping maybe someone could enlighten me about a few things.
First, i'm trying to write a float value to file with fwrite. When i do
a cat on the file in question, i only get ??? but it does seem to read
the value ok.
This is the code i use to save:
while(tmp) {
fwrite(tmp->timestamp,sizeof(tmp->timestamp),1,fp);
fwrite(tmp->hostname,sizeof(tmp->hostname),1,fp);
fwrite(tmp->event,sizeof(tmp->event),1,fp);
fwrite(&tmp->r,sizeof(tmp->r),1,fp);
fwrite(&tmp->g,sizeof(tmp->g),1,fp);
fwrite(&tmp->b,sizeof(tmp->b),1,fp);
fwrite(&tmp->a,sizeof(tmp->a),1,fp);
tmp=tmp->next;}
And the other thing is, when i write a timestamp, it seems that i get a
lot of whitespace after the timestamp itself. It also reads in
correctly. Is this because of strncpy filling up everything with '\0' ?
I think so. Now this wouldn't be such a big problem, but i also get
this strange sign right after the data if i display it, sort of like a
little v and t on top of each other, right after the year being
displayed.
This is my savefile:
cat events.db
Sat Dec 16 12:45:36 2006
localhostInterface 5 eth5 [10.0.0.138]/[total of 6 interfaces]
down???Sat Dec 16 12:45:37 2006
localhostInterface 5 eth5 [127.0.0.1]/[total of 6 interfaces]
down???Sat Dec 16 12:45:38 2006
As you can see the timestamp takes a lot of whitespace, and the floats
write out as ???.
thanks for any help.
| |
| Stefaan A Eeckels 2006-12-16, 8:05 am |
| On Sat, 16 Dec 2006 13:00:22 +0100
atv <alef@xs4all.nl> wrote:
> I was hoping maybe someone could enlighten me about a few things.
> First, i'm trying to write a float value to file with fwrite. When i
> do a cat on the file in question, i only get ??? but it does seem to
> read the value ok.
Obviously, because when you write a float to a file you store the
binary value, not a text representation which you would need for cat to
show something readable.
All your problems stem from the fact that you expect the fwrite() call
to convert your variables (be they floats or timestamps) to a text
representation. It doesn't.
If you want to be able to cat the file you're writing, you'll need to
use fprintf() to write it, and fscanf() to read it back. If you don't
want to cat the file, just be glad you can read what you wrote.
--
Stefaan A Eeckels
--
People don't ask for facts in making up their minds. They would rather
have one good soul-satisfying emotion than a dozen facts.
--Leavitt
| |
|
| On 2006-12-16 13:43:03 +0100, Stefaan A Eeckels <hoendech@ecc.lu> said:
> On Sat, 16 Dec 2006 13:00:22 +0100
> atv <alef@xs4all.nl> wrote:
>
>
> Obviously, because when you write a float to a file you store the
> binary value, not a text representation which you would need for cat to
> show something readable.
>
> All your problems stem from the fact that you expect the fwrite() call
> to convert your variables (be they floats or timestamps) to a text
> representation. It doesn't.
> If you want to be able to cat the file you're writing, you'll need to
> use fprintf() to write it, and fscanf() to read it back. If you don't
> want to cat the file, just be glad you can read what you wrote.
> --
> Stefaan A Eeckels
Ok, well i want to be portable so i guess fwrite/fread is not adequate
then. I'll guess i switchover to fprintf then. One more question, i
seem to have fixed the strange sign by terminating the buffer i gave
strncpy, which is a bit weird because strncpy is supposed to fill
everything up with a '\0'. I did it with buffer[strlen(buffer)]='\0';
Is that the correct way? Strlen uses '\0' as a criteria to determine
the string length as well right? So that would be a bit redundant.
Funnily enough, it does seem to work..
| |
|
| On 2006-12-16 13:43:03 +0100, Stefaan A Eeckels <hoendech@ecc.lu> said:
> On Sat, 16 Dec 2006 13:00:22 +0100
> atv <alef@xs4all.nl> wrote:
>
>
> Obviously, because when you write a float to a file you store the
> binary value, not a text representation which you would need for cat to
> show something readable.
>
> All your problems stem from the fact that you expect the fwrite() call
> to convert your variables (be they floats or timestamps) to a text
> representation. It doesn't.
> If you want to be able to cat the file you're writing, you'll need to
> use fprintf() to write it, and fscanf() to read it back. If you don't
> want to cat the file, just be glad you can read what you wrote.
> --
> Stefaan A Eeckels
btw, i thought fwrite/fread could do both, binary and text. I can also
write out poitners as text, why not normal variables ?
| |
| Stefaan A Eeckels 2006-12-16, 8:05 am |
| On Sat, 16 Dec 2006 13:59:17 +0100
atv <alef@xs4all.nl> wrote:
> On 2006-12-16 13:43:03 +0100, Stefaan A Eeckels <hoendech@ecc.lu>
> said:
>
>
> btw, i thought fwrite/fread could do both, binary and text. I can
> also write out poitners as text, why not normal variables ?
Because these calls do not convert data, they write a number of bytes
stored at a particular location. You don't write "pointers as text", you
write the contents of the buffer that is pointed to, which happens to be
text.
--
Stefaan A Eeckels
--
"The most common of all follies is to believe passionately in
the palpably not true. It is the chief occupation of mankind."
--H. L. Mencken
| |
| Stefaan A Eeckels 2006-12-16, 8:05 am |
| On Sat, 16 Dec 2006 13:56:52 +0100
atv <alef@xs4all.nl> wrote:
> On 2006-12-16 13:43:03 +0100, Stefaan A Eeckels <hoendech@ecc.lu>
> said:
>
>
> Ok, well i want to be portable so i guess fwrite/fread is not
> adequate then. I'll guess i switchover to fprintf then. One more
> question, i seem to have fixed the strange sign by terminating the
> buffer i gave strncpy, which is a bit weird because strncpy is
> supposed to fill everything up with a '\0'. I did it with buffer
> [strlen(buffer)]='\0';
strncpy does _not_ fill everything with '\0'. Look at the man page:
| The strncpy() function copies exactly n
| bytes, truncating s2 or adding null characters to s1 if
| necessary. The result will not be null-terminated if the
| length of s2 is n or more.
strncpy adds null characters to the destination _only_ if the source
isn't long enough. If the source string is longer than the number of
characters to copy _no_ null character is added. So you yourself
should add a null character to make sure the string is properly
terminated.
--
Stefaan A Eeckels
--
Governments are like babies: digestive tracts with a big appetite at
one end and no sense of responsibility at the other. The better run
ones from time to time get clean diapers...
| |
|
| On 2006-12-16 14:43:37 +0100, Stefaan A Eeckels <hoendech@ecc.lu> said:
> On Sat, 16 Dec 2006 13:56:52 +0100
>
> strncpy does _not_ fill everything with '\0'. Look at the man page:
>
> | The strncpy() function copies exactly n
> | bytes, truncating s2 or adding null characters to s1 if
> | necessary. The result will not be null-terminated if the
> | length of s2 is n or more.
> strncpy adds null characters to the destination _only_ if the source
> isn't long enough. If the source string is longer than the number of
> characters to copy _no_ null character is added. So you yourself
> should add a null character to make sure the string is properly
> terminated.
> --
> Stefaan A Eeckels
This is what my man page says (mac os x):
The strncpy() function copies at most len characters from src into dst.
If src is less than len characters long, the remainder of dst is filled
with `\0' characters. Otherwise, dst is not terminated.
So the way i understand it, is that when i give it a size in the 3param
that is bigger then that which i copy into dest, it will append for the
remaining space with '\0';. Ofcourse, if it is not bigger, i would be
in trouble so it would be a good idea to terminate it anyway :-)
| |
| Stefaan A Eeckels 2006-12-16, 7:06 pm |
| On Sat, 16 Dec 2006 15:02:30 +0100
atv <alef@xs4all.nl> wrote:
> Ofcourse, if it is not bigger, i would be
> in trouble so it would be a good idea to terminate it anyway :-)
You got it.
--
Stefaan A Eeckels
--
"We have gone from a world of concentrated knowledge and wisdom to one
of distributed ignorance. And we know and understand less while being
increasingly capable." Prof. Peter Cochrane, formerly of BT Labs
(With thanks to Brian Hamilton Kelly)
|
|
|
|
|