Is it possible to create in-memory streams in C?

This is a discussion on Is it possible to create in-memory streams in C? within the C Programming forums, part of the General Programming Boards category; Hey all! I need to create a FILE* input stream in plain C, which reads from a memory buffer (char*) ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Is it possible to create in-memory streams in C?

    Hey all!

    I need to create a FILE* input stream in plain C, which reads from a memory buffer (char*) as if it were from standard input. I want be able to create
    such a stream and read it with fgetc(), fscanf(), fread() or what ever stream based input function in the standard C library. Something similar to the StrStream class in C++ or the StringReader class in Java, but I want a plain C solution. It must be transparent to the user if the file pointer takes data from a file, socket, pipe or memory buffer!

    It have to work in UNIX! I'm not interested in a
    Win32 exclusive solution.

    Is it possible at all? But in fact Java and C++ implements it so I think there must be a workaround. Otherwise there wouldn't be any StrStreams and StringReaders!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I need to create a FILE* input stream in plain C, which reads
    >from a memory buffer (char*) as if it were from standard input
    If you must use the FILE struct then you will have to initialize it yourself instead of using a prewritten function such as fopen. Check your compiler's includes to see what the members of FILE are and what they do.

    >Something similar to the StrStream class in C++
    Why not just use sscanf and sprintf for this?

    >It must be transparent to the user if the file pointer takes data
    >from a file, socket, pipe or memory buffer!
    Generic functions are difficult to get right, you would be better off just accepting a mode as the argument and then performing the correct operation according to the mode. Such as taking a -s flag for reading from strings and -f flag for reading from files.

    >It have to work in UNIX!
    Standard C tends to work in UNIX.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    2
    If you must use the FILE struct then you will have to initialize it yourself instead of using a prewritten function such as fopen. Check your compiler's includes to see what the members of FILE are and what they do.

    M> No! The FILE struct is not the problem per se!
    If I only could get a file descriptor coupled
    to the memory location I could link it to a
    FILE using fdopen().

    Why not just use sscanf and sprintf for this?

    M> That simply don't work. I can't use sscanf()
    to feed a stream into the stomach of lex and
    yacc for one sake.


    Generic functions are difficult to get right, you would be better off just accepting a mode as the argument and then performing the correct operation according to the mode. Such as taking a -s flag for reading from strings and -f flag for reading from files.

    M> There should darn well be a way to accomplish
    such "generic" functions as you call'em. How
    else would Java's StringReaders be
    implemented? Maybe I have to look at the source of some compiler to solve the problem?

    Regards Mats

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Go figure it out yourself if you're going to snap at me for trying to help you. You come here with a very vague question that could be interpereted any number of ways, then when someone tries to help, you ***** and moan that it isn't what you wanted. Go do research, figure it out on your own and if you have a more specific problem then come back and ask.

    And try to be more polite, we don't HAVE to help you.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-06-2009, 11:27 AM
  2. Cannot create shared memory
    By Phoenix_Rebirth in forum C Programming
    Replies: 3
    Last Post: 11-07-2008, 10:32 AM
  3. Memory allocation/reallocation
    By magda3227 in forum C Programming
    Replies: 10
    Last Post: 07-04-2008, 03:27 PM
  4. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  5. Novice Pointers/Class Question
    By C++Gamer in forum C++ Programming
    Replies: 8
    Last Post: 06-28-2006, 05:36 PM

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