• C

using fprintf to write to a file...

Ive inherited some code which I have modified and it works fiine!

Essentially, it reads in a stream from stdin, does stuff and writes it out to a new user defined stream.
Ive used streams just because I thought it would give me most flexibility for future development.

I wrapped my C binary in a shell wrapper and redirected my stream output by using  binary_name 6>file.out

For operational reasons I need to change that so that instead of sending my fprintf to my stream, I send it straight to the file.

That wouldnt seem to be an insummountable problem but I cant seem to do it!!

Can anyone help?  I thought it should be easy for me to change fprintf(STREAM,string) to fprintf(FILE,string) because I thought an open file took a stream as its input anyway, so in essence I should just be able to somehow point my stream to the file?

Julian

stummjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

stummjAuthor Commented:
If anyone needs to chat to get clarifications I am on msn right now julian@stumm.me.uk
0
jkrCommented:
What about using 'freopen()', e.g. like

/* FREOPEN.C: This program reassigns stderr to the file
 * named FREOPEN.OUT and writes a line to that file.
 */

#include <stdio.h>
#include <stdlib.h>

FILE *stream;

void main( void )
{
   /* Reassign "stderr" to "freopen.out": */
   stream = freopen( "freopen.out", "w", stderr );

   if( stream == NULL )
      fprintf( stdout, "error on freopen\n" );
   else
   {
      fprintf( stream, "This will go to the file 'freopen.out'\n" );
      fprintf( stdout, "successfully reassigned\n" );
      fclose( stream );
   }
   system( "type freopen.out" );
}
0
avizitCommented:
show what was the original code and what you changed it to ..

only the modified part should be sufficient I thin ....
0
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

stummjAuthor Commented:
The original code just wrote to the stdout stream. I just copy and pasted really, and wrote to my custom stream instead.
0
avizitCommented:
say you had

fprintf(stdout, "xyz");

you can put the following code in its place

File *fp;

fp = fopen("newfile.txt",  "w" );
if(fp == NULL ) { print error and exit or whatever suits you }

fprintf(fp, "xyz");

fclose(fp);   // when yuo are done close the file
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stummjAuthor Commented:
Thanks both.

I simply had to change:

stream=fdopen(descriptor,type)

to

stream = fopen(filename,type)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.