#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Read 7 lines at a time from a file (HELP)


    Hey, So I need to make a program that tracks enrolment. So I need to make a struct for courses. Heres where my problem starts, I get a file with several information.. the information is line by line. These are the variables in my struct course that need to be read from a file and saved :
    char title[100], num[100], instructor[100], date[100], start_time[100], end_time[100] , location[100]

    So the file usually has this format that is read in:

    num
    title
    instructor
    date
    start_time
    end_time
    location
    .
    .
    .
    // same thing here repeated

    All i want to figure out is how to read all 7 variables at 1 read in a file so i can assign these variables with strcpy to the struct. Thanks
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int read_n_lines(char**buffer,int number_of_lines,int trailing_dimension_of_buffer,FILE*pf) {
      while (0 < number_of_lines--)
        if (NULL == fgets(*buffer++, trailing_dimension_of_buffer, pf))
          exit(EXIT_FAILURE);
    }
    Make sure you can explain it to your teaching assistant.
    Untested.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int read_n_lines(char**buffer,int number_of_lines,int trailing_dimension_of_buffer,FILE*pf) {
      while (0 < number_of_lines--)
        if (NULL == fgets(*buffer++, trailing_dimension_of_buffer, pf))
          exit(EXIT_FAILURE);
    }
    Make sure you can explain it to your teaching assistant.
    Untested.
    Hi, thanks for the input , i'll test it out. What would be the "trailing_dimension_of_buffer pararmeter? Thanks
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,905
    Rep Power
    481
    Please search the forum for key phrases like
    "not homework"
    and
    "show effort"

    With google search use

    site:forums.devshed.com key words
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo