hi all,

I'm trying to read a file containing this:

Class Time
Physics 15:30
Calculus 9:00
Biology 14:30
Chemistry 11:30

what i want to do is read the file and tokenize it and put the class names and class time in different array. I also want to ignore the first two line but i dont know how to do that. I'm copying each line that's read into a new array, then I'm tokenizing it to separate the name and time.This is what i have so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ( void )

FILE *inp = NULL;
char classes[200];
inp = fopen("schedule.txt", "r");

char className[6];
char classTime[6];
char *token;
int i;
while (fgets(classes, 6, inp) != NULL){
if (strlen(classes) > 1){
token = strtok(aLine," ");
strcpy(className, token);
token = strtok(NULL, " ");
strcpy(classTime, token);
printf("%s", className);
printf("%s", classTime); fclose(inp); return 0; } }`
for the output im getting a segmentation fault error. I'm still pretty new to C and i haven't figured out how to read the file starting from the third line. any help would be greatly apreaciated.

Edited by Reverend Jim: Moved to programming forum

1 Year
Discussion Span
Last Post by JamesCherrill
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.