Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

how to capture stderr to a string in CHelpful Member! 

naru21 (TechnicalUser) (OP)
4 Jan 05 20:17
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.,

Salem (Programmer)
4 Jan 05 22:07
This is a shell question, not a C question.

./testfile > test.out 2>errors.txt

--

naru21 (TechnicalUser) (OP)
5 Jan 05 10:26
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.

Helpful Member!  DaveSinkula (Programmer)
5 Jan 05 11:09
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!"
*/
naru21 (TechnicalUser) (OP)
5 Jan 05 12:16
Yup This Works! Thanks a Bunch Dave!!
chipperMDW (Programmer)
5 Jan 05 19:11
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 dup or dup2 to play around with file descriptors.

Get descriptor 2 (stderr) to point to a pipe that you open up with the pipe 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.
Salem (Programmer)
6 Jan 05 3:00
> 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.

--

marsd (IS/IT--Management)
6 Jan 05 22:03
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.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close