1
#include<stdio.h>
#include<string.h>
int main()
{
    char temp[4][10],wc[10];
    int i=0,j;
    FILE *fp;
    fp=fopen("ip.txt","r");
    fscanf(fp,"%s",wc);
    while(strcmp(wc,"\n"))
    {
        strcpy(temp[i],wc);
        fscanf(fp,"%s",wc);
        i++;
    }

    for(j=0;j<i;j++)
    {
        printf("%s",temp[j]);
    }
}

Output: segmentation fault(core dumped)

ip.txt contains:

LOOP ADD AREG,A
 SUB B,A

Where is the error?

6
  • char *temp[4] --> char temp[4][10]; Also fscanf(fp,"%s", wc); need in loop. and wc does not include newline(\n). Commented Feb 19, 2017 at 9:26
  • above correction also gives segmentation fault Commented Feb 19, 2017 at 9:28
  • strcmp(wc,"\n") never become 0. So temp[i++] occurs out of bounds. Commented Feb 19, 2017 at 9:31
  • Then try 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%s specifier). Commented Feb 19, 2017 at 9:31
  • 2
    @alk It seems like the OP is trying to read 4 strings separated by spaces per line, so 10 would be enough then. Commented Feb 19, 2017 at 10:38

2 Answers 2

2

My friends, after watching your question, I find that you mistakenly wrote j into i in the final loop of your program. For i == 4, the array temp[] will proceed to cross the border, which will lead to the segmentation fault.The way to deal is to replace i with j in the final loop, and strcmp(wc,"\n") never become 0 in the first loop, so temp[] will also cross the border.To be honest, I advice you to write down the desire in the question next time.And it is also dangerous to copy wc into the elements of the array of pointers, especially before you allocate space for them, this is also the reason to cause segmentation fault.

Sign up to request clarification or add additional context in comments.

3 Comments

Politest answer ever.
i am copying string named wc into empty string pointed by temp[i]
"And it is also dangerous to copy wc into the elements of the array of pointers" temp is not an array of pointers. So this (not allocating space) is not the reason for segFault here.
2
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:

  1. 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); 
    
  2. 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

}

7 Comments

while((sscanf(buffer, "%9s", wc) == 1) && (i < 4)) : wc is same in this loop.
@BLUEPIXY I'm sorry I didn't get it. Yes, wc is same. What difference does it make?
In case of LOOP ADD AREG,A it must be as follows temp[0]: LOOP, temp[1]: ADD, temp[2]: AREG,A,
@BLUEPIXY Umm not sure what do you mean by that. Will you please elaborate?
buffer does not change during the loop, and the position to take out is the same, so wc will have the same result. see DEMO
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.