Thread: fprintf in void function Not Printing

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    fprintf in void function Not Printing

    I have a function call in my main function. It is suppose to be a progress reporter. Each time the function is called, as shown below, the progress is to be printed on the screen.
    Code:
    loop_counter(&loop_count, some_constant);
    The function body looks like the following:
    Code:
    void loop_counter(long long *loop_count, long long some_constant)
    {
    *loop_count++;
    if(*loop_count % 40000 == 0 || *loop_count == some_constant)
    fprintf(stderr,"\r %d %% done ", (long long)(100.0*(double)(*loop_count)/(double)some_constant));
    }
    For some reason, nothing gets printed on the screen. Can anyone tell me why this is?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >fprintf(stderr,"\r %d
    To print a long long, use either %lld, or if that doesn't work, %I64d.
    Code:
    fprintf(stderr,"\r %I64d %% done ", (long long)(100.0*(double)(*loop_count)/(double)some_constant));
    If nothing at all is being printed, then the if condition isn't being met. You would have to show us what loop_counter and some_constant are at this call:
    >loop_counter(&loop_count, some_constant);

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    MSVC 6 doesn't have support for long longs in its standard C library, but Dev-C++ and others do. MSVC treats it like a long, so if you don't exceed the limits of a long, it should work even there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    If I replace
    Code:
    loop_counter(&loop_count, some_constant);
    in my main() function with
    Code:
    loop_count++;
    if(loop_count % 40000 == 0 || loop_count == totalN)
    fprintf(stderr,"\r %d %% done ", (long long)(100.0*(double)loop_count/(double)totalN));
    then everything works fine. I don't think the problem is the %d or the long long. The problem is that nothing at all gets printed to the screen.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Based on the code you just posted, then here:
    >*loop_count++;
    What you really want is:
    Code:
    (*loop_count)++;

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    Thanks. That did it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM