read data from a textfile and pass it into 2D-array

Hello guys,

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:
1 2 3
1 2 3
1 2 3


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
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int col;
    int row;
    int arr[3][3];
    
    FILE *ifp,*ofp;
    
    ifp=fopen("myhelp.txt","r");
    ofp=fopen("myhelp2.txt","w");
    
   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);
                       

}


This is the output:
-1540223532-21977063642
19771312581055719210560384
268677619771307810


All random numbers...
I have to use fscanf....
Help please,

Dutchman
Last edited on
It looks like your file didn't open correctly. You should always check that your file opened correctly, and if it doesn't do something about it.

The documentation for fopen can be found here: http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html".
Last edited on
Well I changed 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
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int col;
    int row;
    int arr[3][3];
    
    FILE *ifp,*ofp;
    
    ifp=fopen("myhelp1.txt","r");
    ofp=fopen("myhelp2.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();    

}


The output on the screen is

Error


But what I am doing wrong then?
Last edited on
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.

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
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int col;
    int row;
    int arr[3][3];
    
    FILE *ifp,*ofp;
    
    ifp=fopen("myhelp1.txt","r");
    ofp=fopen("myhelp2.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();    

}

Nou werkt ie wel :) je was vergeten rij en kol te veranderen naar row en col. En let op dat myhelp1.txt ook echt door de .exe gevonden kan worden.
Last edited on
@jlb I have checked, but the files are in the same directory and I am still getting the error on my screen.

@Fransje Jaa ik zag het, ik heb het toen snel aangepast. Maar ik krijg nog steeds de error op mijn scherm en de bestanden staan op de goede plaats....
vaag, bij mij werkte het wel.
Ik weet in ieder geval dat mijn code goed is en dat het ergens anders aan ligt...
Toch bedankt!
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...
123123123
19771312581193344811936640
268677619771307810


How?

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:
1 2 3
1 2 3
1 2 3
Last edited on
I did that but it didn't worked. But if I replace row=o for row=1 and col=0 to col= 1, I get almost a good output.

Here it is:
123123
1230
Try using the zero based loops and print a new line in your printf() that is in your loop.
1
2
3
4
5
 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]);
Well it did some of it good but stil there are random numbers...

output:
123
123
123

1977131258
3610376
3613568

2686776
1977130781
0
Are you sure your input file doesn't contain any other information? Maybe you should create a new file with the correct information to be sure.
unfortunately I did this many times but it won't work...
Please post your current code.

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
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
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int col;
    int row;
    char 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=1; row<3; row++)
              for(col=1; col<3; col++){
                          fscanf(ifp,"%d",&arr[row][col]);
                          
                          fprintf(ofp,"%d\n",arr[row][col]);
                         }
                          fprintf(ofp,"\n");
                          
    
    fclose(ifp);
    fclose(ofp);
                  
    getchar();    

}


My output is:

123
123
123
0
The following program works for me with the following input
123
123
123
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
#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.

Thanks a lot, I appreciate your sympathy.

P.S.

this 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
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int col;
    int row;
    int arr[10][10];
    
    FILE *ifp,*ofp;
    
    ifp=fopen("myhelp.txt","r");
    ofp=fopen("myhelp2.txt","w");
    
    if(ifp==NULL)
    printf("error");
    
    else
    printf("File opened");
    
   for(row=0; row<10; row++){
              for(col=0; col<10; col++){
                          fscanf(ifp,"%d",&arr[row][col]);
                          
                          fprintf(ofp,"%d",arr[row][col]);
                         }
                          fprintf(ofp,"\n");
                          }
    
    fclose(ifp);
    fclose(ofp);
                  
    getchar();    

}
Without the spaces between the numbers you only have three numbers in your file, not the 9 you are trying to retrieve.

Topic archived. No new replies allowed.