Thread: Scanning Data into a structure from a File

  1. #1
    Registered User
    Join Date
    Nov 2006

    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.

    #include <stdio.h>
    #include <string.h>
    #define STR_SIZE 25
    #define ARR_SIZE 10
    typedef struct
     char fName[STR_SIZE];
     char lName[STR_SIZE];
    typedef struct
     char month[STR_SIZE];
     int date;
     int year;
    typedef struct
     NAME name;
     int level;
     char ssn;
     DATE birthdate;
    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);
       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->level, &arr->ssn, 
         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->level, arr->ssn,
    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 04:31 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    &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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.

    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, 05: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, 11:47 AM