filling an array from a file using fscanf...

This is a discussion on filling an array from a file using fscanf... within the C Programming forums, part of the General Programming Boards category; I am trying to fill a 2d array from a text file that contains 54 integers. The files open fine ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Location
    unf
    Posts
    16

    filling an array from a file using fscanf...

    I am trying to fill a 2d array from a text file that contains 54 integers. The files open fine but I am having a problem with a segmentation fault that I get when I try to read the ints into the array. It is probably an error from pointers and vars. Any help would be greatly appreciated and thank you for your help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define TRUE 1
    #define FALSE 0
    #define ROW 5
    #define COL 8
    
    int openFiles(int argc, char *argv[], FILE **pFPIn, FILE **pFPOut);
    void readData(FILE *pFPIn, int arr[][]);
    void calcMeans(int arr[][], double means[]);
    void printData(FILE *fpOut, int arr[][], double means[]);
    
    int main(int argc, char *argv[])
    {
       int arr[ROW][COL];
       int opened;
       FILE *pFPIn, *pFPOut, *fpIn;
    
       opened = openFiles(argc, argv, &pFPIn, &pFPOut);
       readData(pFPIn, arr);
    
       printf("\n\n");
       return FALSE;
    }//end of main
    
    //////////
    //Name: openFiles()
    //Purpose: open external files for input and output
    //////////
    int openFiles(int argc, char *argv[], FILE **pFPIn, FILE **pFPOut)
    {
       int status = FALSE;
       
       if(argc < 3)
       {
          printf("\nToo few arguements...");
          printf("\nTry: ./a.out <input file name> <output file name>");
       }
       else if(argc == 2)
       {
          *pFPIn = fopen(argv[1], "r");
       if(!pFPIn)
       {
          printf("\nCould not open input file");
          status = TRUE;
       }
          *pFPOut = fopen(argv[2], "w");
       if(!pFPOut)
       {
          printf("\nCould not open output file");
          status = TRUE;
       }
       }//end else if
    
       return status;
    }//end of openFiles()
    
    //////////
    //Name: readData()
    //Purpose: reads the data into the array
    //////////
    void readData(FILE *pFPIn, int arr[][COL])
    {
       int i, j;
       int num;
       char spc;
       
       for(i = 0; i < ROW; i++)
       {
          for(j = 0; j < COL; j++)
          {
          fscanf(pFPIn, "%d %c", &arr[i][j], &spc);
          
          //printf("%5d", arr[i][j]);
          }//end inner for
       }//end outer for
    }//end of readData()
    the text file looks like this:

    Code:
    67   98   78   89   99   102   45   78   54 88   90   67   72   
    
    83   100   34   99   87
    76   92   59   61   90   98   56   80   72
    98   67   78   99   102   89   45   54   88 78   99   
    
    87   88   72   90   67   83   100   76   92   
    59   61   90   98   56   80   72

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Code:
    fscanf(pFPIn, "%d %c", &arr[i ][j], &spc);
    I haven't looked into this too closely but I don't think you need to handle the spaces as a special case. The fscanf function should skip leading whitespace when reading data (I believe).

    What are doing with the other 14 values you aren't reading from the file?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Code:
    
      /* in your main() */
       opened = openFiles(argc, argv, &pFPIn, &pFPOut);
       readData(pFPIn, arr);
    
     
      /* in your openFiles() */
       if(argc < 3)
       {
          printf("\nToo few arguements...");
          printf("\nTry: ./a.out <input file name> <output file name>");
       }
       else if(argc == 2)
    (In addition to suggestions from hk_mp5kpdw.)

    Bug number 1:
    In openFiles: you never open the files, since argc is equal to 3. So openFiles returns 0.

    Bug number 2:
    In main() you don't test the value returned from openFIles, so you go ahead and try to read from the files

    Bug number 3:
    In readData() you dont test the returned value of scanf().

    1 leads to 2 (Which is probably where your segfault comes from)

    3 is something that will, somehow, some day, cause you much, much grief (although in itself doesn't cause a segfault). It will someday bite you where you don't want to be bitten. I guarantee it.

    Regards,

    Dave

    Buglet number 4: misspelled "arguments"
    Last edited by Dave Evans; 03-16-2005 at 08:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21