Thread: Stdin question

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    10

    Stdin question

    I've been trying to make my program get its input from a file instead of typing everything myself.
    So I thought of using fprintf to write what I had in the file to the stdin, and I tought everytime scanf appeared in the code it would read what I wrote in stdin but the program worked as if nothing changed.
    Is it possible to do what I want with stdin or is there another way?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Instead of what you're doing, open your file (using fopen()) as an INPUT file, and replace your calls of scanf() with calls to fscanf(the_file, <rest of arguments>). Keep in mind that scanf(<arguments>) is functionally equivalent to fscanf(stdin, <arguments>);

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    So I thought of using fprintf to write what I had in the file to the stdin
    u cant do that. stdin is a standard pointer pointing to standard input stream. u get an error when u do that

    ssharish2005

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    int main ( ) {
      char buff[BUFSIZ];
      while ( fgets( buff, sizeof buff, stdin ) != NULL ) {
        fputs( buff, stdout );
      }
      return 0;
    }
    If you run this program from the command line as
    prog.exe
    then you would just type stuff in.

    Or you could run it like this, to get input from a file
    prog.exe < input.txt


    Code:
    int main ( int argc, char *argv[] ) {
      char buff[BUFSIZ];
      FILE *fp = fopen( argv[1], "r" );
      while ( fgets( buff, sizeof buff, fp ) != NULL ) {
        fputs( buff, stdout );
      }
      return 0;
    }
    You could run this program as
    prog.exe input.txt

    By changing just a few lines, you can flexibly read input from either stdin or from a file. Once the line of input is in 'buff', you can do whatever you want to it, with whatever functions you like.

    One advantage of sscanf() over scanf/fscanf is that it doesn't alter the state, so you can have several passes over the same buffer with sscanf().

    A good example is getting the user to type in a date in a flexible format (12/13/06 or Dec-13), and figuring out which format they chose.
    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.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    I don't think I explained my problem well, because I think it is a bit more complex.
    My program asks for a command from a user then uses a scanf to get the first letter and uses that letter as the variable for a switch.
    Inside the switch the program uses several scanfs to get the needed information from the buffer.
    What I want to do is a command that opens a file and execute the commands that are written in the file, with every command with \n in the end.
    I hope I was clear enough :|

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We're not going to write the whole damn thing for you. Get crackin' and produce your attempt.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I hope I was clear enough
    I thought I was - you should have no problem if you read the snippets of code I posted.

    I mean, just replace fputs(buff,stdout) with decodeMyCommandLine(buff) and you're almost there.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    10
    Quote Originally Posted by Salem
    > I hope I was clear enough
    I thought I was - you should have no problem if you read the snippets of code I posted.

    I mean, just replace fputs(buff,stdout) with decodeMyCommandLine(buff) and you're almost there.
    I guess I misunderstood your previous post. Now I understand that I have to copy what is in the file to a string and then get the information from the string.

    I guess I'll have to do some changes to the way the program runs, but nothing major.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. stdin question
    By audinue in forum C Programming
    Replies: 12
    Last Post: 01-11-2009, 02:57 AM
  3. stdin question
    By SourceCode in forum C Programming
    Replies: 1
    Last Post: 04-10-2003, 08:50 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. stdin question
    By oomen3 in forum C Programming
    Replies: 6
    Last Post: 02-19-2003, 02:52 PM