Thread: Non-sequential printing

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    21

    Non-sequential printing

    Hi, I'm having issues with perror(), it's printing before an fprintf call which occurs before it. The tokptr variable is correct, I'm also aware that errno should be saved if perror isn't ocurring directly after fopen(), though fprintf AFAIK does not alter the errno and it certainly isn't not-printing ;p

    Code:
    if( fopen(tokptr,"r") == NULL ) {
         fprintf(stdout,"%s\n", tokptr);  /* Test */
         fprintf(stdout,"Error opening settings file %s: ",tokptr);
         perror("Error opening tokptr");
         exit(1);
    }
    Output:
    Code:
    saec@saeculum:~/GA3$ ./GASim-helper --settingsFile="loo.ini"
    loo.ini
    No such file or directory
    Error opening settings file loo.ini
    The code will print the perror() string before it prints the fprintf string, unless the fprintf string has a newline character at the end, like so:
    Code:
    fprintf(stdout,"Error opening settings file %s\n: ",tokptr);
    I have similar code in a different file, where the fprintf occurs before the perror, and it prints in this order.

    This is the code that works as it should - when testing, this code isn't reached so plays no part in the issue.
    Code:
    if( freopen(argv[1], "a", stdout) == NULL ) {
            fprintf(stdout,"Error with freopen(): ");
            perror("");
            return 0;
    }
    Can anyone shed some light onto this, my only idea was that tokptr wasn't correct, but it is.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Perror prints to stderr, which is unbuffered. stdout is buffered, and the output may be "held" to improve performance.

    If you change the remaining code to use stderr as well, it will come out in order (and it's most likely what you want anyways, since you are printing error messages!)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    21
    Ahh, why didn't I catch that, nice one! I'd need to flush stdout to ensure it would print before the perror call, but you're right with the stderr, I should be using it to print error messages.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. sequential file help plz
    By dutrachr in forum C Programming
    Replies: 4
    Last Post: 04-18-2006, 11:41 AM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM