0

I'm trying to make a C program which can take both stdin and files then prints it to stdout.
This program will have an option parser to manage a defined set of options :
i.e: -x, -y, -z / or combined: -zx

Let's say we have the following functions :
- get_stdin -> get standard input and put it on stdout
- get_file -> get file contents and put it on stdout

How would one write this following pseudocode ?

ft_array =
[
    ( STDIN, &get_stdin );
    ( FILE, &get_file );
]
Jay
  • 179
  • 1
  • 5
  • 18
  • 5
    Hi Jean-Baptiste ETNA wouldn't be happy to see you asking for your homework here, no ? think about it ;) – Yann Jun 25 '14 at 09:55
  • 4
    Post the C code you have this far and point out what part you have trouble with. – Lundin Jun 25 '14 at 09:58
  • Hey @Yann, I'm just trying to understand how to use an array of function pointers. I want to code my project myself of course. I'll post some code soon. – Jay Jun 25 '14 at 11:34
  • No problem with that, so your answers are [THERE](http://stackoverflow.com/questions/252748/how-can-i-use-an-array-of-function-pointers) and [THERE](http://stackoverflow.com/questions/5488608/how-define-an-array-of-function-pointers-in-c). – Yann Jun 25 '14 at 11:39
  • So far I've got 2 functions. 1) gets the stdin, puts it on stdout. 2) gets contents of file, puts it on stdout. I'm now trying to see how to know when a file is given or none in which case I shall get stdin. I'm trying my hand at it with http://stackoverflow.com/questions/3495092/read-from-file-or-stdin-c – Jay Jun 25 '14 at 14:18
  • 2
    In your simple case you don't need function pointers. stdin is just a file, so in the init phase you can either open a file or take stdin and then you can proceed with the same code just reading from the file. – Werner Henze Jun 25 '14 at 14:58
  • `stdin` really is a `FILE *`. http://www.cplusplus.com/reference/cstdio/stdin – diapir Jun 25 '14 at 15:03
  • It does not matter if the input is stdin (keyboard) or the file. Write the invokation/command line to re-direct stdin to the file when using a file for input. I.E. execname < filename. – user3629249 Jun 26 '14 at 06:20

0 Answers0