Read data from a file to another using fscanf and pass it into 2D-array
Oct 25, 2012 at 6:58pm UTC
Hellp readers,
I have a slight problem...
I want to read data from a textfile to another by using fscanf.
The problem is that the output in the document is wrong.
I have saved the 'input.txt' document on the desktop to make sure that the documents are in the same directory.
Please help
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <stdio.h>
#include <stdlib.h>
int main()
{
int col;
int row;
int arr[3][3];
FILE *ifp,*ofp;
ifp=fopen("input.txt" ,"r" );
ofp=fopen("output.txt" ,"w" );
if (ifp==NULL)
printf("error" );
else
printf("File opened" );
for (row=0; row<3; row++){
for (col=0; col<3; col++){
fscanf(ifp,"%d" ,&arr[row][col]);
fprintf(ofp,"%d" ,arr[row][col]);
}
fprintf(ofp,"\n" );
}
fclose(ifp);
fclose(ofp);
getchar();
}
output on my screen
the output in my textdocument:
1231231231977131258
1977131258393805639412482686776
2686776197713078100
02686832164156
Last edited on Oct 25, 2012 at 7:54pm UTC
Topic archived. No new replies allowed.