Thread: Storing all of stdin into a variable

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    Storing all of stdin into a variable

    Hi,

    I have an unknown amount of input coming from a pipe into stdin from a child process to a parent. How would I store all of this into a variable so I am then able to process it several times?

    Thanks

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Read all of it as you would any normal file and store it in some blob char array.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Thinking about it you would require:

    Some way to allocate a block of memory at will, of any size you like.
    Some way to enlarge that block of memory at will, to any size you like.
    Some way to read from the file.

    Which sounds to me suspiciously like the bog-standard, well-documented, always-available, built-in C functions:

    malloc
    realloc
    fopen/fread/fclose

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It's really the "unknown" amount of input that sounds like somewhere along the way, a bad design decision was made.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing Words in a Variable?
    By zacharyrs in forum C Programming
    Replies: 3
    Last Post: 05-20-2009, 04:50 AM
  2. storing a fil to a variable.
    By lilrayray in forum C Programming
    Replies: 46
    Last Post: 08-05-2006, 12:00 AM
  3. Storing an IP Address in a Variable?
    By guitarlover317 in forum C++ Programming
    Replies: 1
    Last Post: 05-12-2006, 07:54 AM
  4. Variable Storing in memory
    By karb0noxyde in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 07:31 PM
  5. Storing a name, what variable?
    By RoD in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2002, 02:36 AM