Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   How To Read integers from a text file to a multidimentional array. (http://www.go4expert.com/forums/read-integers-text-file-t23817/)

Programming_Kills 10Nov2010 11:17

How To Read integers from a text file to a multidimentional array.
 
Hi EveryOne.
i am Learnng C hence it is not a part of my course but i am trying to learn it.
i am reading a file which contains integers.
for example.
12345637
32423423
34234233
34234234
43242343
65765767
65858688
87078787

and i want to load the same file into A 8x8 array.
so can anyone plz help me out.
Thanks in Advance

xpi0t0s 10Nov2010 14:42

Re: How To Read integers from a text file to a multidimentional array.
 
Something like this maybe?
Code:

void test33()
{
        int arr[8][8];
        int x=0, y=0;
        FILE *fp;
        if (!fopen_s(&fp,"C:\\_xfr\\numbers.txt","r"))
        {
                while (!feof(fp))
                {
                        int c=fgetc(fp);
                        if (c>='0' && c<='9')
                        {
                                arr[x][y]=c-'0';
                                x++;
                                if (x>7)
                                {
                                        x=0;
                                        y++;
                                        if (y>7)
                                                break;
                                }
                        }
                }
                fclose(fp);
        }
        for (y=0; y<8; y++)
        {
                for (x=0; x<8; x++)
                {
                        printf("%d ",arr[x][y]);
                }
                printf("\n");
        }
}

Output:
1 2 3 4 5 6 3 7
3 2 4 2 3 4 2 3
3 4 2 3 4 2 3 3
3 4 2 3 4 2 3 4
4 3 2 4 2 3 4 3
6 5 7 6 5 7 6 7
6 5 8 5 8 6 8 8
8 7 0 7 8 7 8 7

Programming_Kills 11Nov2010 18:47

Re: How To Read integers from a text file to a multidimentional array.
 
Thank you very MUch xpi0t0s.
May Allah Almighty Bless You.

xpi0t0s 11Nov2010 21:40

Re: How To Read integers from a text file to a multidimentional array.
 
He does, frequently, although I know him as Jesus. xpi0t0s is derived from his name in Greek (christos).


All times are GMT +5.5. The time now is 10:42.