LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2012, 04:20 AM   #1
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77
Blog Entries: 1

Rep: Reputation: Disabled
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?
 
Old 08-11-2012, 04:38 AM   #2
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 647

Rep: Reputation: 189Reputation: 189
Not sure if you're asking this, but you can redirect a program stdout on the shell by:
Code:
./program > outputFile.txt
 
Old 08-11-2012, 04:43 AM   #3
silendo
Member
 
Registered: Jun 2012
Location: Italy
Distribution: Slackware 13.37
Posts: 45

Rep: Reputation: Disabled
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
 
Old 08-11-2012, 05:03 AM   #4
Balvinder87
Member
 
Registered: Jun 2012
Location: India
Distribution: debian
Posts: 77

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
please elaborate the answer the concept is not clear
 
Old 08-13-2012, 12:23 AM   #5
piyush.sharma
Member
 
Registered: Jul 2012
Location: Delhi, India
Distribution: CentOS
Posts: 82

Rep: Reputation: Disabled
Quote:
Originally Posted by Balvinder87 View Post
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.
 
Old 08-13-2012, 08:23 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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/
 
  


Reply

Tags
file



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
using grep to output non-matches into output file binny959 Linux - General 5 12-24-2010 11:20 PM
Output Redirection - Trying to output to screen and file? helptonewbie Linux - Newbie 7 03-19-2009 07:05 AM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration