March 25th, 2013, 06:08 AM
-
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
March 25th, 2013, 09:15 AM
-
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!
March 25th, 2013, 09:19 AM
-
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
March 25th, 2013, 09:35 AM
-
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!