Scanning Data into a structure from a File

This is a discussion on Scanning Data into a structure from a File within the C Programming forums, part of the General Programming Boards category; Can anybody help me understand why this isn't working correctly, I'm attempting to scan in the data from a command ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    Scanning Data into a structure from a File

    Can anybody help me understand why this isn't working correctly, I'm attempting to scan in the data from a command line file. I am storing the data in structures but every time I compile and run, the program never ends, it gets caught in a never ending loop, but I don't know why.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define STR_SIZE 25
    #define ARR_SIZE 10
    
    typedef struct
    {
     char fName[STR_SIZE];
     char lName[STR_SIZE];
    }NAME;
    
    typedef struct
    {
     char month[STR_SIZE];
     int date;
     int year;
    }DATE;
    
    typedef struct
    {
     NAME name;
     int level;
     char ssn;
     DATE birthdate;
    }STUREC;
    
    
    int fillArray(FILE *inf, STUREC arr[]);
    void sortArray(STUREC arr[], int numEls);
    void swap(STUREC *stu1, STUREC *stu2);
    void printArray(STUREC arr[], int numEls);
    
    int main(int argc, char *argv[])
    {
       int numEls;
       STUREC arr[ARR_SIZE];
       FILE *inf;
    
       inf = fopen(argv[1], "r");
       numEls = fillArray(inf, arr);
       printArray(arr, numEls);
       //sortArray(arr, numEls);
       printArray(arr, numEls);
       fclose(inf);
    
       return 0;
    }
    
    
    
    int fillArray(FILE *inf, STUREC arr[])
    {
      int ElsCnt = 0;
      int ef;
    
      printf("\nProgramers Name: \n\n");
      //while (inf != NULL)
        while ((ef= fgetc(inf)) !=EOF)
    
      {
         fscanf(inf, "%s %s %d %s %s %d, %d", arr->name.fName, 
                                              arr->name.lName,
                                              &arr->level, &arr->ssn, 
                                              arr->birthdate.month,
                                              &arr->birthdate.date,
                                              &arr->birthdate.year);
         ElsCnt  ++;
      }
    
      return ElsCnt;
    }
    
    
    void printArray(STUREC arr[], int numEls)
    {
    
      printf("%20s %10s %15s %15s", "Name", "Level", "SSN", "Birthdate\n");
      printf("%20s %10s %15s %15s", "----", "-----", "---", "---------\n");
      /*printf("%10s%10s%10d%15s%10s/%2d/%2d", arr->name.fName,
                                             arr->name.lName,
                                             arr->level, arr->ssn,
                                             arr->birthdate.month,
                                             arr->birthdate.date,
                                             arr->birthdate.year);
     */                                       
    }
    I'm guessing it has to do with the "while (inf !=NULL)" line, But I don't know how to implement eof into c? ...or can you?

    EDIT: Ok, fixed that (I think) (check code)
    but now I'm having trouble storing the elements in an array. I'd assume you just use a for loop?
    Last edited by Tha_Rappa; 12-07-2006 at 03:31 PM.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    &arr stores everything into first element
    use arr++ to move to the next element

    But you also heed an array size in your function to check that you don't get out of bounds

    In your print function - yes you need a for loop
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have many problems. You aren't indexing your array at all. You also are skipping one character each time your loop executes, which I doubt is what you want. Why don't you just use fscanf's return value to control your loop? You don't magically move through an array just because you use its name.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. Help filling a structure with data from a file.
    By System_159 in forum C++ Programming
    Replies: 41
    Last Post: 09-27-2006, 06:39 PM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 10:47 AM

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