|
Home > Archive > Unix Programming > December 2006 > writing this out into file
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 this out into file
|
|
|
| Ok, as requested, here's a more complete version of what i want to do.
I have a struct with a lot of pointers to char, or char arrays. The
strings that are being assigned to these are variable. I made all the
variables char * or char arrays, even the ints so i can write them out
as ascii (to overcome the fact that fwrite doesn't write normal vars
out in ascii)
Do i need a extra length variable for every string so when reading i
know how much to read? Seems to be a messy method :-). Is there no
other way?
Also, when writing, should i write the '\0' as well ? Because i'm
afraid when writing out to file with for example fwrite the file won't
be ascii because of the '\0' being there terminating every string.
If more information is needed, i can provide it ofcourse.
Thanks for any help.
| |
| Pascal Bourguignon 2006-12-18, 1:20 pm |
| atv <alef@xs4all.nl> writes:
> Ok, as requested, here's a more complete version of what i want to do.
> I have a struct with a lot of pointers to char, or char arrays. The
> strings that are being assigned to these are variable. I made all the
> variables char * or char arrays, even the ints so i can write them out
> as ascii (to overcome the fact that fwrite doesn't write normal vars
> out in ascii)
>
> Do i need a extra length variable for every string so when reading i
> know how much to read? Seems to be a messy method :-). Is there no
> other way?
>
> Also, when writing, should i write the '\0' as well ? Because i'm
> afraid when writing out to file with for example fwrite the file won't
> be ascii because of the '\0' being there terminating every string.
>
> If more information is needed, i can provide it ofcourse.
It depends on the file format you're using.
If you want to write a text file, then you should avoid null bytes and
rather use fprintf than fwrite. The question when writting strings to
a text file, is whether the string will contain newline characters or
not.
If they contain newline characters, you cannot use the newline to
separate the strings (you cannot just put one string per line). So
you need to encode the string in some way, for example, putting it
between double-quote and escaping any newline, escape character and
double-quote inside the string.
fputc("\"",file);
for(i=0;string[i]!='\0';i++){
switch(string[i]){
case '"': fputs("\\"",file); break;
case '\n': fputs("\\\n",file); break;
case '\': fputs("\\\\",file); break;
default: fputc(string[i],file); break;
}
fputc("\"",file);
If they can't contain any newline, then you can just write one string
per line:
fprintf(file,"%s\n",string);
Even in a text file, you can also use a length+data format, but you
have to be careful in what units you give the length: it migth be in
character or in bytes. Better to give it in character, because the
byte count can change when you convert the encoding or the newline
sequence (LF vs. CR+LF).
fprintf(file,"%d\n%s\n",strlen(string),string);
then you first read the length and the newline, and next you read the
indicated number of characters, then and the last newline. One
advantage of this format, is that when you read it you know exactly
what buffer size to allocate (1+length).
If you want to write a binary file, you have to specify the file
format. You can have fixed size records, where each string is stored
in a record of the same size, or variable size record: you need to
store the size of the record before the record, or streams with
separators, that can be the null byte of the string, or any other
character or sequence, but you must ensure that the separator doesn't
appears in the string. There's also the possibility to write the
bytes of the string in sequence, and to write an index giving the
start position and length of each string. The possibilities are
infinite...
--
__Pascal Bourguignon__ http://www.informatimago.com/
The world will now reboot. don't bother saving your artefacts.
| |
|
| On 2006-12-18 15:17:36 +0100, Pascal Bourguignon <pjb@informatimago.com> said:
>
> e
> bytes of the string in sequence, and to write an index giving the
> start position and length of each string. The possibilities are
> infinite...
Thanks Pascal, you have given me again some food for thought.
Kind regards,
atv
|
|
|
|
|