Find the answer to your Linux question:
Results 1 to 3 of 3
ALL: I have googled and binged for the last day with no success. I've got a C program that I've added some 'printf' statements to monitor a couple of variables. ...
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    19

    Redirecting output from printf statement in C Program

    ALL:

    I have googled and binged for the last day with no success.

    I've got a C program that I've added some 'printf' statements to monitor a couple of variables. When I run this program manually or from a script, the output is displayed on screen. However, I need to change various variables in the 'test.c' file, run 'make clean' and 'make' a few hundred thousand times. I'm using a script to read the variables in and then using sed to do in-place edits of the file. Unfortunately, with this amount of iteration, it is getting rather tired!

    Anyway, I've created a script that is working as long I respond to prompts. I've tried the following to no avail:
    Code:
    /path/to/script > /tmp/output
    /path/to/script > /tmp/output 2>&1
    /path/to/script | tee (no output even after adding the -a option)
    In my C program, I have the following 'printf' statement:
    Code:
    printf ("variable1: $s\nvariable2: $s",var1,var2);
    What am I missing? I've worked with redirection before and it's always worked out fine, but this one plain stumps!

    Help, Please!
    tk.

  2. #2
    Linux Engineer rcgreen's Avatar
    Join Date
    May 2006
    Location
    the hills
    Posts
    1,114
    Maybe the redirect needs to be written inside the script.

    Maybe I'm confused. This script edits the source file, compiles it
    and runs the executable? Put the redirect at the line where it
    executes the program.

  3. #3
    Just Joined!
    Join Date
    Mar 2007
    Location
    Melbourne, Australia
    Posts
    19
    Thanks rcgreen for you suggestion.

    Unfortunately, that did not work either. I've taken your suggestion further and modified the 'test.c' program to write directly to a file. However, that too appears to be an interesting one. The first time I ran the program, it worked and data appeared in the file. However, consequent runs have not resulted ion data output.

    What the script is doing is spawning the program, capturing the pid of the spawned process, then killing it (via SIGTERM) after a 20 second sleep. Could this be causing me the issue? Should I be using a different signal to let the program terminate gracefully?

    Cheers,
    tk.

Posting Permissions

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