0

the following code snippet is from a function I wish to add to an existing program. it looks good to me , the compiler is happy with it, but when the program encounters it, the program stops, (or disappears off into an infinite loop somewhere) and I get a dialog box telling me the program has "Stopped working".

The puts() statements before and after this snippet show me that it is this bit of code that is killing the program.

the file it is trying to access does exits, and is closed.

Any ideas?

void fill_array(void)

{

    struct data_cell save_1 , save_2 , save_3 ;
    int array_no , a , b , c ;
    int dimensions[3];
    char new_cell_data[20];


    char path = "C:\\Users\\dave\\Documents\\data.txt";
    FILE *fp;

    puts("Got here_1");

    if ((fp = fopen(path,"w")) == NULL)
            {
            printf("cannot open file");
            exit(1);
            }

     puts("Got here_2");

     printf("Enter the target array, (range 1 - 3 \t");
     scanf("%d", &array_no);

     ...... more code......

     fclose(fp);

}

    
DaveC
  • 19
  • 2
  • 6
    I am confident that your compiler *does not* accept the code presented in the question. Do not retype your code into the question; always copy & paste to ensure that you do not make errors. Moreover, code snippets generally are not very useful. Our standard is a [mre]. As a bonus, the exercise of preparing a MRE often results in discovering the flaw for yourself. – John Bollinger Oct 18 '20 at 13:43
  • Note that since your `printf` for "Enter the target array" doesn't end with a newline, [buffering](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin/1716621#1716621) means the message will likely not be printed before `scanf` starts waiting for input. I'm wondering if this is what you are interpreting as "disappearing into an infinite loop somewhere'. – Nate Eldredge Oct 18 '20 at 14:54
  • 1
    [This](https://godbolt.org/z/r6sv9e) is not called "happy" in any language. – n. m. could be an AI Oct 22 '20 at 07:56
  • Voted to close as typo, since `char` and `char*` are not the same type, and your compiler will have told you about this. – Marcus Müller Apr 16 '21 at 18:04

0 Answers0