Thread: reading content of .txt file int oa 2d array

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    10

    reading content of .txt file int oa 2d array

    Hello everyone,

    I have a 'simple' and quick question. I want to read .txt file and copy the content into a 2d array for further processing in my program.

    Let's say i have a 5 by 5 integer array saved in .txt format.

    Could you please send me a simple example/template which could solve my problem?


    Thanks,

    Joealem.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Please check the FAQ for examples of code.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Thanks Dino for quick reply. I search my question in FAQ but i cant find a specific answer to my problem. Could you please write a simple example? or send me the link of the FAQ or any other info.

    Thanks again,

    Joealem

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We're not here to do everything for you. Post your attempt, what you're stuck on, etc., and we'll point you in the right direction. I also find it extremely hard to believe that there are zero search results on this forum for people working with 2D arrays and reading from files.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    
    int main()
    {
    	int i,j,it=7,nz=5,nx=5;
    	
    	int P[nz][nx];
    	int x[nz][nx];
    	
    	
    	char filename[20];
    	
    	sprintf(filename,"SF_%d.txt", it);
    	FILE *file;
    	file = fopen(filename, "w" );
    	
    	for (i=0; i<nz; i++) 
    	{
    		for (j=0; j<nx; j++) 
    		{
    			P[i][j]=i*i;
    		}
    	}
    	
    	for(i=0;i<nz;i++)
    	{
    		for(j=0;j<nx;j++)
    		{
    			fprintf(file,"%d\t",P[i][j]);
    		}
    		fprintf(file,"\n");
    	}
    	
    	FILE *hFile;
    	char filename2[20];
    	sprintf(filename2,"SF_%d.txt",it);
    	hFile = fopen("SF_7.txt", "r");
    	
    	
    		for(i=0;i<nz;i++)  
    		{
    			for(j = 0; j < nx; j++)  
    			{
    				fscanf(hFile,"%d\t", &x[i][j]);
    			}
    		}
    	
    
    		for (i=0; i<nz; i++) 
    		{
    		for (j=0; j<nx; j++) 
    		{
    		printf("%d\t",x[i][j]);
    		}
    		printf("\n");
    		}
    	return 0;
    	
    }
    Out put: SF_7.txt and

    0 8 -1881015350 -1881017180 -1073743808
    3 48 44 1287 0
    0 1 -1880731168 3260 -1073743848
    3 -1881144184 -1880955984 -1073743736 -1881029853
    -1073743808 3 48 -1073743792 1287


    which is 5 by 5 matrix displayed after reading the file SF_7.txt. I don't understand why i am getting this result. The expected result is

    0 0 0 0 0
    1 1 1 1 1
    4 4 4 4 4
    9 9 9 9 9
    16 16 16 16 16

    Please give me your comments.

    Thanks,

    Joealem.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Please forward me some comments....
    thanks,

    Joealem

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    FILE *hFile;
    	char filename2[20];
    	sprintf(filename2,"SF_%d.txt",it);
    	hFile = fopen("SF_7.txt", "r");
    Why do you have this block in there twice? Looks like you got a bit too excited with the copy-paste there.

    I'm not sure why you aren't just reusing the name that you've already set up earlier. Anyway, you never fclose your file after writing to it, before you try opening it again to read it.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Dec 2010
    Posts
    10
    Quote Originally Posted by quzah View Post
    Code:
    FILE *hFile;
    	char filename2[20];
    	sprintf(filename2,"SF_%d.txt",it);
    	hFile = fopen("SF_7.txt", "r");
    Why do you have this block in there twice? Looks like you got a bit too excited with the copy-paste there.

    I'm not sure why you aren't just reusing the name that you've already set up earlier. Anyway, you never fclose your file after writing to it, before you try opening it again to read it.


    Quzah.

    Code:
    .......
     fclose(file)
    ......
    It works. Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting Issue?
    By matthewlane in forum C Programming
    Replies: 1
    Last Post: 11-15-2010, 05:55 PM
  2. i need help with this question:
    By marcuspax in forum C Programming
    Replies: 16
    Last Post: 08-16-2010, 08:12 AM
  3. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  4. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  5. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM