cheese425
cheese425

Reputation: 61

Segmentation fault 11?

My program compiles but when I run it ./program -n 3 1000.txt, I get

segmentation fault: 11.

What does it mean?

What my program does: print the first 10 lines of the file. If the file has less than 10 lines the entire file should be printed. The program should not create any files. It just prints part of the content of a file to standard output.

It should also be possible to specify that a different number of lines be printed. This will specified by passing the string -n as the first argument to the program, the number of lines to be printed as the second argument and the file as the third argument.

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

int main(int argc, char * argv[]){

int i, n;
char line[1024];
n = atoi(argv[2]);

if(argc == 2){ //eg ./program 1000.txt
    FILE * fPointer;
    fPointer = fopen("1000.txt", "r");
    for(i = 1; i <= 10; i++){
        fgets(line, 1024, fPointer);
        fprintf(fPointer, "%s\n", line);
    }
    fclose(fPointer);
} else if(argc == 4){ //eg ./program -n 4 1000.txt
    FILE * fPointer;
    fPointer = fopen("1000.txt", "r");
    for(i = 1; i <= n; i++){
        fgets(line, 1024, fPointer);
        fprintf(fPointer, "%s\n", line);
    }
    fclose(fPointer);
} else {
    printf("Wrong number of inputs.\n");
}

return 0;

}

Upvotes: 0

Views: 1729

Answers (2)

Yağmur Oymak
Yağmur Oymak

Reputation: 477

fprintf(fPointer, "%s\n", line)

This is the problem. You are getting a line by fgets into your array, and instead of printing it to standard output, you try to print to the file which you have opened read-only. You just need to use normal printf for that job.

EDIT

It turned out it is not the reason why it segfaults, but it still is a problem if you want your program do what you want.

Check if the file really exists, that may be the problem as pointed out by @MikeCAT.

Upvotes: 3

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26647

This example will print 3 lines of the file for the arguments $ ./a.out -n 3 data.txt. I moved the atoint inside the condition and I changed the printing to printf.

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

int main(int argc, char *argv[]) {
    int i, n;
    char line[1024];
    FILE *fPointer;
    if (argc == 2) { //eg ./program 1000.txt
        FILE *fPointer;
        fPointer = fopen("data.txt", "r");
        for (i = 1; i <= 10; i++) {
            fgets(line, 1024, fPointer);
            printf("%s\n", line);
        }
        fclose(fPointer);
    } else if (argc == 4) { //eg ./program -n 4 1000.txt
        n = atoi(argv[2]);
        FILE *fPointer;
        fPointer = fopen("data.txt", "r");
        for (i = 1; i <= n; i++) {
            fgets(line, 1024, fPointer);
            printf("%s\n", line);
        }
        fclose(fPointer);
    } else {
        printf("Wrong number of inputs.\n");
    }
    return 0;
}

File data.txt

Superman
Batman
Cyclops
Thor
Wolverine
Superman
Batman
Cyclops
Thor
Wolverine
Superman
Batman
Cyclops
Thor
Wolverine
Superman
Batman
Cyclops
Thor
Wolverine

Output

$ ./a.out -n 3 data.txt
Superman

Batman

Cyclops

Upvotes: 4

Related Questions