Thread: reading from stdin

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    reading from stdin

    Hi,

    I'm trying to use a software at:ftp://ftp.toulouse.inra.fr/pub/multalin.
    The software takes a file as input. But what I want to do is make it read from stdin so that each time I want to run it I don't have to create a text file and feed it in!That kills disk space and also its an IO read. I've got the .c code. But I don't know exactly how to make it read from stdin. Any suggestions on this?
    Also, if there are no means to make it read from stdin are there any means to create some "virtual file" in C?

    Thank,
    Angkar

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Can you show the section of the program that does the file I/O operations?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The program might already support passing "-" for the filename to read from stdin; most Linux programs do.

    Otherwise, replace the file stream with "stdin" and get rid of the code that opens and closes the file (search for "fopen" and "fclose").
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    you mean something like scanf?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, replace calls to fscanf(fp, "%i", &i) with fscanf(stdin, "%i", &i) or the equivalent scanf("%i", &i).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking stdin for input
    By hawaiian robots in forum C Programming
    Replies: 7
    Last Post: 05-19-2009, 09:06 AM
  2. help reading from stdin
    By asdffa in forum C Programming
    Replies: 8
    Last Post: 11-15-2006, 03:53 PM
  3. stdin + (ctrl+z), how i detect it?
    By Olimpo in forum C Programming
    Replies: 1
    Last Post: 09-30-2006, 05:33 AM
  4. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  5. Array, reading in response etc...
    By mattz in forum C Programming
    Replies: 4
    Last Post: 12-05-2001, 11:41 AM