last day (15 days later) » 

3:58 PM
0
Q: redirect stderr to std::string in c++ under Windows

user3210625I want to redirect stderr to a string under Windows 7 in C++. This is the code I have come up with so far. (from different sources on the Web) #include <windows.h> #include <streambuf> #include <iostream> #include <io.h> #include <sstream> #include <fstream> #include <stdlib.h> #include <stdio.h>...

 
the solution in redirect stdout/stderr to a string unfortunately does not compile under Windows
 
Oh, what's the error?
 
The error is that unistd.h seems not to be available under Windows. I used VS Community 2019.That' s why I emphasized Windows in the heading for this question. (Apart from that I was looking for a simpler solution)
 
@user3210625 You don't need unistd.h to implement your own version of the accepted answer. It uses C++ standard mechanisms only.
If you try this, will it work? It's just a quick example and not very well designed.
 
@Ted Lynmgo When I compile the example without unistd.h I get 26 errors like e.g. Fehler (aktiv) E0020 Der Bezeichner ""close"" ist nicht definiert. which in English is "identifier not defined"
 
3:58 PM
@user3210625 You are then not using the accepted answer in the duplicate we linked to. Did you try the example I made here?
 
@Ted Lyngmo I tested it, but it did not compile (gave me 42 errors)
 
@user3210625 Interesting, will try it when I get home. Did you also try using the accepted answer in the duplicate link?
 
@Ted Lyngmo In fact I tried all the answers there before I asked this question (otherwise I probably would not have aked, except for the last which I tested in the meantime: it gave me no errors on compiling but also no success.
 
@user3210625 The example I pasted on godbolt compiles without warnings or errors and runs fine in VS2019 (16.1.3). What was the first error you got when trying to compile my example?
 
@Ted Lyngmo I pasted the complete errorlog above
 
3:58 PM
@user3210625 Project\codbolt properties\Configuration Properties\C/C++\Language\C++ Language Standard is set to something older than C++17 (like default or C++14), right? Try C++17 or Preview. I''ve used the C++17 string_view in the example and possibly some other C++17 features too. Btw, where are you creating the file data? Are you expecting to capture stdout/stderr from the curl library?
 
@Tedd Lyngmo With C++ 17 it worked. The whole purpose of my question is to capture the error curl outputs to stderr into a string so that I can output it in a reference parameter of function which I do for all errormessages in this project the code will be part of,
 
@user3210625 Ah ... then I think it'll be easier than to try to redirect the standard streams. I've haven't worked with libcurl before, but will give it a try. See you in a few hours :-) ... it's really a completely different question. "How do I capture libcurl output to a variable?" or something along those lines.
@user3210625 Got it working I think. I don't have libcurl on my windows machine so there may be some quirks to sort out, not sure. It has a lot of warnings since I don't use many of the variables I've declared in the callbacks, but I can collect both the requested document and the protocol stuff into separate std::strings.
@user3210625 How does this look on your machine?
https://pastebin.com/Wf2CL3qC
The "char buffer[...];" I declared was just when I was testing, it's not used so remove that.
 
4:18 PM
You probably also want CURLOPT_FOLLOWLOCATION set to 1L. Be aware of when capturing the debug output, you'll get SSL certificates as binary blobs and they will look aweful if priinted directly to screen I just noticed. You may also want CURLOPT_HTTPPROXYTUNNEL to make it work through proxies etc. It really has a ton of options :-)
 
4:33 PM
@Tedd Lyngmo cYour code from pastebin runs perfect after removing that char buffer
 
@user3210625 Lovely! I've edited your question and proposed to open it again so I can make a formal answer.
 
curl has indeed more options than I need. I need it just to get a list of timers from a German DVB recording software which provides the timers via a web api interface to get them parsed via an xml-parser at the end
 
I see, yes, curl looks really powerful. Strange that I've never used it before myself. :-)
 
what I wondered was why my my datafile to which I redirected the stderr in my first code was empty because when I let stderr be printed on screen (without redirection ) it printed stderr perfectly, so there was stderr, but not in datafile..btw..thexample with datafile stems from the MS documentation of the function dup
 
I didn't read so much about it but it seems you can redirect to a datafile (by supplying a FILE*). You had a std::string data and then tried to open a file named "data" (if I read it correctly) That won't work.
 
4:55 PM
ok, btw: sorry fot my typing so slow and misprints - I have my arm in plaster
I see..I took it directly from here (docs.microsoft.com/de-de/cpp/c-runtime-library/reference/…) and adopted it uncorrectly
that was actually the part in the code I got stuck with, I thought that you have to open the DataFile for writing in order for the redirection t properly work
 
No worries! Hope your arm heals fast. Ok, you could probably have succeeded in redirecting to a file instead and then reading it, but it's probably more elegant if you can keep it all in memory instead (unless you get really HUGE documents).
 
5:15 PM
no, it's not a huge document, and it would be perfect to keep it in memory..I also tried the very first solution in the othe SO question...I really digged into the answers before asling mine..but strangely enough the first solution gave me an empty string
so the solution which uses the rdbuf to get the string via a stringstream if I understood that correctly..I'm realyy not that good at C++ I admit .-(
In the end I want a function that gets the url, and stores the errormessages, if curl did not succee, in a reference variable, so when I use the function I get stderr in this variable and can process it further..because if understand the curl ducumentation correctly curl does not always populate its custom errorbuffer with a errormessage..so I would like to have the stderr as well
 
I see. Well, it's worth digging deeper into the options I guess. I'm looking into encapsuling the curl calls in some C++ classes for easier handling now. It feels like someone must have done that before. I'd better check that out before doing too much work for no reason :-)
 
.-)
:-)
 
5:58 PM
I found some - but not in the libcurl distribution, so I'll continue a little. It doesn't look anything like the code you got from me right now :-D
 
no problem thnak you very muh
 
You are welcome. This is fun :-)
 
 
1 hour later…
7:34 PM
First draft: https://pastebin.com/nRG6uPrw

Would you mind checking if that works in Windows? I can't ...
 
7:44 PM
no prob, it compiles perfect and gives a lot of output to the screen
I ignore the green lines that VS puts in some lines
 
8:42 PM
Fantastic! Thanks! Wonder what those green lines might indicate though :-) Do you get any indication?
 

last day (15 days later) »