-
Quick Q - Array to File
I'm sure there is likely a simple solution to this, but my brain has turned to mush working on this code.
Code:
while(i > 0)
{
printf("%d\n", array[--i]);
}
if this code will simply display my array....How can I change this to write the array[--i] to a file. I've tried the following code, but get an error message. Could someone please point me in the right direction.
Code:
while(i > 0)
{
fptr = fopen("junk3.txt", "w");
fwrite(array[--i], sizeof(array[i]), 1, fptr);
fclose(fptr);
}
-
Code:
fwrite(&array[--i], ... /* why not write entire array at once? */
fwrite(array, i, 1, ... /* hooray */
-
Put fclose() outside the while loop.
-
-
Here is another idea.
Code:
fptr = fopen("junk3.txt", "w");
if(fptr)
{
while(i > 0)
{
fprintf(fptr, "%d\n", array[--i]);
}
fclose(fptr);
}
Text files have advantages of being "human readable", the data can be viewed/changed easily using a text editor, and they are fairly portable.