LinuxQuestions.org
Help answer threads with 0 replies.
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 10-16-2015, 08:21 AM   #1
srinietrx
Member
 
Registered: May 2013
Posts: 101

Rep: Reputation: Disabled
Output in Log file instead of Standard Input Output


I am developing C++ application.As size of the code increases, I feel my printf output needs to go to some file instead of stdio.

Is there is any standard class or I need to write code from scratch?

Last edited by srinietrx; 10-16-2015 at 08:29 AM.
 
Old 10-16-2015, 09:06 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
If it is about error/debug-messages, they could be sent to stderr (cf: fprintf), to separate them from the normal output. stderr can be redirected from shell:
Code:
./myexe 2>messages.log
 
Old 10-16-2015, 09:33 AM   #3
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Or... if you simply want to write to a FILE instead of to stdout,
Code:
#include <fstream>
Sort of like this:
Code:
#include <iostream>
#include <fstream>
using namespace std;

int main() 
{
    ofstream log;
    log.open("cpp.log", ios::app);
    log << "Log entry here.\n";
    log.close();

    return 0;
}
So after executing the code above (named file_write):
Code:
./file_write
...three times, I get the following content in the file cpp.log:
Code:
cat cpp.log 
Log entry here.
Log entry here.
Log entry here.
But maybe that was too simple?

Best regards,
HMW

PS. Haven't used c++ in AGES. Please be gentle! DS.

Last edited by HMW; 10-16-2015 at 09:36 AM.
 
Old 10-18-2015, 09:34 AM   #4
srinietrx
Member
 
Registered: May 2013
Posts: 101

Original Poster
Rep: Reputation: Disabled
Thanks.I want similar to what HMW has written.
 
  


Reply



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
[SOLVED] stty: standard input: Input/output error [bash] DoME69 Programming 7 05-22-2015 12:14 AM
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
Connecting both standard input and output to another program so that they can interac eantoranz Linux - Software 7 06-28-2009 07:03 PM
utf , standard output / input and grep too Fascistchicken Linux - Software 2 11-05-2004 08:52 AM
How to redirect standard output of piped command to log file andrewstr Linux - General 10 02-04-2004 02:07 PM

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

All times are GMT -5. The time now is 11:41 PM.

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