First off, this is my first c program and i am by no means a programmer so ignore me if i don't meet your standard and let those willing to show me how it is done respond.
Problem Statement:
I am trying to use "strtok" to tokenize the strings in my file to screen.
Say i have a file "samplefile" which contains three lines:
one two three four five six seven
one two three four five six seven
one two three four five six seven
What i am trying to accomplish eventually is to read the strings i specify in the array (it is a rather long problem but i want to write the code in increments so i understand what each line of code does and can solve similar problems in the future), right now i want my code to just read the file and spit out the tokenized result but my code only prints the first string on each line:
My Output:
one
one
one
Desired output
one two three four five six seven
one two three four five six seven
one two three four five six seven
I would appreciate any help.
Thanks in advance.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
main()
{
char line [120];
FILE *fp;
fp=fopen("samplefile", "r");
if (fp == NULL) perror ("error opening file");
else
{
fgets (line, sizeof (line), fp);
while (fgets(line, sizeof (line),fp))
{
char* t=strtok(line," ");
if (t != NULL);
{
printf("%s\n",line);
}
}
}
fclose(fp);
return(0);
}