Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Let suppose I am make a program of adding 2 numbers and giving input from the command line
as
c://>program_name 12 13

How can i proceed so that output of my program must go to stdout and error messages must go to stderr?

int main(int argc, char *argv[])
{    
                   
      int i=argv[1];
      string j=argv[2];
      cout<<i+j;
      getch();
      return 0;
}
Posted
Updated 28-Aug-11 18:16pm
v3

1 solution

cin reads from stdin
cout writes to stdout
cerr writes to strerr

C++
string input;
cin >> input; // Read from stdin
cout << "Write to stdout";
cerr << "Write to stderr";


Is stdout and stderr used in c++?
Yes, like I said cout and cerr writes to those.

by using fprintf() or stuff like that by taking streams?
Ifyou want to use fprintf then you just pass the stream you want to write to as the first parameter:
C++
#include <stdio.h>
fprintf(stdout, "Writing to stdout");


FILE* stdout, stderr, stdin are defined in stdio.h
 
Share this answer
 
v5
Comments
Trick7 28-Aug-11 22:47pm    
I got u but can u please write some code so that i can have some better understanding
Simon Bang Terkildsen 28-Aug-11 22:50pm    
well you already did that yourself in the code you posted, but I've updated the answer to include sample code.
Trick7 28-Aug-11 22:54pm    
Is stdout and stderr used in c++????
by using fprintf() or stuff like that by taking streams???
Simon Bang Terkildsen 28-Aug-11 23:02pm    
updated the answer
Trick7 28-Aug-11 23:09pm    
c://>program_name 12 13

//header files included....
int main(int argc, char *argv[])
{
FILE* stdout, stderr;
int i=argv[1];
string j=argv[2];
int p=i+j;
int fprintf(stdout, "%d\n", p);
getch();
return 0;
}

but error is coming........
help........

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900