0

I am experiencing a segfault using fopen(). My code is below.

void encoding(char otext[], char ibin[]){
    //Some characters for storage:
    char c, h, a, r;
    unsigned int x;

    //Check if file is available:
    FILE *in; 
    FILE *out;
    in = fopen("in.txt", "r");
    if (in==NULL){
        printf("Error: No input file\n");
        exit(1);
    }
    out = fopen("out.txt", 'w');
    if (out==NULL){
        printf("Error: No output file\n");
        exit(1);
    }
    //Scanning the file: 
    while(!feof(in)){//While not at the end of the in file
        fscanf(in, "%c", &c);
        fscanf(in, "%c", &h);
        fscanf(in, "%c", &a);
        fscanf(in, "%c", &r);
        x = pack(c,h,a,r);
        fprintf(out, "%u", x);
        //fwrite(&x,sizeof(int),1,out);
    }
    //Close file
    fclose(in);
    fclose(out);
}

unsigned int pack(char c1, char c2, char c3, char c4){//Works
    int bits = 8;
    unsigned int x = c1;
    x = (x << bits) | c2;
    x = (x << bits) | c3;
    x = (x << bits) | c4;
    return x;
}

unsigned int encrypt(unsigned int x){//Works
    int i = 0;
    while (i < KEY){
        unsigned int temp = x;
        x = (x<<1);
        if (temp > x){
            x += 1;
        }
        i+=1;
    }
    return x;
}

I found this topic: Segfault while using fopen - which suggests that the issue may be in using fprintf() incorrectly. So I commented out the fprintf() line and simply had it set to open and close out, like so:

//Edit for clarity: I commented out everything BUT the 3 lines of 
//code below, and I still got the segfault.
/*...*/
File *out;
out = fopen("out.txt", "w");
/*...*/
fclose(out);

Even this causes a segfault, leading me to believe the issue is not in my use of fprintf(). At this point I'm at a loss.

M.M
  • 138,810
  • 21
  • 208
  • 365
Luke Mathwalker
  • 107
  • 1
  • 6
  • Have a look at valgrind. – Jeff Holt Nov 27 '19 at 22:50
  • 2
    Also have a look at `feof()` – wildplasser Nov 27 '19 at 22:51
  • 1
    This site uses a Question/Answer format -- please do not edit the question to include answers in it. You can Accept an answer posted below, or post your own answer if none of the existing answers solve your problem – M.M Nov 27 '19 at 23:26
  • Not your main problem, but: your use of [`feof` in your while loop is wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). You should be checking the return value of each `fscanf` call, and exiting the loop if the return value is anything other than 1. – Steve Summit Nov 27 '19 at 23:56

1 Answers1

2

This is your issue. You're passing a char instead of a string.

out = fopen("out.txt", 'w');
if (out==NULL){
    printf("Error: No output file\n");
    exit(1);
}

Replace the 'w' with "w". This is the correct version.

out = fopen("out.txt", "w");
if (out==NULL){
    printf("Error: No output file\n");
    exit(1);
}
Ahmed Tounsi
  • 1,482
  • 1
  • 14
  • 24
  • 2
    Oh my god. That worked. Thank you, and I hate myself. – Luke Mathwalker Nov 27 '19 at 22:58
  • 1
    + Do `#include ` so you get a warning pointing you in this direction. – Petr Skocik Nov 27 '19 at 22:58
  • 1
    You should have gotten a warning with or without `#include `. Without it, `fopen` is undeclared, and as of C99 C doesn't permit calls to undeclared functions. Before adding the `#include`, figure out how to invoke your compiler so it warns you about this. @PSkocik – Keith Thompson Nov 27 '19 at 23:12
  • 1
    ^ preferably, invoke the compiler so that it gives an *error* about this. Some people think warnings are unimportant – M.M Nov 27 '19 at 23:28
  • I suggest compiling with the flags `-Wall -Werror`. – Ahmed Tounsi Nov 27 '19 at 23:36
  • @M.M: I suggest doing one or both of the following: (a) Configure your compiler to treat serious problems like this as fatal errors and/or (b) take warnings seriously. – Keith Thompson Nov 27 '19 at 23:45