filling numbers from a file to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pallavi27
    New Member
    • Mar 2007
    • 14

    filling numbers from a file to an array

    hii,
    i am working in c and my problem is that i have created a file which contains numbers,i want to store these numbers in an array.,please guide me how to go about it this is the part of code that i have written ,it is showing hundred numbers but all values are same and it shows 277 866 100 times.

    while((ch=fgetc (fp)!=NULL)
    {
    for(i=0;i<10;i+ +)
    for(j=0;j<10;j+ +)
    {
    image[i][j]=ch;
    printf("%d %d",image[i][j]);
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    put the read in the loops?
    Code:
    {
    for(i=0;i<10;i++)
    for(j=0;j<10;j++)
    {
    ch=fgetc(fp);
    image[i][j]=ch;
    printf("%d %d",image[i][j]);
    }
    fegtc() reads characters, should you use
    Code:
    scanf("%d", image[i][j]);

    Comment

    • pallavi27
      New Member
      • Mar 2007
      • 14

      #3
      Originally posted by horace1
      put the read in the loops?
      Code:
      {
      for(i=0;i<10;i++)
      for(j=0;j<10;j++)
      {
      ch=fgetc(fp);
      image[i][j]=ch;
      printf("%d %d",image[i][j]);
      }
      fegtc() reads characters, should you use
      Code:
      scanf("%d", image[i][j]);

      sorry this didn't worked if possible can you give any other solution to this

      Comment

      • pallavi27
        New Member
        • Mar 2007
        • 14

        #4
        how to store numbers in a file to an array

        i am working in c and i have created a file which contains 100 numbers.i want to store these numbers in an array like a[10][10].how to do this.please give the code to do tjis.i have displayed the numbers on the screen but after that i want to store the numbers in an array.please help its urgent.

        Comment

        • pallavi27
          New Member
          • Mar 2007
          • 14

          #5
          how to store numbers in a file to an array

          i am working in c and i have created a file which contains 100 numbers.i want to store these numbers in an array like a[10][10].how to do this.please give the code to do tjis.i have displayed the numbers on the screen but after that i want to store the numbers in an array.please help its urgent.
          the code is as follows:
          #include<stdio. h>
          #include<conio. h>
          main()
          {
          FILE *fp;
          int i,,j,a[10][10];
          char ch;
          fp=fopen("abb.t xt","r");
          if(fp==NULL)
          {
          printf("Cannot open file");
          exit();
          }
          while((ch=fgetc (fp))!=EOF)
          {
          for(i=0;i<10;i+ +)
          for(j=0;j<10;j+ +)
          {
          ch=fgetc(fp);
          image[i][j]=ch;
          printf("%d %d",image[i][j]);
          }
          }
          fclose(fp);
          }
          :

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by pallavi27
            i am working in c and i have created a file which contains 100 numbers.i want to store these numbers in an array like a[10][10].how to do this.please give the code to do tjis.i have displayed the numbers on the screen but after that i want to store the numbers in an array.please help its urgent.
            the code is as follows:
            #include<stdio. h>
            #include<conio. h>
            main()
            {
            FILE *fp;
            int i,,j,a[10][10];
            char ch;
            fp=fopen("abb.t xt","r");
            if(fp==NULL)
            {
            printf("Cannot open file");
            exit();
            }
            while((ch=fgetc (fp))!=EOF)
            {
            for(i=0;i<10;i+ +)
            for(j=0;j<10;j+ +)
            {
            ch=fgetc(fp);
            image[i][j]=ch;
            printf("%d %d",image[i][j]);
            }
            }
            fclose(fp);
            }
            :
            1.) Do not double post
            2.)Post code using code tags
            3.)Haven't you already put the values in the array called image?

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Why do you need the while loop? It is supposed to execute until the end of file, but it works by reading in a character - then, in your loop, you read in a new character, meaning the first is lost. You should just have the for...loops, no while loop.

              Also, in your printf call, you use %d twice to indicate you are printing two values, but you only include a[i][j] to be printed - should you only have one %d?

              Comment

              • gpraghuram
                Recognized Expert Top Contributor
                • Mar 2007
                • 1275

                #8
                HI,
                Can you provide me a sample of your input file...
                i can give a try to help you with code.
                Thanks
                Raghuram

                Comment

                • pallavi27
                  New Member
                  • Mar 2007
                  • 14

                  #9
                  Originally posted by gpraghuram
                  HI,
                  Can you provide me a sample of your input file...
                  i can give a try to help you with code.
                  Thanks
                  Raghuram
                  HII.
                  my text file contains numbers every row having 10 numbers.
                  as given below


                  3 3 4 5 6 6 .3
                  3 3 3 3 6 3..3
                  2
                  2 .............
                  2 3 4 5 6 7 3

                  Comment

                  • gpraghuram
                    Recognized Expert Top Contributor
                    • Mar 2007
                    • 1275

                    #10
                    Hi,
                    The problem with your code was reading the integer with fgetc
                    try this one...
                    Code:
                    #include<stdio.h>
                    
                    int main()
                    {
                    	int read_arr[10][10];
                    	char ch;
                    	int i=0,j=0;
                    	char read_array[200];
                    	
                    	FILE *fptr=fopen("ip_3.txt","r");
                    	if(fptr != NULL)
                    	{
                    		while(!feof(fptr))
                    		{
                    			for(j=0;j<=10;j++)
                    			{
                    				fscanf(fptr,"%d ",&(read_arr[i][j]));
                    				printf("%d ",i,j,read_arr[i][j]);
                    			}
                    			printf("\n");
                    			i++;
                    		}
                    		fclose(fptr);
                    	}
                    }
                    Thanks
                    Raghuram

                    Comment

                    • pallavi27
                      New Member
                      • Mar 2007
                      • 14

                      #11
                      Originally posted by gpraghuram
                      Hi,
                      The problem with your code was reading the integer with fgetc
                      try this one...
                      Code:
                      #include<stdio.h>
                      
                      int main()
                      {
                      	int read_arr[10][10];
                      	char ch;
                      	int i=0,j=0;
                      	char read_array[200];
                      	
                      	FILE *fptr=fopen("ip_3.txt","r");
                      	if(fptr != NULL)
                      	{
                      		while(!feof(fptr))
                      		{
                      			for(j=0;j<=10;j++)
                      			{
                      				fscanf(fptr,"%d ",&(read_arr[i][j]));
                      				printf("%d ",i,j,read_arr[i][j]);
                      			}
                      			printf("\n");
                      			i++;
                      		}
                      		fclose(fptr);
                      	}
                      }
                      Thanks
                      Raghuram
                      thanks for your program but
                      sorry this too didn't work.
                      your program shows the output :
                      0000000000
                      1111111111
                      2222222222
                      3333333333
                      4444444444
                      5555555555
                      6666666666
                      7777777777
                      8888888888
                      9999999999
                      but i want that it should display the numbers that i have saved in a notepad file.
                      and also i didn't get why you initialised the same array twice with different datatypes.it is giving error.array initialised more than once..

                      Comment

                      • Ganon11
                        Recognized Expert Specialist
                        • Oct 2006
                        • 3651

                        #12
                        Originally posted by pallavi27
                        thanks for your program but
                        sorry this too didn't work.
                        This is why you shouldn't copy & paste someone else's code without checking it yourself first. There are several things wrong with this code. For one, the for...loop starts j at 0 and has an end condition of j <= 10, meaning there will be a total of 11 executions. This will make you run out of numbers and also read data past the bounds of read_arr. Also, the printf call gives room for one output, but tries to output i, j, and read_arr[i][j].

                        Comment

                        • gpraghuram
                          Recognized Expert Top Contributor
                          • Mar 2007
                          • 1275

                          #13
                          Hi,
                          The problem was the printf statement
                          printf("%d ",i,j,read_ arr[i][j]);

                          Change this to
                          printf("%d ",read_arr[i][j]);

                          Thanks
                          Raghuram

                          Comment

                          Working...