Thread: Get output to string

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    21

    Get output to string

    Hi,

    I have the following code in C (console app):

    Code:
    int main(int argc, char *argv[])
    {
    
        FILE *fp;
        char path[1035];
    
        fp = _popen(argv[1], "r");
        if (fp == NULL) {
            printf("Failed to run command\n");
            exit(1);
        }
    
        while (fgets(path, sizeof(path) - 1, fp) != NULL) {
            printf("%s", path);
        }
    
        _pclose(fp);
    
        return 0;
    }
    The app, as you can see, runs an executable, and prints the output.

    What I am trying to do is to gather the output in a string (char).

    I had no luck so far; I've tryed to create a buffer - char buffer[1024] - and append to it the results - strcat(buffer,path) - in the while loop instead of printing - printf("%s", path)...

    Any ideas are welcomed...

    Cheers

    JKepler

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by jkepler
    I had no luck so far; I've tryed to create a buffer - char buffer[1024] - and append to it the results - strcat(buffer,path) - in the while loop instead of printing - printf("%s", path)...
    That wouldn't work, not because the idea is wrong, but because of the implementation details: if you have a char array of 1024 characters, it can at most store a null terminated string of length 1023, but on each iteration of fgets you are reading into a buffer of 1035 characters, i.e., even on the first iteration you could read a string that is too long for your final buffer.

    Perhaps you should consider doing dynamic memory allocation, e.g., to realloc on each iteration, or to keep track of the number of chars in use and the number of chars allocated and realloc by a factor when the number of chars in use is about to exceed the number of chars allocated.

    By the way, using strcat to append in a loop can be inefficient, i.e., if you always provide a pointer to the start of the array, strcat must search the array for the first null character in order to append, which gets slower and slower as the string gets longer and longer. On the other hand, if you keep track of the string length, you can use strcat to append directly to the end of the string, which would be efficient.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-03-2010, 08:47 AM
  2. String output
    By heredia21 in forum C++ Programming
    Replies: 2
    Last Post: 02-08-2010, 10:42 PM
  3. output string
    By rybo7g in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2007, 05:11 PM
  4. output a string to a standard output
    By sh4k3 in forum C Programming
    Replies: 3
    Last Post: 06-15-2007, 05:59 AM
  5. need help with string output
    By stimpyzu in forum C++ Programming
    Replies: 6
    Last Post: 04-23-2002, 08:49 AM

Tags for this Thread