fscanf(fp,"%s",wc);
%s will match a sequence of non-white-space characters and will store that sequence at wc. Now since \n is a white-space character this call will ignore it!
So wc will never be equal to \n.
Therefore, while(strcmp(wc,"\n") is an infinie loop as the strcmp will never return 0. (Even when the end of file ip.txt is reached!)
The statement i++ will at some point make i greater than 4 (array limit) and that's when you get the segmentation fault. (Saves you from an infinite loop though!)
Other issues:
fscanf(fp,"%s",wc);
Now what if a sequence of non-white-space characters is greater than 9 characters? fscnaf will store that sequence and an addiotional \0 byte at the end of it, overwriting the buffer wc (size is 10, remember?)
To avoid buffer overruns always use maximum field width with %s. e.g.
fscanf(fp,"%9s",wc);
fp=fopen("ip.txt","r");
Now how do you know that fopen succeded? Always check whether fp is NULL before doing any processing with fp e.g.
if(fp=fopen("ip.txt","r"))
{
//`fp` code goes here
}
else
printf("fopen failed!\n");
Solution:
You haven't mentioned but if your aim is to store the words from just a single line in an array temp, do processing and then again store the words from next line in an array temp then you can use fgets() to fetch a line in a buffer. And then using sscanf() to retrieve each word from that buffer to store them in temp array.
Something like:
char buffer[50], temp[4][10], *buf, *wc;
int i;
while(fgets(buffer, 50, stdin) != NULL)
{
i = 0;
buf = buffer;
while((wc = strtok(buf, " \n")) && (i < 4))
{
strcpy(temp[i++], wc);
if (buf && i)
buf = NULL;
}
// process words here
}
char *temp[4]-->char temp[4][10];Alsofscanf(fp,"%s", wc);need in loop. andwcdoes not include newline(\n).strcmp(wc,"\n")never become0. Sotemp[i++]occurs out of bounds.char temp[4][50]; char wc[50]'Your whole problem is that your buffers overflow, so either make them larger or use an input function with bounds checking (not the%sspecifier).