Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading/writing to a file using getchar/putchar

Posted on 1997-11-29
7
Medium Priority
?
517 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_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

670 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