I have a slight problem.
I want to read in data from a text file and store that in a 2D-array. Then I have to print it in another file.
The content of my text document is:
The file is probably not in the correct directory. Since you are creating myhelp2.txt you may want to use your operating systems find functionality to locate myhelp2.txt and insure myhelp1.txt is in the same directory.
I saved my document with the data on the desktop.
Then I compiled and run my program, it said it had opened the file.
So I check the file and there is still an error...
Well the first line looks correct for the data file you posted, you may want to add whitespace between your items and maybe a new line or two. Is there more information in the file in addition to:
I put in a new file with the same exact data then I tried to change int to char. I also changed row= 0 and col=0 to row=1 and col= 1.
Here is my code now
#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\n");
for(row=0; row<3; row++)
for(col=0; col<3; col++) {
fscanf(ifp,"%d",&arr[row][col]);
fprintf(ofp,"%d\n",arr[row][col]);
}
fprintf(stdout,"\n");
fclose(ifp);
fclose(ofp);
getchar();
}
And this is the output:
1
2
3
1
2
3
1
2
3
If you change the for loop starting point to 1 you won't be able to read the entire file correctly. Also just changing the data type without changing the fscanf() and fprintf() specifiers will not work, the specifier must match the data type.
well well well..... this is very unexpected but I found out that if you don't space between your integers in your document, he wil put al kind of random and strange things. And if you do space between your integers, it wil print out perfect.
I don't know why but apparently this is how it works for me. I think this is so stupid..
I use dev cpp to program in, I don't know if that has anything to do with it, but if so than it is time to update dev cpp.