so I have this code on main:
fprintf(stderr, "ERROR");
then how do I know that in stderr contains ERROR?? I tried to do cat stderr but it doesn't work
so I have this code on main:
fprintf(stderr, "ERROR");
then how do I know that in stderr contains ERROR?? I tried to do cat stderr but it doesn't work
Typically stderr would also print to the console, like stdout.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Umm . . . stderr is much like stdout. It's a standard stream. By default, both streams output to the screen. However, you can redirect stdout to a file quite easily, leaving stderr still "pointing" to the screen.
An ordinary run of that will give this output:Code:#include <stdio.h> int main() { fprintf(stderr, "stderr\n"); printf("stdout\n"); /* same as fprintf(stdout, "stdout\n") */ return 0; }
But run it from the command line like this:Code:stderr stdout
and "stderr" will go to the screen while file.txt will contain "stdout".Code:program > file.txt
On Linux systems, you can usually redirect stderr with "2>". I'm not sure about Windows.
So, here's a sample session of some shell or another:
Code:$ cat program.c #include <stdio.h> int main() { fprintf(stderr, "stderr\n"); printf("stdout\n"); /* same as fprintf(stdout, "stdout\n") */ return 0; } $ gcc program -o program $ ./program stderr stdout $ cat output.txt $ ./program > output.txt stderr $ cat output.txt stdout $ ./program 2> output.txt stdout $ cat output.txt stderr $ ./program >output.txt 2>&1 $ cat output.txt stdout stderr $
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
stderr is the output stream for error logging I believe. Common errors such as this one get logged:
Most implementations simply display the stderr stream on the screen unless you redirect it. I believe redirection for stderr is indicated like this:Code:if ( (fp = fopen( filename, mode )) == NULL ) { fprintf( stderr, "Couldn't open: %s\n", filename ); }
cprog 2> mylog.txt
And then errors are no longer displayed on the screen but rather written to the file. The terminal opens and closes the stream for you, and will always empty previous versions of mylog.txt.
To append an existing file, you might try cprog 2>> mylog.txt
There's all sorts of shell specific jargon for this type of thing. You've probably caught on to the fact that you could open your own file and do error logging. I really don't see much harm in that, though it may be less convenient.
this may be unrelated but if I use a diff function and I get this error:
1c1
< usage: ./main [--binary] floatnumber
---
> usage: ./main [--binary] floatnumber
what does it mean??
It means that that line has changed. You probably added some whitespace or changed the newline ending of that line.
It's not an error. It's indicating that line one on the left (indicated by "<") has changed to line one on the right (">"). "1c1". 1 changed to 1.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
so what should I do to make this diff works
yes I did added a \n at that line, but if I remove that it will complaint of not finding a new line.. this is freakin me out
Look, it's not a problem. The diff is telling you that something has changed. You know that, because you changed it. It's not an error. It's not a problem. It's merely telling you that something changed.
diff tells you the differences between files. It doesn't give you errors that you have to deal with or anything.
As for why it complains about not finding newlines at the end of the file -- some UNIX tools work better with files that end with blank lines. GCC, for example, issues a warning if there's no newline at the end of the file.
It's not really that important. You can have it either way. But don't worry about diff outputs, they just tell you what you did.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
how about this one:
0a1,3
> ERROR: sizeof(int_u4) is not 4, it's 8
> ERROR: sizeof(int_4) is not 4, it's 8
> aborting
what does it mean??
"0a1,3": At line 0, lines from 1 to 3 have been added. (They don't exist in the left file, but they do in the right.) As far as I can tell. I don't use diff very often . . . .
Also see Wikipedia. http://en.wikipedia.org/wiki/Diff
Code:0a1,6 > This is an important > notice! It should > therefore be located at > the beginning of this > document! > 8,14c14 < compress the size of the < changes. < < This paragraph contains < text that is outdated. < It will be deleted in the < near future. --- > compress anything. 17c17 < check this dokument. On --- > check this document. On 24a25,28 > > This paragraph contains > important new additions > to this document.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.