Siaw
2011-10-17 06:08:41 UTC
Write a program that reads the input from the text file, number.data, and then writes these data to a new file named "output.txt". Your program must apply array and structure in a text files.
_______________________________________________________________________________
Note that the number.data files include below information...(I already type in those information into a notepad and save in .data)
1 Ng 4.4
2 Lee 4.5
3 Lau 6.6
_______________________________________________________________________________
My coding so far...
#include
#include
#include
struct PayRecord {
long id;
char name[20];
double rate;
};
int main(void) {
FILE *fpin, *fpout;
int i=0, j;
char ch;
struct PayRecord pr[10] = {0};
fpin = fopen("number.dat","r");
fpout = fopen("numberout.txt","w");
if((!fpin) || (!fpout))
printf("Cannot open number.data.\n");
else {
while(fscanf(fpin,"%ld", &pr[i].id)==1) {
fscanf(fpin,"%s %lf", &pr[i].name, &pr[i].rate);
i++;
}
for(j=0; j printf("%d", pr[j].id);
printf("%s", pr[j].name);
printf("%lf\n", pr[j].rate);
}
while((ch = getc(fpin)) != EOF)
fputc(ch,fpout);
fclose(fpout);
fclose(fpin);
}
return 0;
}
___________________________________________________________________________
I successful read this number.data and change them into structure form and array form...but now I can't write them into a new text files? Any help? Very thanks.