Thread: system command's output

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    36

    system command's output

    i want to save a system command's output in a file without displaying it on screen. i thought of defining stdout to the file i want and haven't tested it yet. Any other ideas?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    thanx

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    36
    I have written this. It compiles fine, when i run it
    the info file is created and hello world is written in the
    file but then i get the "this program has performed an illegal
    operation" error.
    Error while opening file is not printed.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void copyFile (FILE * input, FILE * output)
    {
        int c;
        while ( (c=getc(input)) != EOF)
            putc(c,output);
    }
    
    
    int main ()
    {
        FILE * pr;
        system("hello > c:\\info"); //hello just prints hello world
        if ((pr = fopen("r","c:\\info")) == NULL)
    	fprintf(stderr, "Error while opening info file");
        copyFile(pr, stdout);
        return 0;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Also, checkout the (non-standard) popen() or _popen() function if you have it. It might be of some use to you, depending on what you're trying to accomplish.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. retrieve output from system()
    By mmongoose in forum C++ Programming
    Replies: 5
    Last Post: 12-15-2005, 06:44 AM
  2. system commands. deletion of file after program terminates
    By xddxogm3 in forum C++ Programming
    Replies: 12
    Last Post: 11-24-2003, 10:41 PM
  3. System Commands Output
    By nasir_qau in forum Linux Programming
    Replies: 2
    Last Post: 03-21-2002, 03:14 PM
  4. string variables and system commands part 2
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-25-2001, 12:57 AM
  5. string variables in system commands
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-24-2001, 12:36 PM