Thread: Standard input way?

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    Standard input way?

    I'm making a program to be opened this way:

    $ proj1 <in.file> out.file
    How can i read the in.file using the stdin parameter?
    How can i get the out.file name to create that output file?

    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You don't need to do anything at all.

    fgets( buff, sizeof buff, stdin );
    will read a line from the file.

    printf( "hello world\n" );
    would write to the output file.

    Redirections are handled by the shell, not your program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by Salem View Post
    You don't need to do anything at all.

    fgets( buff, sizeof buff, stdin );
    will read a line from the file.

    printf( "hello world\n" );
    would write to the output file.

    Redirections are handled by the shell, not your program.
    Are you serious? Each printf stays automatically saved in the output file without me making a fopen() to it? That's great.


    But to read the inputfile using getc(stdin) how can i do it?

    Thanks!

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    just do it...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by vart View Post
    just do it...
    but its for college, my teacher says that i need to use getc(stdin) to receive the file, i know that are other ways but that is a must unfortunatly

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    but its for college, my teacher says that i need to use getc(stdin) to receive the file, i know that are other ways but that is a must unfortunatly
    So? As I said - just use it. No difference from the fgets approach Salem suggested... Instead of reading lines - read char by char

    Code:
    int c;
    while((c=getc(stdin)) != EOF)
    {
       process char here
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Banned
    Join Date
    Mar 2008
    Posts
    78
    ok thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. Standard Input Trouble
    By c_323_h in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2006, 02:14 PM
  3. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 06:16 AM
  4. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM