Writing to memory instead of FILE

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anders Thomsen

    Writing to memory instead of FILE

    (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
  • John Harrison

    #2
    Re: Writing to memory instead of FILE


    "Anders Thomsen" <junk@mexp.dk > wrote in message
    news:5db7bcad.0 404290556.70dad 4a7@posting.goo gle.com...[color=blue]
    > (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[/color]

    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


    Comment

    • Julie

      #3
      Re: Writing to memory instead of FILE

      John Harrison wrote:
      [snip][color=blue]
      > 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.[/color]

      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.

      Comment

      • Anders Thomsen

        #4
        Re: Writing to memory instead of FILE

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

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

        Thanks for the info.

        Comment

        • Claudio Puviani

          #5
          Re: Writing to memory instead of FILE

          "Anders Thomsen" <junk@mexp.dk > wrote in message
          news:5db7bcad.0 404290556.70dad 4a7@posting.goo gle.com...[color=blue]
          > (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?[/color]

          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


          Comment

          • Walter Tross

            #6
            Re: Writing to memory instead of FILE

            On 29 Apr 2004 06:56:45 -0700, junk@mexp.dk (Anders Thomsen) wrote:
            [color=blue]
            >(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?[/color]

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

            Comment

            • John Harrison

              #7
              Re: Writing to memory instead of FILE


              "Walter Tross" <walter@waltert ross.com> wrote in message
              news:5403901ssm qa2e6jvsjvd7gv2 25339urq6@4ax.c om...[color=blue]
              > On 29 Apr 2004 06:56:45 -0700, junk@mexp.dk (Anders Thomsen) wrote:
              >[color=green]
              > >(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?[/color]
              >
              > How about opening the FILE in "a+" mode, and using ftell and fseek?
              >[/color]

              How would that help? Its still a file.

              john


              Comment

              Working...