1

I was reading and couldn't figure out how to properly do this.

I have a main.c that can't be changed, I'm given a char array char fn[MAX]; max is 100. It is passed into a function readFileName(fn); This is where I get user input to get a new file name. Every time I run my function I get a segmentation fault(core dumped) error. This is what my function looks like. Obviously I'm doing it wrong, but I'm not sure why. Do I need to make a pointer somewhere? Any help would be much appreciated.

void readFileName(char fn[]){
    printf("Please enter the new file that you would like to open\n");
    scanf("%s", fn);

    printf("%s", fn);
}

This is the beginning of the program

int main(int argc, char *argv[]){
    int month, choice;
    int * temps;
    FILE * fin = NULL;
    char fn[MAX];
    fin = openFile(argc, argv);

    month = readMonth(fin);
    temps = fillArray(month, fin);

This is towards the end of the program

            fclose(fin);
            fin = NULL;
            cleanUp(temps);
            temps = NULL;
            readFileName(fn);
//          fin = openInputFile(fn);
            month = readMonth(fin);
            temps = fillArray(month, fin);
6
  • How do you define the fn that you pass in the function call? Commented Nov 17, 2013 at 18:35
  • What do you mean. char fn[MAX]; is created in the beginning of the program and never used. and then it is sent readFileName(fn) Commented Nov 17, 2013 at 18:37
  • So char fn[MAX] is a global that's never used? Can you show us how you call the function readFileName? Also, try checking the return value of scanf. Commented Nov 17, 2013 at 18:45
  • it isn't global. it's declared as char fn[MAX] and then when we call the function it is readFileName(fn) Commented Nov 17, 2013 at 18:46
  • Try checking the return value of scanf. Commented Nov 17, 2013 at 18:46

1 Answer 1

1

I recommend using fgets instead of scanf or your application will crash every time the input is longer than MAX. The scanf is writing over the allocated memory boundaries messing up your stack memory inside the main function. (Of course stdin may still have characters inside of it if the user input is too long and needs to be flushed, but that is another story).

Here is a minimal example:

#include <cstdio>

#define MAX 100

void readFileName(char fileName[]){
    printf("Please enter the new file that you would like to open\n");
    fgets(fileName, MAX, stdin);

    printf("%s", fileName);
}

int main() {
    char fileName[MAX];
    readFileName(fileName);
    printf("Still works here: %s", fileName);
    return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

That works, but it seems like my error was after the input hahaha. Thank you

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.