Solved

reading/writing to a file using getchar/putchar

Posted on 1997-11-29
7
500 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
ID: 1256448
Are you not allowed to go with fgetc() and fputc()?
0
 

Author Comment

by:cokeman_
ID: 1256449
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1256450
Then can you dup(STDOUT_FILENO) before you freopen?
0
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!

 
LVL 5

Expert Comment

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

Expert Comment

by:julio011597
ID: 1256452
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
ID: 1256453
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
ID: 1256454
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

Independent Software Vendors: 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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

685 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