简体   繁体   中英

Segmentation fault in String copy function strcpy in C

#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?

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.

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 . eg

     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 eg

     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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM