LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   output to file (http://www.linuxquestions.org/questions/programming-9/output-to-file-4175421542/)

Balvinder87 08-11-2012 04:20 AM

output to file
 
#include <stdio.h>
main()
{
int i=0;
int j;
j =4;
printf("Value of i and j are %d %d\n",i, j);
}
Instead of getting i and j printed on the monitor , you can get it printed in the file as well using same printf function. What you need to do is you need to change your standard output to a file?

414N 08-11-2012 04:38 AM

Not sure if you're asking this, but you can redirect a program stdout on the shell by:
Code:

./program > outputFile.txt

silendo 08-11-2012 04:43 AM

Hello friend, you can change the std out in this way:

Code:

#include <stdio.h>
#include <stdlib.h>
main()
{
int i=0;
int j;
j =4;
fprintf(stdout,"Value of i and j are %d %d\n",i, j);
fprintf(stderr,"Value of i and j are %d %d\n",i, j);
}

In this way you can redirect the output into stderr or stdout.
If you want redirect the stdout into file, you must insert into fprintf the file descriptor.
For example. If *fp is a file descriptor, when you insert into fprintf(fp,".....");, it write output into file

Balvinder87 08-11-2012 05:03 AM

please elaborate the answer the concept is not clear

piyush.sharma 08-13-2012 12:23 AM

Quote:

Originally Posted by Balvinder87 (Post 4751583)
please elaborate the answer the concept is not clear

Which concept ? please try to write quote if you refer special post. We can't get whom you are asking to.

H_TeXMeX_H 08-13-2012 08:23 AM

There are two main ways to do this:

1) The more flexible option is stated by silendo above. Basically you print to stdout and redirect to a file in bash. This is the way I usually use.

2) Do it in C by opening a file and fprintf to the file instead of stdout.
http://www.cplusplus.com/reference/c...stdio/fprintf/


All times are GMT -5. The time now is 09:29 PM.