Hi everyone.
I'm making a program that uses the functions fseek(), fprintf() and fscanf(). It doesn't work when I recover the data I just stored. here's the code:
#include <stdio.h>
#include <stdlib.h>
void WriteData(), ReadData();
FILE *fp;
int Data[10];
long int i;
int main (void) {
WriteData();
ReadData();
}
void WriteData() {
/* I open the file here with fp = fopen()... no problem */
/* I don't use register 0, so I put a cero in it */
fseek(fp, 0, SEEK_SET); /* locate in register cero */
fprintf(fp, "%d", 0); /* write a cero */
/* here I fill the array Data[] with int values, no problem */
.
.
.
/* Write the data to the file starting with register one, and end with number ten */
for (i=1; i<10; i++) {
fseek(fp, i, SEEK_SET); /* point to register i in the file */
fprintf(fp, "%d", Data[i]); /* write the data */
}
fclose(fp);
}
void ReadData() {
int D;
/* here I open the file as same as above, and get fp */
/* here I read the data as follows */
for (i=1; i <10; i++) {
fseek(fp, i, SEEK_SET); /* Points to the register i in the file */
fscanf(fp, "%d", &D); /* reads the data */
Data[i] = D;
}
}
End of story.....
When I output to the screen the array Data[], it is really a mess, because i get negative and very big numbers.
Would you help me on this? anyone?
Thanks in advance.
Gustaff.