Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: system(command) not consistent with printing to stdout

  1. #1
    Join Date
    Aug 2012
    Beans
    486

    system(command) not consistent with printing to stdout

    Hello,
    I wrote a program which does something like
    Code:
    while(i<100)
    {
     printf(some_message_indicating_what_system_command_I'mdoing);
     system(some system command);
     printf("\n");
    }
    When I do ./a.out > log.txt, I find that all my printf's are at the bottom of the file, and all the system command outputs are at the top of the file. I would like to have them in sequential order so that I can know which system command is called when comparing outputs.

    Please advise.
    Thanks.

    PS : Consider I'm printing the ls output of dir1, dir2, etc, upto dir100 in a loop. As of now, I get an output something like(see below). So this doesn't tell me which dir contains which files.

    -----------------------------------
    file.txt
    hello.txt
    ..
    ..
    upto dir100 contents



    printing dir1 contents
    printing dir2 contents
    printing dir3 contents
    printing dir100 contents
    -----------------------------------

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: system(command) not consistent with printing to stdout

    It's probably because the buffer for stdout is not flushed until you send the '\n'. Try putting '\n' at the end of the first printf.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Aug 2012
    Beans
    486

    Re: system(command) not consistent with printing to stdout

    Quote Originally Posted by r-senior View Post
    It's probably because the buffer for stdout is not flushed until you send the '\n'. Try putting '\n' at the end of the first printf.
    r-senior, thanks for the reply. But I have taken care of that. That used to be one of my frequent mistakes, but not anymore.
    I also tried putting a sleep, but doesn't work.

    At the moment, my code looks something like(below), within a while loop.
    Code:
    printf("command == [%s]\n",command);
        system(command);
        printf("\n");
    Last edited by IAMTubby; March 7th, 2013 at 10:58 AM.

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,182
    Distro
    Kubuntu

    Re: system(command) not consistent with printing to stdout

    Try this ordering of statements in your loop:
    Code:
    printf(...);
    system(...);
    fflush(stdout);    // this is key!
    Here's an example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
        const char* dirs[] = { "/etc",  "/usr/bin",  "/usr/sbin" };
    
        for (size_t i = 0; i < sizeof(dirs) / sizeof(const char*); ++i)
        {
            char cmd[1024];
            snprintf(cmd, sizeof(cmd), "ls %s", dirs[i]);
    
            printf("Listing the files within %s...\n",  dirs[i]);
            system(cmd);
            fflush(stdout);
            printf("\n\n");
        }
    
        return 0;
    }

  5. #5
    Join Date
    Aug 2012
    Beans
    486

    Re: system(command) not consistent with printing to stdout

    Quote Originally Posted by dwhitney67 View Post
    fflush(stdout); // this is key!
    wow sir, that works

    EDIT:I'm trying to "mark thread as solved". where do I do that ? need a couple of days to get acquainted with the new layout which is great
    Last edited by IAMTubby; March 7th, 2013 at 02:27 PM.

  6. #6
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: system(command) not consistent with printing to stdout

    You can't. It's currently broken with the upgrade. The workaround is to edit the title of the thread and prefix with [SOLVED].
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  7. #7
    Join Date
    Aug 2012
    Beans
    486

    Re: system(command) not consistent with printing to stdout

    Quote Originally Posted by r-senior View Post
    You can't. It's currently broken with the upgrade. The workaround is to edit the title of the thread and prefix with [SOLVED].
    Alright, I'll wait for it then.
    A small suggestion : can we change the background color for posts from gray back to white ? Otherwise the site's great.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,182
    Distro
    Kubuntu

    Re: system(command) not consistent with printing to stdout

    Quote Originally Posted by IAMTubby View Post
    Otherwise the site's great.
    On the contrary... too much space is allocated for displaying one's avatar and forum metrics above each post. I fail to see what was wrong with the previous version.

  9. #9
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: system(command) not consistent with printing to stdout

    +1

    I need to get a cooling fan for my mouse wheel.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  10. #10
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,690
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: system(command) not consistent with printing to stdout

    The real problem is that the buffering of stdout varies with the type of the actual output. By default, if the output is a tty (ie, no redirection), stdout is line-buffered, and a "\n" in the stream forces a flush. Otherwise, it is block-buffered and you get output only when the buffer (usually 4K) is full, or when the stdout is closed or when the program exits...

    There are functions to control buffering, so, instead of fflush()'ing left and right, you can use setlinebuf(stdin) at the beginning of the program and keep the rest of the code unchanged. See "man setvbuf" for the gory details.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •