Solved

reading/writing to a file using getchar/putchar

Posted on 1997-11-29
7
495 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:cokeman_
7 Comments
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
Are you not allowed to go with fgetc() and fputc()?
0
 

Author Comment

by:cokeman_
Comment Utility
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Then can you dup(STDOUT_FILENO) before you freopen?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 5

Expert Comment

by:julio011597
Comment Utility
It is anyway a mess, since freopen() gives back a FILE *.
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
Maybe, if you tell what is this for, we can give some meaningful advice (well, at least, i need it).
0
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 200 total points
Comment Utility
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
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now