Reading/Writing other files from inside a C program

Discussion in 'C Programming' started by explorer@gmail.com, Feb 26, 2005.

  1. Guest

    I am learning C, so I decided to try out r/w-ing to other files. Can
    anyone please tell me why I get the following error when running the
    program?


    Transcript:
    Name?
    Nikhil R. Mulani
    Bus error


    Code:

    #include <stdio.h>

    main()
    {
    char name[60];
    FILE *Fp;

    Fp = fopen("/deeter.dat", "w");

    printf("Name?\n");
    scanf("%s",name);

    fprintf(Fp, "Name: %s\n", name);

    int fclose( FILE *Fp );
    }
     
    , Feb 26, 2005
    #1
    1. Advertising

  2. wrote:
    > I am learning C, so I decided to try out r/w-ing to other files. Can
    > anyone please tell me why I get the following error when running the
    > program?
    > [...]
    > scanf("%s",name);
    > [...]


    You miss an "&" before "name".

    --
    If geiger counter does not click,
    the coffee, she is just not thick
     
    Sebastian Hungerecker, Feb 26, 2005
    #2
    1. Advertising

  3. Guest

    thanks!
     
    , Feb 26, 2005
    #3
  4. infobahn Guest

    Sebastian Hungerecker wrote:
    >
    > wrote:
    > > I am learning C, so I decided to try out r/w-ing to other files. Can
    > > anyone please tell me why I get the following error when running the
    > > program?
    > > [...]
    > > scanf("%s",name);
    > > [...]

    >
    > You miss an "&" before "name".


    I haven't seen the original article, but if name is an array of char,
    then prepending & is incorrect. %s matches char *, not char (*)[].

    Also, the OP should be aware that scanf("%s", name) opens the program
    up to the possibility of a buffer overrun attack.
     
    infobahn, Feb 26, 2005
    #4
  5. -berlin.de Guest

    <> wrote:
    > I am learning C, so I decided to try out r/w-ing to other files. Can
    > anyone please tell me why I get the following error when running the
    > program?


    > Transcript:
    > Name?
    > Nikhil R. Mulani
    > Bus error


    > Code:


    > #include <stdio.h>


    > main()
    > {
    > char name[60];
    > FILE *Fp;


    > Fp = fopen("/deeter.dat", "w");


    > printf("Name?\n");
    > scanf("%s",name);


    > fprintf(Fp, "Name: %s\n", name);


    > int fclose( FILE *Fp );
    > }


    There are several problems with your program of different importance.

    1) main() is supposed to return an int. While you will get away without
    specifying a return type on older compilers (i.e. non-C99) you should
    avoid that and use either

    int main( void )

    or

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

    2) You never test if the call of fopen() succeeds. But when it fails
    'Fp' will be set to NULL and you can't write to the file.

    3) Using scanf() to read in strings is tricky. First of all, when
    you just have a "%s" conversion specifier you can't avoid that
    the user enters more characters than fit into the buffer into
    which the result gets written - and if (s)he does you write past
    the end of that buffer, which is a very bad mistake. Thus you
    should call it at least with

    scanf( "%59s", name );

    That way the user can't enter more than 59 characters (you need
    the 60th for the final '\0' character).

    Another problem is that scanf() stops at white space (i.e spaces,
    tabs etc.). This will keep you from entering "Nikhil R. Mulani"
    since scanf() will stop reading after the "Nikhil". While that
    can be circumvented by using

    scanf( "%59[^\n]", name );

    it probably is simpler to use fgets() to read in the whole line.

    4) Since you didn't check that 'Fp' isn't NULL you might get into
    problems with the call of fprintf() - this expects a valid FILE
    pointer as the first argument.

    5) The line

    int fclose( FILE *Fp );

    should get the compiler upset (and I strongly doubt that you got
    the program compiled in the for you posted it). I guess you want

    fclose( Fp );

    here - but don't call fclose() when 'Fp' is NULL.

    6) Since main() is supposed to return an int your program is missing
    a return statement at the end.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Feb 26, 2005
    #5
  6. -berlin.de Guest

    Sebastian Hungerecker <> wrote:
    > wrote:
    >> I am learning C, so I decided to try out r/w-ing to other files. Can
    >> anyone please tell me why I get the following error when running the
    >> program?
    >> [...]
    >> scanf("%s",name);
    >> [...]


    > You miss an "&" before "name".


    No, he definitely doesn't - 'name' is an array of chars and in value
    context (like here) it is automatically converted to a pointer to the
    first element of the array.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Feb 26, 2005
    #6
  7. -berlin.de wrote:
    > Sebastian Hungerecker <> wrote:
    >>You miss an "&" before "name".

    >
    > No, he definitely doesn't


    You're right. I didn't look/think carefully enough - I'm sorry for the
    wrong advice.

    --
    If geiger counter does not click,
    the coffee, she is just not thick
     
    Sebastian Hungerecker, Feb 26, 2005
    #7
  8. Guest

    Thank you all. I'll try out these suggestions.
     
    , Feb 26, 2005
    #8
  9. Guest

    I got it to work, and learned a lot too. thanks again
     
    , Feb 26, 2005
    #9
  10. Sebastian Hungerecker wrote:
    > wrote:
    >
    >> I am learning C, so I decided to try out r/w-ing to other files. Can
    >> anyone please tell me why I get the following error when running the
    >> program?
    >> [...]
    >> scanf("%s",name);
    >> [...]

    >
    >
    > You miss an "&" before "name".
    >


    You are wrong.
     
    Martin Ambuhl, Feb 26, 2005
    #10
  11. wrote:
    > thanks!
    >


    Don't thank him: he's wrong.
     
    Martin Ambuhl, Feb 26, 2005
    #11
  12. wrote:
    > I am learning C, so I decided to try out r/w-ing to other files. Can
    > anyone please tell me why I get the following error when running the
    > program?
    >
    >
    > Transcript:
    > Name?
    > Nikhil R. Mulani
    > Bus error


    It's hard to say. It's probably not because of the failure to explicitly
    state the return type of main as int (required for C99), or
    because of the failure to explicitly return a value from main (required
    for predictable behavior before C99), or because of the failure to check
    that the fopen succeeded, or because of using scanf("%s",... for input
    data that *might* contain whitespace. My best guess is ...

    > Code:
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > char name[60];
    > FILE *Fp;
    > Fp = fopen("/deeter.dat", "w");
    > printf("Name?\n");
    > scanf("%s",name);
    > fprintf(Fp, "Name: %s\n", name);
    > int fclose( FILE *Fp );


    .... this line, which contains a bogus 'int' and 'FILE *', and so looks
    like a declaration in the middle of your code. This should, on a
    pre-C99 compiler, have resulted in a failure to compile.

    > }


    Here is an example of how you might start to make your code look more
    like a real C program. Improvements to my example code are, of course,
    possible.

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

    int main(void)
    {
    char name[60];
    FILE *Fp;
    if (!(Fp = fopen("./deeter.dat", "w"))) {
    fprintf(stderr,
    "Could not open \"./deeter.dat\" for output.\n");
    exit(EXIT_FAILURE);
    }
    printf("What is your name? ");
    fflush(stdout);
    if (fgets(name, sizeof name, stdin)) {
    char *nl;
    if ((nl = strchr(name, '\n')))
    *nl = 0;
    fprintf(Fp, "Name: %s\n", name);
    }
    else {
    fprintf(stderr, "Arcane input error.\n");
    fclose(Fp);
    remove("./deeter.dat");
    exit(EXIT_FAILURE);
    }
    fclose(Fp);
    return 0;
    }
     
    Martin Ambuhl, Feb 26, 2005
    #12
  13. Jack Klein Guest

    On 26 Feb 2005 17:59:11 GMT, -berlin.de wrote
    in comp.lang.c:

    > <> wrote:
    > > I am learning C, so I decided to try out r/w-ing to other files. Can
    > > anyone please tell me why I get the following error when running the
    > > program?

    >
    > > Transcript:
    > > Name?
    > > Nikhil R. Mulani
    > > Bus error

    >
    > > Code:

    >
    > > #include <stdio.h>

    >
    > > main()
    > > {
    > > char name[60];
    > > FILE *Fp;

    >
    > > Fp = fopen("/deeter.dat", "w");

    >
    > > printf("Name?\n");
    > > scanf("%s",name);

    >
    > > fprintf(Fp, "Name: %s\n", name);

    >
    > > int fclose( FILE *Fp );
    > > }

    >
    > There are several problems with your program of different importance.


    [snip]

    > 5) The line
    >
    > int fclose( FILE *Fp );
    >
    > should get the compiler upset (and I strongly doubt that you got
    > the program compiled in the for you posted it). I guess you want


    You went a little overboard here. The line represents a perfectly
    correct prototype for the function fclose(), and a prototype is
    perfectly legal at this point in the source code. So it most
    certainly should compile, although it doesn't do what the OP expects.

    > fclose( Fp );
    >
    > here - but don't call fclose() when 'Fp' is NULL.


    The rest of your advice is excellent, of course.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 26, 2005
    #13
  14. Jack Klein <> writes:
    > On 26 Feb 2005 17:59:11 GMT, -berlin.de wrote
    > in comp.lang.c:

    [snip]
    >> 5) The line
    >>
    >> int fclose( FILE *Fp );
    >>
    >> should get the compiler upset (and I strongly doubt that you got
    >> the program compiled in the for you posted it). I guess you want

    >
    > You went a little overboard here. The line represents a perfectly
    > correct prototype for the function fclose(), and a prototype is
    > perfectly legal at this point in the source code. So it most
    > certainly should compile, although it doesn't do what the OP expects.
    >
    >> fclose( Fp );
    >>
    >> here - but don't call fclose() when 'Fp' is NULL.

    >
    > The rest of your advice is excellent, of course.


    A prototype is legal at that point only if you have a C99 compiler (or
    a C90 compiler that allows declarations to follow statements as an
    extension) <OT>or a C++ compiler</OT>.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 26, 2005
    #14
  15. "" <> writes:
    > I am learning C, so I decided to try out r/w-ing to other files. Can
    > anyone please tell me why I get the following error when running the
    > program?
    >
    >
    > Transcript:
    > Name?
    > Nikhil R. Mulani
    > Bus error
    >
    >
    > Code:
    >
    > #include <stdio.h>
    >
    > main()
    > {
    > char name[60];
    > FILE *Fp;
    >
    > Fp = fopen("/deeter.dat", "w");
    >
    > printf("Name?\n");
    > scanf("%s",name);
    >
    > fprintf(Fp, "Name: %s\n", name);
    >
    > int fclose( FILE *Fp );
    > }


    My best guess is that the fopen() call failed because you don't have
    permission to create the named file. You then call fprintf() with Fp
    as an argument, which invokes undefined behavior. Dropping the
    leading '/' might address the problem. In any case, you need to check
    whether the fopen() succeeded, and *don't* attempt to write to the
    file if it failed.

    Something else you should address is the fact that scanf("%s", name)
    only grabs the first whitespace-delimited word of input; in this case,
    just "Nikhil", not "Nikhil R. Mulani". You probably want fgets().

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Feb 26, 2005
    #15
  16. -berlin.de Guest

    Jack Klein <> wrote:
    > On 26 Feb 2005 17:59:11 GMT, -berlin.de wrote
    > in comp.lang.c:
    >> 5) The line
    >>
    >> int fclose( FILE *Fp );
    >>
    >> should get the compiler upset (and I strongly doubt that you got
    >> the program compiled in the for you posted it). I guess you want


    > You went a little overboard here. The line represents a perfectly
    > correct prototype for the function fclose(), and a prototype is
    > perfectly legal at this point in the source code. So it most
    > certainly should compile, although it doesn't do what the OP expects.


    Yes, thank you. I didn't realize that it's allowed now in C99 and
    only tested in strict ANSI-89 mode.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Feb 26, 2005
    #16
  17. Jack Klein Guest

    On Sat, 26 Feb 2005 20:23:56 GMT, Keith Thompson <> wrote
    in comp.lang.c:

    > Jack Klein <> writes:
    > > On 26 Feb 2005 17:59:11 GMT, -berlin.de wrote
    > > in comp.lang.c:

    > [snip]
    > >> 5) The line
    > >>
    > >> int fclose( FILE *Fp );
    > >>
    > >> should get the compiler upset (and I strongly doubt that you got
    > >> the program compiled in the for you posted it). I guess you want

    > >
    > > You went a little overboard here. The line represents a perfectly
    > > correct prototype for the function fclose(), and a prototype is
    > > perfectly legal at this point in the source code. So it most
    > > certainly should compile, although it doesn't do what the OP expects.
    > >
    > >> fclose( Fp );
    > >>
    > >> here - but don't call fclose() when 'Fp' is NULL.

    > >
    > > The rest of your advice is excellent, of course.

    >
    > A prototype is legal at that point only if you have a C99 compiler (or
    > a C90 compiler that allows declarations to follow statements as an
    > extension) <OT>or a C++ compiler</OT>.


    You are correct, sir, my bad. Jens forgot about C99, and I forgot
    about C90!

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 28, 2005
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ken Philips
    Replies:
    0
    Views:
    351
    Ken Philips
    Dec 6, 2004
  2. Erin K
    Replies:
    2
    Views:
    421
    William Brogden
    Aug 7, 2006
  3. Replies:
    0
    Views:
    806
  4. Replies:
    5
    Views:
    516
    Wolf Behrenhoff
    May 15, 2009
  5. Narendra Sisodiya

    Reading files inside zip files from Javascript

    Narendra Sisodiya, Dec 7, 2009, in forum: Javascript
    Replies:
    7
    Views:
    296
    Michael Haufe (\TNO\)
    Dec 8, 2009
Loading...

Share This Page