I have a file named email.txt, which contains some text. I am trying to split the given string containing spaces into multiple strings. The program I wrote does not split the words into separate lines. It gives me the output of the last word of the file. Here is my code
#include <stdio.h>
#include <string.h>
#define MAXCHAR 1000
int main()
{
FILE *fp2;
char str2[MAXCHAR];
int line = 0;
char delim[] = " ";
int init_size = strlen(str2);
fp2 = fopen("email.txt","r");
while( fgets( str2, MAXCHAR, fp2 )) {
line++;
// printf(" %s",str2);
}
char *ptr = strtok(str2, delim);
while(ptr != NULL)
{
printf("%s\n", ptr);
ptr = strtok(NULL, delim);
}
return 0;
}