Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to capture stderr to a string in C 1

Status
Not open for further replies.

naru21

Technical User
May 24, 2002
13
US
Hi All,
Is there a way to read standard error.
I am working on embedded sql in c. some XXXX Function is returning error to standard error, How can i capture this
stderr to a string or to some temporary file?
I tried to redirect the output to a file but the error message is not found in the redirected file insted its still displaying on the console.

for ex ./testfile > test.out

Is there a way to capture/assign/write the stderr from a function to a string/file?

Thanks for your help.,

 
This is a shell question, not a C question.

[tt] ./testfile > test.out 2>errors.txt
[/tt]

--
 
Hi Salem,
Actually what i want is to return the stderr from a function in C to be assigned to a array or pointer string. Since its an inbuilt function in the package i dont know the syntax or funtionality of the funtion when ever there is an sql error it returns the verbage of the sqlerror to stderr, instead of displaying it to stderr i want to catch that error and assign it to a string.
When i am unable to do that i am trying to redirect to a file.
what i want is to assign the stderr returned by the function to sqlmsg
sqlmsg=isc_print_status(gds__status);
I appreciate any help in this matter.

 
Not my strong suit, but maybe you can to something with freopen.
Code:
#include <stdio.h>

int main ( void )
{
   const char filename[] = "file.txt";
   FILE *capture = freopen(filename, "w", stderr);
   if ( capture )
   {
      printf("stderr will be captured in \"%s\"\n", filename);
      /* ... */
      fputs("Help! I've fallen and I can't get up!", stderr);
      /* ... */
      fclose(capture);
      /* ... */
      capture = fopen(filename, "r");
      if ( capture )
      {
         char buffer [ BUFSIZ ];
         if ( fgets(buffer, sizeof buffer, capture) )
         {
            printf("buffer = \"%s\"\n", buffer);
         }
         fclose(capture);
      }
   }
   return 0;
}

/* my output
stderr will be captured in "file.txt"
buffer = "Help! I've fallen and I can't get up!"
*/
 
Yup This Works! Thanks a Bunch Dave!!
 
This might be an weird idea, but if it works, it would keep you from having to use a file...


Before calling the problem function, you could use [tt]dup[/tt] or [tt]dup2[/tt] to play around with file descriptors.

Get descriptor 2 (stderr) to point to a pipe that you open up with the [tt]pipe[/tt] system call, and save the old stderr as some other descriptor.

Then, call the function. It'll write its data to stderr, which is now the pipe.

Then, you can restore stderr to point to its real home.


Ok, I see a couple of problems with that. First, I don't know that a single process can even use a pipe like that. Second, if the error message happens to be too big, the process might end up blocking itself.

Both those problems can be solved by making a child process whose only purpose is to read from the pipe, store the message in a buffer, and then send it back through antoher pipe when it's done.


Anyway, it's probably a bad idea, and you already have a solution, but this may be worth looking at.
 
> sqlmsg=isc_print_status(gds__status);
I can't believe that the API is that badly broken such that the only way to get the error is to mess around intercepting the stderr stream.

Another good read of the manual pages seems in order.

--
 
I'm with salem here, though chippers insights on how
to use ipc(pipes) to grab and redirect IO for different purposes is the standard way of grabbing input from related processes and dealing with it in *nix.

There should be a strerror equivalent for this API
that allows you to write the translated error to a
buffer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top