How to provide buffer size dynamically.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Man4ish
    New Member
    • Mar 2008
    • 151

    How to provide buffer size dynamically.

    Hi,

    Normally when we read a file we need to provide buffer size. How it can be provided dynamically to save the space and time.


    Regards
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    That problem description is too vague for us to answer clearly. That is people may answer but because your description is so vague there is a high probability that they will misinterpret you and give you the answer to a different question to the one you are trying to ask.

    It is common for small to medium size files to get the size of the file first and allocate a buffer large enough to hold the entire file before reading it in.

    Reading a line at a time into a string in C++ you don't need to know the size of the line the library sizes the string to the required size.

    Comment

    • Man4ish
      New Member
      • Mar 2008
      • 151

      #3
      Thanks for our answer which is appropriate but using string class will be slower. Is there any way to allocate the buffer size dynamically?

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Use new (or malloc if it is C)

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Do a seek to the end of the file followed by a tell. That will get you the number of bytes in the file. Allocate that amount.

          Comment

          Working...