• C

reading/writing to a file using getchar/putchar

How would I copy a file using getchar and putchar?  I have to use those function calls.  As far as I can tell, those functions read and write from/to stdin and stdout.  The only way I have figured so far, is to use freopen to open the files.  However, I need to write to the screen after I have finished copying the file.  If I were to use freopen, how can I tell it to stop using the file as stdout?  Or, if you could suggest a way other than freopen, what would that be? I can not use function calls like fgetc or fputc.  I have to use getchar and putchar.
cokeman_Asked:
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.

julio011597Commented:
Are you not allowed to go with fgetc() and fputc()?
0
cokeman_Author Commented:
Edited text of question
0
ozoCommented:
Then can you dup(STDOUT_FILENO) before you freopen?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

julio011597Commented:
It is anyway a mess, since freopen() gives back a FILE *.
0
julio011597Commented:
Maybe, if you tell what is this for, we can give some meaningful advice (well, at least, i need it).
0
Tommy HuiEngineerCommented:
Well, if you must use getchar and putchar, then the only thing you can change is what stdin and stdout is pointing to. In this case, you will need to use redirect stdin and stdout using the command line shell:

  myprog <inputfile >outputfile

This will cause your program (myprog) to be executed with the stdin of inputfile and stdout with outputfile.

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
ozoCommented:
That's certainly the easiest way, but if you don't have that option,
you can still do in your program the same thing that the shell does
when it redirects your stdin and stdout.
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.