?
Solved

problem with redirecting stdout to a new file

Posted on 2009-04-26
5
Medium Priority
?
379 Views
Last Modified: 2012-05-06
Hey, I'm trying to redirect the stdout to a new file, like, simulating echo hi > newfile.txt.

But the thing is, my program's doing something weird with it, I asked the question a few days back, http:/Q_24347815.html , but now it doesn't seem to be working...

Appreciate any advice, thanks in advance!!
printf("works %d\n", case_num);
      pid = fork();
 
      if(pid < 0)
        perror("fork");
      else if(pid == 0){
        fid = open(fourth_string, O_CREAT|O_WRONLY|O_TRUNC, 0666);
        if(fid < 0)
          perror("fid");
        close(1);
        ret = dup2(fid, fileno(stdout));
        if(ret < 0)
          perror("ret");
        close(fid);
        fprintf(stdout, "test\n");
      }
      else if(pid > 0){
        case_num = 0;
        wait(NULL);
      }

Open in new window

0
Comment
Question by:errang
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 24237748
when I set
char * fourth_string="Q_24356764.out";
and run that program, I get a file Q_24356764.out containing
test
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 2000 total points
ID: 24237778
I don't think there is anything wrong with this specific code.

One comment, you don't need to call "close(1);" before the call to dup2(). dup2() will take care of the filenumber 1.

I created this standalone test sample from your code, and it works fine.

#include <fcntl.h>
#include <stdio.h>
 
int main() {
   printf("forking\n");
   int pid = fork();
 
   if(pid < 0)
        perror("fork");
   else if(pid == 0) {
       int fid = open("out.log", O_CREAT|O_WRONLY|O_TRUNC, 0666);
       if(fid < 0)
          perror("fid");
       int ret = dup2(fid, fileno(stdout));
       if(ret < 0)
          perror("ret");
       close(fid);
       fprintf(stdout, "test\n");
   }
   else if(pid > 0) {
       wait(NULL);
   }
}

Open in new window

0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24237782
Oops, sorry ozo did not see your response. I also tested the sample standalone.
0
 

Author Comment

by:errang
ID: 24237812
huh... it used to print out the prompt for me... so how would I make it so that it would print everything between echo and >?

Do I need to use a dynamic array to make that happen?

like...

char somestring[][80];

somestring[0] = malloc (size of (80 * char)); ??

and replace that with fprintf(stdout, "test\n");?

And what about stderr? do I just use close(2) and dup will take care of the rest?
0
 

Author Comment

by:errang
ID: 24237822
>> and replace that with fprintf(stdout, "test\n");?

sorry, replace fprintf(stdout, "test\n); with frpintf(stdout, somestring);
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question