I need help with fputc and fgetc

This is a discussion on I need help with fputc and fgetc within the C Programming forums, part of the General Programming Boards category; hi, this is my code: FILE *ff; FILE *gg; ff=fopen("11.txt","w"); int val=54321; fputc(val,ff); gg=fopen("11.txt","r"); int get; get=fgetc(gg); printf("get: %d",get); fclose(gg); ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    3

    I need help with fputc and fgetc

    hi, this is my code:

    FILE *ff;
    FILE *gg;
    ff=fopen("11.txt","w");
    int val=54321;
    fputc(val,ff);
    gg=fopen("11.txt","r");
    int get;
    get=fgetc(gg);
    printf("get: %d",get);
    fclose(gg);
    fclose(ff);


    why does it allways print me: -1
    and never the value that I inset to val(54321 in this case...) ???

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    Those functions accept integer values of characters...<256...54321 isn't a valid character, try something below 256.

    ASCII - Wikipedia, the free encyclopedia

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    Also, you probably ought to fclose(ff) before you go and fopen() gg, since they reference the same file.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    If you want to write an integer, use fwrite() instead of fputc().
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    974
    Quote Originally Posted by bithub View Post
    If you want to write an integer, use fwrite() instead of fputc().
    In text mode?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by Epy View Post
    In text mode?
    If he wants to write the ascii representation of the number, then he can use fprintf() instead of fwrite(). It is unclear what he is trying to do though.
    bit∙hub [bit-huhb] n. A source and destination for information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. fgetc and fputc
    By phoebus in forum C Programming
    Replies: 5
    Last Post: 04-24-2008, 11:23 AM
  3. fputc fgetc file incomplete reed
    By kryptkat in forum C Programming
    Replies: 7
    Last Post: 12-11-2007, 08:20 PM
  4. putc() != fputc() ?
    By trekker in forum C Programming
    Replies: 2
    Last Post: 07-02-2002, 08:40 AM
  5. *Flails*
    By Clyde in forum C Programming
    Replies: 5
    Last Post: 03-27-2002, 02:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21