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.

Re: Reading a file using fgets and tokenizing it using strtok in C 80 80

i haven't figured out how to read the file starting from the third line

Just read and discard the first two lines.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.