Would someone please tell me the best way to write values to a file that I have opened as "write"? The values I need to write are both integer and character strings. Thank you!
Janice
Would someone please tell me the best way to write values to a file that I have opened as "write"? The values I need to write are both integer and character strings. Thank you!
Janice
It depends on how you plan to read them back. The formatting of a file is very important, as is the portability. These factors determine what functions you use to write to the file and in what order.
-Prelude
My best code is written with the delete key.
Thank you for your reply Prelude. I have the file opened as write only (no append). There are 5 variables whose values I want written to this file. Four of them are declared as integers and the fifth one is declared as a character array of 6. I hope what I just told you helps. Janice
Assuming you just want to write them to the file like that and the character array is nul terminated then this should work fine:
fprintf ( file, "%d %d %d %d %s", a, b, c, d, array );
If the character array is not nul terminated then you will have to specify how many characters to write and make sure that there are no garbage values in unused elements:
fprintf ( file, "%d %d %d %d ", a, b, c, d );
for ( i = 0; i < 6; i++ ) putc ( array[i], file );
-Prelude
My best code is written with the delete key.
Thanks Prelude. I will try this.
Janice