0

Im still considered a beginner in c and i started learning about files. i already built a blank file. Every time i compile this program, the file is still blank. Need help!!

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

     int main()
    {
       FILE * x;
       char name[25];

       printf("enter your name: ");
       scanf("%s", &name);

       x = fopen("x1.txt", "w");

       if(x = NULL)
      {
         printf("Unable to open the file");
      }

       else
      {
         fprintf(x, "%s\n", name);

         printf("date has been entered successfully to the file");
         fclose(x);
      }


 return 0;
}

Thank you

jxh
  • 69,070
  • 8
  • 110
  • 193
  • 3
    Change `x = NULL` to `x == NULL`. –  Mar 17 '17 at 20:43
  • I'm guessing you didn't do what your problem says `every time I compile the program the file is still blank`, I say that because compiling won't have any effect on the file. However, running the program `after fixing the errors` will have an affect. – KevinDTimm Mar 17 '17 at 20:48
  • Compiling a program does not execute it. You should run it. – too honest for this site Mar 17 '17 at 20:54
  • Your question is unclear. Say exactly what you are doing in what environment and exactly what was output (errors messages, files, etc). – philipxy Mar 17 '17 at 21:16
  • this line: `scanf("%s", &name);` allows the user to enter enough characters to overrun the input buffer. This is undefined behavior and can/will lead to a seg fault event. Suggest: `scanf("%24s", &name);` – user3629249 Mar 18 '17 at 16:49
  • this line: `printf("Unable to open the file");` should be: `perror( "fopen for x1.txt for write failed" ); exit( EXIT_FAILURE );` – user3629249 Mar 18 '17 at 16:50
  • regarding this text: "date has been entered" perhaps you meant: "data has been entered" – user3629249 Mar 18 '17 at 16:52
  • this line: `printf("date has been entered successfully to the file");` leaves the text in the output buffer until the program closes. since there are no formatting specifiers, suggest: `puts( "date has been entered successfully to the file" );` as `puts()` appends a '\n' to the output, which will force the flush of the stdout buffer – user3629249 Mar 18 '17 at 16:55
  • this line: `if(x = NULL)` is an assignment, not a comparison. You can get a lot of gray hairs trying to find that keypunch error in a large program. However, if you make it a habit to always place the literal on the left, then the compiler would find that for you. I.E. `if( NULL = x)` will produce a compiler error due to the assignment to a literal. – user3629249 Mar 18 '17 at 16:58

1 Answers1

5

A file existed, and contained my name, after making the following changes and rebuilding/running the program:

(see comments in line for reasons)
Change:

if(x = NULL)//assignment - as is, this statement will always evaluate 
            //to false, as x is assigned to NULL.

To:

if(x == NULL)// comparison - this will test whether x is equal to NULL without changing x.

Change: (this was key to your file not being populated)

   scanf("%s", &name);//the 'address of' operator: '&' is not needed here.
                      //The symbol 'name' is an array of char, and is
                      //located at the address of the first element of the array.

To:

   scanf("%s", name);//with '&' removed.

Or better:

   scanf("%24s", name);//'24' will prevent buffer overflows
                       //and guarantee room for NULL termination. 

Yet one more method to address the comment about not using scanf at all...:

char buffer[25];//create an additional buffer
...
memset(name, 0, 25);//useful in loops (when used) to ensure clean buffers
memset(buffer, 0, 25);
fgets(buffer, 24, stdin);//replace scanf with fgets...
sscanf(buffer, "%24s", name);//..., then analyze input using sscanf 
                             //and its expansive list of format specifiers 
                             //to handle a wide variety of user input.
                             //In this example, '24' is used to guard 
                             //against buffer overflow.

Regarding the last method, here is a page detailing the versatility of handling user input strings using sscanf.

ryyker
  • 22,849
  • 3
  • 43
  • 87