-1

The below program giving the following error when compiling:

./vpl_test: line 2: 18699 Segmentation fault (core dumped) ./solution

What could be the problem with the below C program ?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void sort(long *sorted, int count,long value){
    int i=0;
    sorted[count] = value;
    if(count == 0)return;
    for(i=count;i>=0;i--){
        if(value<sorted[i-1])
        sorted[i] = sorted[i-1];
        else break;
    }
    sorted[i]=value;
}

int main(int argc,char *argv[]){
    FILE *fp = NULL;
    long sorted[1024];
    long value;
    int count = 0;
    int i=0;
    fp = fopen("brandlist.txt","r+");
    //fp = fopen("brandlist.txt","w");
    if(NULL == fp){
        perror("fopen");
        exit(0);
    }
    while(!feof(fp)){
        fscanf(fp,"%ld\n",&sorted[i]);
        sort(sorted,count,value);
        ++count;
    }
    for(i=0;i<count;i++){
        fprintf(fp,"%ld\n",sorted[i]);
    }

    if(fp){
        fclose(fp);
        fp = NULL;
    }
}
Jones Joseph
  • 4,703
  • 3
  • 22
  • 40
John
  • 1
  • 1
  • 2
    See: [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/q/5431941/253056) – Paul R Jun 05 '17 at 05:43
  • while not the end of file , then loop while execute. – John Jun 05 '17 at 05:45
  • I have removed the “while ( !feof (file) )” then the program is not giving any errors and no output..can you please help me.. – John Jun 05 '17 at 05:51
  • Welcome to Stack Overflow! It sounds like you may need to learn how to use a [debugger](https://en.wikipedia.org/wiki/Debugger) to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Paul R Jun 05 '17 at 05:52
  • 1
    `fscanf(fp,"%ld\n",&sorted[i]); sort(sorted,count,value);` --> `fscanf(fp,"%ld\n",&value); sort(sorted,count,value);` and `rewind(fp);` before print. – BLUEPIXY Jun 05 '17 at 06:02
  • 1
    `while(!feof(fp)){ fscanf(fp,"%ld\n",&sorted[i]);` --> `while(1 == fscanf(fp,"%ld\n",&value)){` – BLUEPIXY Jun 05 '17 at 06:05
  • 1
    `sorted[i-1]` : if `i == 0`, `sorted[-1]` – BLUEPIXY Jun 05 '17 at 06:11
  • Cannot reproduce, please provide sample input. – Yunnosch Jun 05 '17 at 07:53
  • Input Zuari Evoks Godrej Duriyan Nilkamal Luxxe Wipro Damro Output Damro Duriyan Evoks Godrej Luxxe Nilkamal Wipro Zuari – John Jun 05 '17 at 11:38
  • Use the existing file “brandlist.txt”. Assume that the file contains 8 brands and length of each name would not be greater than 20. Store the sorted list into the same file. – John Jun 05 '17 at 11:45
  • 1
    Please provide additional information by editing the question. That would for example allow to indicate whether the names are each on one line or all on one line. – Yunnosch Jun 05 '17 at 12:43
  • 1
    You sound like quoting a homework assignment by the way, while not providing the input textfile..... Please tell me that you did not make me do your homework for you. Take the [tour] to make me feel a little better about that. – Yunnosch Jun 05 '17 at 13:57

1 Answers1

0

I could not reproduce the segfault (probably because of being "lucky" or maybe because of having wrong input).
The problem I did have was wrong sorting and strange values in sorted output, I believe that the same problem causes all misbehaviours, including the segfault in your case.

Here is a commented version of your code, picking up from the excellent comments and adding the (many) necessary changes to work on strings instead of integers.
(Allow me this little rant: It would really have saved a lot of time to provide actual sample input from start.)
It does have no segfault (at least not for me), no missorting and no strange values. Output (i.e. new content of input file):

Damro
Duriyan
Evoks
Godrej
Luxxe
Nilkamal
Wipro
Zuari

Code:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef char brandname[20];

void sort(brandname *sorted, int count, brandname *value){
    int i=0;
    strncpy((char*)sorted[count], (char*)value, sizeof(brandname));
    if(count == 0) return;

    // picking up input by BLUEPIXY;
    // this probably prevents the segfault,
    // it definitly prevents strange values
    // appearing in the sorted result
    for(i=count;i>0;i--)
    {
        if(0>strncmp((char*)value, (char*)sorted[i-1],sizeof(brandname)))
            strncpy( (char*)sorted[i], (char*)sorted[i-1], sizeof(brandname));
            else break;
    }
    strncpy((char*)sorted[i], (char*)value, sizeof(brandname));
}
int main(int argc,char *argv[]){
    FILE *fp = NULL;
    brandname sorted[1024];
    brandname value;
    int count = 0;
    int i=0;
    fp = fopen("brandlist.txt","r+");
    //fp = fopen("brandlist.txt","w");
    if(NULL == fp){
        perror("fopen");
        exit(0);
    }
    // picking up input from cooments on feof
    // but also use the increasing "count"
    // instead of unchanging "i"
    while(1==fscanf(fp,"%s", value))
    {
        // use the read value inside "sorted[count]"
        sort(sorted, count, &value);
        ++count;
    }
    // do not append sorted to input
    rewind(fp);
    for(i=0;i<count;i++){
        fprintf(fp,"%s\n",sorted[i]);
    }

    if(fp){
        fclose(fp);
        fp = NULL;
    }

    // avoid a warning
    return 0;
}
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Segmentation fault is resolved, but no output after compilation nothing is obtained – John Jun 05 '17 at 11:22
  • You do get output on the console, don't you? Do you mean no output to the inputfile? Then use the `fprintf()` I deactivated instead of the `printf()` I added. You might want to also `rewind(fp);`, as BLUEPIXY recommended; that is for sorting the input file, instead of appending the content sorted again. – Yunnosch Jun 05 '17 at 11:53
  • Yes, I didnt get any output on the console, It is empty... Instead i need to get the strings sorted. – John Jun 05 '17 at 12:11
  • You do have a file named "brandlist.txt" next to it, with lines of integers? – Yunnosch Jun 05 '17 at 12:29
  • Can you provide your brandlist.txt? – Yunnosch Jun 05 '17 at 12:30
  • How do you compile and how do you execute? – Yunnosch Jun 05 '17 at 12:31
  • Zuari Evoks Godrej Duriyan Nilkamal Luxxe Wipro Damro These names are there in the brandlist.txt we need to sort these strings and store in the same file itself – John Jun 05 '17 at 12:34
  • You are reading integers from it `fscanf(fp,"%ld\n"..`. And you compare what you read with an integer variable `if(value – Yunnosch Jun 05 '17 at 12:39
  • Are the names one per line or all on one line? – Yunnosch Jun 05 '17 at 12:40
  • names are one per line..sorry for the mistake – John Jun 05 '17 at 12:41