Sponsored Content
Top Forums Programming C, UNIX: How to redirect 'stdout' to a file from a C code? Post 302983598 by alex_5161 on Thursday 13th of October 2016 04:19:09 PM
Old 10-13-2016
Thank you, jim mcnamara, it is perfectly helps!
Sure here is more than I need in my case, but everything is clear and strait forward!
Additionaly, your code keep a way to switch back that not needed right now, but could be a task later on such approach!

Also I've found useful (by another reply) the 'freopen()' C-function and did it in very simple way, too.
(... for anybody else with the same task and for myself later, here is how I did it with freopen() ) :
Code:
#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

void redir(char* fl_nm)
{
  freopen(fl_nm,"a",stdout);
}

int main()
{
   cout << "starting - by cout\n";
   printf(" this written by printf()\n Following messages shoul go to the file '/tmp/tst_redir.log'\n");

   redir((char*)"/tmp/tst_redir.log");

   cout << ".. By 'cout' after using the redir(): this should be written to the file\n";
   printf(" .. By printf() after 'redir()'...\n");

  return 0;
}

But, again, thanks for your solution!!
 

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

problem with redirect stdout to file

Hi all hope you can help as I am going MAD!!! :eek: The below is in a shell script but the redirection in the sed line does not work and outputs to the screen and the $fname_2 does note get created ????? Can any one help ?? #!/bin/ksh cd /app/ for fname in `ls -1 X*` do sed 1d $fname... (3 Replies)
Discussion started by: mlucas
3 Replies

2. UNIX for Advanced & Expert Users

STDOUT redirect to a FILE, when fuser command is used !!

Hi all, I have the following script: ------------------------------------------------- #SCRIPT TO CHECK WHO HAS ACCESSED THE LOG/FILE IN PAST 'N' MINUTES, AND MAIL ACCORDINGLY. MYPATH="/clocal/mqbrkrs/user/mqsiadm/sanjay/" MAIL_RECIPIENTS="vg517@dcx.com" Subject="File accessed in last... (6 Replies)
Discussion started by: varungupta
6 Replies

3. Shell Programming and Scripting

How to redirect stderr and stdout to a file

Hi friends I am facing one problem while redirecting the out of the stderr and stdout to a file let example my problem with a simple example I have a file (say test.sh)in which i run 2 command in the background ps -ef & ls & and now i am run this file and redirect the output to a file... (8 Replies)
Discussion started by: sushantnirwan
8 Replies

4. Programming

redirect stdout

hello again! i use dup2 to redirect stdout. I run what i want, now i want undo this redirection. how can i do that? thanx in advance (7 Replies)
Discussion started by: nicos
7 Replies

5. Shell Programming and Scripting

Redirect stdout/stderr to a file globally

Hi I am not if this is possible: is it possible in bach (or another shell) to redirect GLOBALLY the stdout/stderr channels to a file. So, if I have a script script.sh cmd1 cmd2 cmd3 I want all stdout/stderr goes to a file. I know I can do: ./script.sh 1>file 2>&1 OR ... (2 Replies)
Discussion started by: islegmar
2 Replies

6. Shell Programming and Scripting

redirect STDOUT to a file in a subshell

Hi, I would like to avoid re-directing line by line to a file. What is the best way to re-direct STDOUT to a file in a subshell? Thanks in advance. Cheers Vj (1 Reply)
Discussion started by: tnvee
1 Replies

7. Shell Programming and Scripting

redirect stdout and stderr to file wrong order problem with subshell

Hello I read a lot of post related to this topic, but nothing helped me. :mad: I'm running a ksh script with subshell what processing some ldap command. I need to check output for possible errors. #!/bin/ksh ... readinput < $QCHAT_INPUT |& while read -p line do echo $line ... (3 Replies)
Discussion started by: Osim
3 Replies

8. UNIX for Dummies Questions & Answers

STDOUT redirect to file and format problems

Hi All, I am using centOS. When I try to redirect STDOUT to a file, it ends up in getting some funny characters. For example ... STDOUT of the command as follows. $ ls H3k27me3 H3k36me3 H3k4me1 H3k4me2 H3k4me3 H3k9ac H4k20me1 $ ls >test $ cat test ^ (1 Reply)
Discussion started by: Chulamakuri
1 Replies

9. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.logBut during script execution I would like the output come back again to screen, how to do that? Thanks Lucas (4 Replies)
Discussion started by: Lord Spectre
4 Replies

10. Shell Programming and Scripting

Redirect STDOUT & STDERR to file and then on screen

Dear all, redirecting STDOUT & STDERR to file is quite simple, I'm currently using: Code: exec 1>>/tmp/tmp.log; exec 2>>/tmp/tmp.log But during script execution I would like the output come back again to screen, how to do that? Thanks Luc edit by bakunin: please use CODE-tags like the... (6 Replies)
Discussion started by: tmonk1
6 Replies
FOPEN(3)						   BSD Library Functions Manual 						  FOPEN(3)

NAME
fopen, fdopen, freopen -- stream open functions LIBRARY
Standard C Library (libc, -lc) SYNOPSIS
#include <stdio.h> FILE * fopen(const char * restrict path, const char * restrict mode); FILE * fdopen(int fildes, const char *mode); FILE * freopen(const char * restrict path, const char * restrict mode, FILE * restrict stream); DESCRIPTION
The fopen() function opens the file whose name is the string pointed to by path and associates a stream with it. The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.): ``r'' Open for reading. ``r+'' Open for reading and writing. ``w'' Open for writing. Truncate file to zero length or create file. ``w+'' Open for reading and writing. Truncate file to zero length or create file. ``a'' Append; open for writing. The file is created if it does not exist. ``a+'' Append; open for reading and writing. The file is created if it does not exist. Additionally: o The mode string can also include the letter ``b'' either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ANSI X3.159-1989 (``ANSI C89'') and has no effect; the ``b'' is ignored. o The letter ``f'' in the mode string restricts fopen() to regular files; if the file opened is not a regular file, fopen() will fail. This is a non ANSI X3.159-1989 (``ANSI C89'') extension. o The letter ``e'' in the mode string sets the close-on-exec flag in the file descriptors of the newly opened file files; if the opera- tion fails, fopen() will fail. This is a non ANSI X3.159-1989 (``ANSI C89'') extension. o The letter 'x' in the mode turns on exclusive open mode to the file ( O_EXCL) which means that the file will not be created if it already exists. Any created files will have mode "S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH" (0666), as modified by the process' umask(2) value. Opening a file with append mode causes all subsequent writes to it to be forced to the then current end of file, regardless of intervening repositioning of the stream. The fopen() and freopen() functions initially position the stream at the start of the file unless the file is opened with append mode, in which case the stream is initially positioned at the end of the file. The fdopen() function associates a stream with the existing file descriptor, fildes. The mode of the stream must be compatible with the mode of the file descriptor. The stream is positioned at the file offset of the file descriptor. The freopen() function opens the file whose name is the string pointed to by path and associates the stream pointed to by stream with it. The original stream (if it exists) is closed. The mode argument is used just as in the fopen() function. The primary use of the freopen() function is to change the file associated with a standard text stream (stderr, stdin, or stdout). RETURN VALUES
Upon successful completion fopen(), fdopen() and freopen() return a FILE pointer. Otherwise, NULL is returned and the global variable errno is set to indicate the error. ERRORS
The functions may fail if: [EFTYPE] The file is not a regular file and the character ``f'' is specified in the mode. [EINVAL] The specified mode was invalid. The fopen(), fdopen() and freopen() functions may also fail and set errno for any of the errors specified for the routine malloc(3). The fopen() function may also fail and set errno for any of the errors specified for the routine open(2). The fdopen() function may also fail and set errno for any of the errors specified for the routine fcntl(2). The freopen() function may also fail and set errno for any of the errors specified for the routines open(2), fclose(3) and fflush(3). SEE ALSO
open(2), fclose(3), fileno(3), fseek(3), funopen(3) STANDARDS
The fopen() and freopen() functions conform to ANSI X3.159-1989 (``ANSI C89''). All three functions are specified in IEEE Std 1003.1-2008 (``POSIX.1''). CAVEATS
Proper code using fdopen() with error checking should close(2) fildes in case of failure, and fclose(3) the resulting FILE * in case of suc- cess. FILE *file; int fd; if ((file = fdopen(fd, "r")) != NULL) { /* perform operations on the FILE * */ fclose(file); } else { /* failure, report the error */ close(fd); } BSD
November 14, 2012 BSD
All times are GMT -4. The time now is 09:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy