I am reading from a file in c and printing it to screen. I am using fgets function to read line by line and separating strings in the line.
int i;
int j = 0;
int ctr = 0;
while (!feof(fp)){
fgets(line, 256, fp);
for(i=0;i<=(strlen(line));i++){
if(line[i]==' '|| line[i]=='\0'){
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else{
newString[ctr][j]=line[i];
j++;
}
}
//printf("the data line by line is: %s\n", line);
}
printf("\n Strings or words after split by space are :\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
Here is the content of the file that I am reading from:
3
9 3 34 4 12 5 2
6 3 2 7 1
256 3 5 7 8 9 1
This is my output:
Strings or words after split by space are :
3
9
3
34
4
12
5
2
6
3
2
7
1
256
3
5
7
8
9
1
256
3
5
7
8
9
1
However it prints the last line twice, and I couldnt figure out why. Please tell me how to remove the extra one. Thanks in advance...