Standard input way?

This is a discussion on Standard input way? within the C Programming forums, part of the General Programming Boards category; I'm making a program to be opened this way: $ proj1 <in.file> out.file How can i read the in.file using ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    just do it...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 03: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, 05: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, 12:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21