Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. preallocating logging files for performance purposes

preallocating logging files for performance purposes

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studioperformance
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alan Kurlansky
    wrote on last edited by
    #1

    I'm looking for some VS c++ code that 1) preallocates a file 2) opens a preallocated file for sequential writing starting at the beginning of the file 3) writes(appends) ascii message strings to the file. Thanks

    L A J 3 Replies Last reply
    0
    • A Alan Kurlansky

      I'm looking for some VS c++ code that 1) preallocates a file 2) opens a preallocated file for sequential writing starting at the beginning of the file 3) writes(appends) ascii message strings to the file. Thanks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I'm not sure exactly what you mena by point 1, but opening a file for appending is quite straightforward, and writing text is even more so. Take a look at the STL file stream classes[^] or use the Createfile[^] and associated functions.

      I must get a clever new signature for 2011.

      1 Reply Last reply
      0
      • A Alan Kurlansky

        I'm looking for some VS c++ code that 1) preallocates a file 2) opens a preallocated file for sequential writing starting at the beginning of the file 3) writes(appends) ascii message strings to the file. Thanks

        A Offline
        A Offline
        Andrew Brock
        wrote on last edited by
        #3

        You need to seek to what you want to be the end of the pre-allocated space, then set the EOF marker, then seek back to the start so you are writing from the beginning of the file, like so

        HANDLE hFile = CreateFile("C:\\test", GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile != INVALID_HANDLE_VALUE) {
        SetFilePointer(hFile, 4 * 1024 * 1024, NULL, SEEK_SET); //seek to 4MB (pre-allocate 4MB)
        SetEndOfFile(hFile); //set the EOF to pre-allocate the space
        SetFilePointer(hFile, 0, NULL, SEEK_SET); //seek back to the start for writing
        //write to the file with WriteFile()
        CloseHandle(hFile);
        }

        Edit: I missed part 3... quite simply seek to the end of the data in the file to append to it.

        SetFilePointer(hFile, 0, NULL, SEEK_END); //If you want to append to the end of the entire file (including after the pre-allocated space)
        SetFilePointer(hFile, nDataLength, NULL, SEEK_SET); //if you are keeping track of the length of the data you have written to the file with the variable nDataLength

        Writing to a file is pretty straight forward. You just need a buffer and the length of data you are writing

        LPSTR szWrite = "hello world";
        DWORD nLength = strlen(szWrite);
        DWORD nBytesWritten;
        WriteFile(hFile, szWrite, nLength, &nBytesWritten, NULL);
        CloseHandle(hFile);

        If you are writing strings in unicode you need to multiply the length by sizeof(wchar_t) or sizeof(TCHAR) Finally, All file writes (and reads) are sequential for all APIs that I know of. It moves the file pointer to the end of the data you have just written (or read)

        modified on Wednesday, January 12, 2011 11:23 AM

        1 Reply Last reply
        0
        • A Alan Kurlansky

          I'm looking for some VS c++ code that 1) preallocates a file 2) opens a preallocated file for sequential writing starting at the beginning of the file 3) writes(appends) ascii message strings to the file. Thanks

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          If you are looking for logging, rather than just output, then the following exists. http://log4cpp.sourceforge.net/[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups