sasha sami
sasha sami

Reputation: 525

replacing stdout and stdin with a file pointer in c++?

I have a sandbox code written in c.It has the following line in it: msb.sbox.task.ofd = STDOUT_FILENO;. That is output for the executable is written to standard output.I want to change this so the output is written to a file instead.I tried doing this

  FILE * fp;
  fp=fopen("myf","w");
  msb.sbox.task.ofd=fp;

But this gives the warning warning: assignment makes integer from pointer without a cast [enabled by default].How do i go about it ??

Upvotes: 2

Views: 1216

Answers (2)

nouney
nouney

Reputation: 4411

Try this:

  FILE * fp;
  fp=fopen("myf","r");
  msb.sbox.task.ofd=fileno(fp);

STDOUT_FILENO is an int, and fp is a pointer to a FILE struct, so your assignment isn't correct. fileno will return the file descriptor (the int value) from a pointer to a FILE struct, that what is fp.

Upvotes: 5

Captain Obvlious
Captain Obvlious

Reputation: 20073

If looks like the structure member is expecting a file id number rather than a pointer to a streamed file buffer. Use fileno to acquire the id from FILE*.

#include <stdio.h>

FILE * fp;
fp=fopen("myf","r");
msb.sbox.task.ofd=fileno(fp);

Upvotes: 4

Related Questions