Problem with fprintf()

This is a discussion on Problem with fprintf() within the C Programming forums, part of the General Programming Boards category; Ok, I don't get it. This fprintf() statement isn't printing the string it should be. I swear it worked when ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Problem with fprintf()

    Ok, I don't get it. This fprintf() statement isn't printing the string it should be. I swear it worked when I had it setup the exact same way in another program. It keeps printing "(null)" now though.

    Code:
        char distext[224];
        FILE *f;
    	f = fopen(filename,"wt");
    	if (!(f)) { MessageBox(NULL,"Unable to open file (PrintResults,1).","Error",0); return 0; }
           sprintf(distext,"some text %d", somevalue);
            fprintf(f,"%08X:  %08X  %s\n", vaddr, value, distext);
    MessageBox(NULL,distext,"Debug",0); //this shows the text fine, but fprintf prints null in place of the string. WHY?
    edit: unbelievable. value being a U64 was screwing it up.
    Last edited by Viper187; 06-21-2008 at 04:16 PM.

  2. #2
    Registered User
    Join Date
    May 2008
    Posts
    53
    That's actually not so unbelievable. Since the fprintf(...) family of functions is not type safe, it has to rely on your format specifiers to know how to interpret what's on the stack. If you provide a 64-bit value yet give it a 32-bit specifier, then the _next_ argument will be taken from the wrong location.

    Most compilers nowadays do give warnings when the specifier and the type don't match up.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Also drop the "t" in "wt". It is not required and is a Microsoft extension.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problems writing some chars to files with fprintf
    By Nazgulled in forum C Programming
    Replies: 3
    Last Post: 04-18-2006, 06:00 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  4. help with basic program
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 02-01-2006, 03:19 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

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