Thread: Read numbers from a file and print them in an array

  1. #1
    Registered User
    Join Date
    Nov 2022
    Posts
    3

    Read numbers from a file and print them in an array

    Hi,

    I have to make a program that reads numbers from a file. Then, it has to store them in an array and, finally, it has to print the numbers.

    I'm a beginner and every program I make it fails.

    How would you make this program? It's C programming.

    Thank you so much.

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,128
    Quote Originally Posted by AnthonyBin View Post
    Hi,

    I have to make a program that reads numbers from a file. Then, it has to store them in an array and, finally, it has to print the numbers.

    I'm a beginner and every program I make it fails.

    How would you make this program? It's C programming.

    Thank you so much.
    You will need to show us your first attempt. We can then guide you. Don't try to do it all in one attempt

    Start with just opening and closing the file.
    Then create the array.
    Then read values into the array.
    Then print the values.

    Does the file exist or are you creating the file?
    What is the structure of the file?
    Binary day or numbers as text?
    Are they integer numbers or floating point numbers?
    Are there any restrictions as to what functions to use to input the data?

    Good luck!

  3. #3
    Registered User
    Join Date
    Nov 2022
    Posts
    3

    Reply

    Thank you for replaying.

    The file I'm using exists. It contains integer numbers just like this:
    1
    5
    65
    3

    There are no restrictions but it should be simple.

    Code:
    int main(){
    FILE *data;
        int array[100],i=0,j,k;
    
        data=fopen("data.txt","r");
        if(data==NULL){
            printf("There was a mistake\n");
            exit(1);
        }
    
        do{
             j=fscanf(data,"%d",array[i++]);
        }while(j!=EOF);
        
       for(k=0;k<sizeof(array);k++){
            printf("The value os the array in the position %d is %d\n",k,array[k]);
    }
    
        return 0;
    }
    I think there is no problem when opening the file. I think the mistake is afterwards.

    Thank you so much.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,128
    Please see my changes:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define DIM 100         // Use #defines rather than "Magic Numbers"
    
    int main(){
    
      FILE *data = NULL;    // Always initialize all variables
      int array[DIM] = {0}; // I would recommend putting each on separate lines
      int count = 0;        // Count of data input, instead of i
      int j = 0;
      int k = 0;
    
      data=fopen("data.txt","r");
      if(data==NULL){
        printf("There was a mistake\n");
        exit(1);
      }
    
    /*
      do{
        j=fscanf(data,"%d",&(array[i]));
        i++;
    
      }while(j!=EOF);
    */
      while(1)
      {
        j=fscanf(data,"%d",&(array[count])); // or array + count
        if(j == EOF)
        {
          break;
        }
        count++;
      }
    
    //  for(k=0;k<sizeof(array);k++){ // sizeof(array) != number of elements
    
      for(k=0; k < count; k++){
        printf("The value os the array in the position %d is %d\n", k, array[k]);
      }
    
      return 0;
    }
    Usually avoid do-while, and use while or for loops instead.
    No reason to print out 100 integers to display the data in only 4 elements.
    Use more descriptive names for all variables other than loop variables
    if your compiler can, use for(int k = 0; k < count; k++)
    j could be be more descriptive as well (ret, retval, etc...)
    Note my other comments in the code.
    Last edited by rstanley; 11-10-2022 at 12:18 PM.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,630
    You should also be checking to insure that you don't overflow the array bounds for the read.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,128
    Quote Originally Posted by jimblumberg View Post
    You should also be checking to insure that you don't overflow the array bounds for the read.
    Yes, of course, and the while() loop could be replaced with a for() loop.
    Code:
      for (i = 0; i < DIM; ++i) {
        j=fscanf(data,"%d",&(array[count])); // or array + count
        if(j == EOF)
        {
          break;
        }
        count++;
      }

  7. #7
    Registered User
    Join Date
    Sep 2022
    Posts
    59
    Closing the file might be a good idea as well
    My attempt would probably look like that
    Code:
    #include <stdio.h>
    
    #define DIM 100
    
    
    int main(void)
    {
      FILE *data = fopen("data.txt", "r");
      if(data == NULL)
      {
        printf("There was a mistake\n");
        return 1;
      }
    
    
      int array[DIM] = {0};
      int count = 0;
      for (; count < DIM; ++count)
      {
        if(EOF == fscanf(data, "%d", &(array[count])))
        {
          break;
        }
      }
    
    
      fclose(data);
    
    
      for(int idx = 0; idx < count; ++idx)
      {
        printf("The value os the array in the position %d is %d\n", idx, array[idx]);
      }
    
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Nov 2022
    Posts
    3
    Excuse me, rstanley, but in your first code, why do you use 1 in "while(1)"?

    Thank all of you for being such nice!

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,128
    while(1) created an infinite loop. There must be a test in the code to "break" or end the loop.

    I later realized that the for() loop was a better choice to prevent an overrun of the end of the array.

    You're welcome!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. print the given file with line numbers
    By David995 in forum C Programming
    Replies: 1
    Last Post: 10-17-2021, 02:33 PM
  2. Why array doesn't print correct numbers?
    By Arthuoff in forum C Programming
    Replies: 1
    Last Post: 09-18-2018, 11:03 AM
  3. Replies: 3
    Last Post: 03-13-2013, 07:10 PM
  4. How not to print duplicate numbers in an array?
    By ahmedbatty in forum C Programming
    Replies: 4
    Last Post: 11-16-2011, 01:22 PM
  5. Print numbers within a certain range of an Array
    By omegasli in forum C Programming
    Replies: 4
    Last Post: 04-10-2011, 01:08 AM

Tags for this Thread