Writing to memory instead of FILE

Discussion in 'C++' started by Anders Thomsen, Apr 29, 2004.

  1. (Using MS VC++ 6.0)

    In a lib file I have a method that takes a FILE as parameter. The
    method writes some text to this file. I don't need the text in a file
    but in a variable.
    So how can I make the method write to a buffer insted of a file on a
    disk?

    Thanks,
    Anders Thomsen
     
    Anders Thomsen, Apr 29, 2004
    #1
    1. Advertising

  2. "Anders Thomsen" <> wrote in message
    news:...
    > (Using MS VC++ 6.0)
    >
    > In a lib file I have a method that takes a FILE as parameter. The
    > method writes some text to this file. I don't need the text in a file
    > but in a variable.
    > So how can I make the method write to a buffer insted of a file on a
    > disk?
    >
    > Thanks,
    > Anders Thomsen


    You cannot. If you can switch to using C++ I/O where this is REALLY EASY,
    instead of using FILE, where it is COMPLETELY IMPOSSIBLE.

    john
     
    John Harrison, Apr 29, 2004
    #2
    1. Advertising

  3. Anders Thomsen

    Julie Guest

    John Harrison wrote:
    [snip]
    > You cannot. If you can switch to using C++ I/O where this is REALLY EASY,
    > instead of using FILE, where it is COMPLETELY IMPOSSIBLE.


    That is a qualified "COMPLETELY IMPOSSIBLE", meaning that within the confines
    of proper and portable C++, it isn't possible.

    However, there may be ways to accomplish what you desire using operating-system
    specific file i/o hooks and/or other hacking & subterfuge -- BUT, these should
    only be considered as a last resort, and only by someone that fully understands
    the implementation details, implications, and ramifications of such hacking.
    Something else that could be considered is how the library is linked in -- if
    it is a library in the traditional sense (static link), the OP may be able to
    provide specialized implementations of the standard file i/o functions (say,
    fputs) and they redirect the output to memory. Depending on how your compiler
    performs and resolves the link, this may or may not work. Again, an advanced
    (hack) method that has all sorts of caveats.
     
    Julie, Apr 29, 2004
    #3
  4. > You cannot. If you can switch to using C++ I/O where this is REALLY EASY,
    > instead of using FILE, where it is COMPLETELY IMPOSSIBLE.


    I don't have access to the source of the lib file, so I'm afraid
    that's not possible.

    Thanks for the info.
     
    Anders Thomsen, Apr 29, 2004
    #4
  5. "Anders Thomsen" <> wrote in message
    news:...
    > (Using MS VC++ 6.0)
    >
    > In a lib file I have a method that takes a FILE as parameter. The
    > method writes some text to this file. I don't need the text in a file
    > but in a variable.
    > So how can I make the method write to a buffer insted of a file on a
    > disk?


    With the caveat that this isn't portable (or standard), you could open both ends
    of a pipe or a socket, associate one ebd with a FILE * that you pass to your
    library, and populate the variable yourself by reading from the other end. As
    convoluted as this is, it would still be much faster than file I/O and it
    wouldn't require having write access to the disk.

    Claudio Puviani
     
    Claudio Puviani, Apr 29, 2004
    #5
  6. Anders Thomsen

    Walter Tross Guest

    On 29 Apr 2004 06:56:45 -0700, (Anders Thomsen) wrote:

    >(Using MS VC++ 6.0)
    >
    >In a lib file I have a method that takes a FILE as parameter. The
    >method writes some text to this file. I don't need the text in a file
    >but in a variable.
    >So how can I make the method write to a buffer insted of a file on a
    >disk?


    How about opening the FILE in "a+" mode, and using ftell and fseek?
     
    Walter Tross, Apr 29, 2004
    #6
  7. "Walter Tross" <> wrote in message
    news:...
    > On 29 Apr 2004 06:56:45 -0700, (Anders Thomsen) wrote:
    >
    > >(Using MS VC++ 6.0)
    > >
    > >In a lib file I have a method that takes a FILE as parameter. The
    > >method writes some text to this file. I don't need the text in a file
    > >but in a variable.
    > >So how can I make the method write to a buffer insted of a file on a
    > >disk?

    >
    > How about opening the FILE in "a+" mode, and using ftell and fseek?
    >


    How would that help? Its still a file.

    john
     
    John Harrison, Apr 30, 2004
    #7
  8. I would try setting up the FILE with setvbuf() , and install a "sufficiently
    large buffer" where all file contents fit, and then read the contents from
    the buffer...

    "Anders Thomsen" <> wrote in message
    news:...
    > (Using MS VC++ 6.0)
    >
    > In a lib file I have a method that takes a FILE as parameter. The
    > method writes some text to this file. I don't need the text in a file
    > but in a variable.
    > So how can I make the method write to a buffer insted of a file on a
    > disk?
    >
    > Thanks,
    > Anders Thomsen
     
    Jesper Madsen, Jul 4, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,403
    HNguyen
    Dec 21, 2004
  2. Replies:
    1
    Views:
    364
    Sanjay
    Feb 17, 2005
  3. Replies:
    6
    Views:
    584
    Gregory Pietsch
    May 21, 2004
  4. pereges

    storing data in file instead of memory

    pereges, Jun 3, 2008, in forum: C Programming
    Replies:
    7
    Views:
    293
    Bartc
    Jun 4, 2008
  5. Fearless Fosdick

    Net::SFTP from memory instead of file?

    Fearless Fosdick, Aug 23, 2005, in forum: Perl Misc
    Replies:
    2
    Views:
    131
    Fearless Fosdick
    Aug 24, 2005
Loading...

Share This Page