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. Just a quick one

Just a quick one

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
4 Posts 4 Posters 1 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.
  • T Offline
    T Offline
    thes3cr3t1
    wrote on last edited by
    #1

    Guys how can i declare a varible of type char and then later in my program set the size of the char buffer to suite the size of the file being read into the buffer? C++

    O P T 3 Replies Last reply
    0
    • T thes3cr3t1

      Guys how can i declare a varible of type char and then later in my program set the size of the char buffer to suite the size of the file being read into the buffer? C++

      O Offline
      O Offline
      Owner drawn
      wrote on last edited by
      #2

      LPTSTR pszFileText;//this is the buffer

      //....Then somewhere else where you are reading in the file content

      DWORD dwFileSize;

      //reading the file size
      dwFileSize = ::GetFileSize(hFile, NULL);

      if(dwFileSize != INVALID_FILE_SIZE)
      {
      LPTSTR pszFileText;

      //Take note you are allocating memory here
      HGLOBAL hGlobal = ::GlobalAlloc(GPTR, dwFileSize + 1);

      //lock memory
      pszFileText = (LPTSTR)::GlobalLock(hGlobal);
      if(pszFileText != NULL)
      {
      DWORD dwRead;
      if(::ReadFile(hFile, pszFileText, dwFileSize, &dwRead, NULL))
      ;
      //... rest of the code
      }
      //... if you don't need the contents of the file any more do this

      //open the lock :)
      ::GlobalUnlock(hGlobal);

      //free memory allocated
      ::GlobalFree(hGlobal);
      }

      Jesus Loves You and Me :)

      --Owner Drawn --Nothing special --Defeat is temporary but surrender is permanent --Never say quits --Jesus is Lord

      1 Reply Last reply
      0
      • T thes3cr3t1

        Guys how can i declare a varible of type char and then later in my program set the size of the char buffer to suite the size of the file being read into the buffer? C++

        P Offline
        P Offline
        Prakash Nadar
        wrote on last edited by
        #3

        char *p; latter calculate how much memory you want, p = (char*) malloc(size);


        -Prakash

        1 Reply Last reply
        0
        • T thes3cr3t1

          Guys how can i declare a varible of type char and then later in my program set the size of the char buffer to suite the size of the file being read into the buffer? C++

          T Offline
          T Offline
          ToxickZero
          wrote on last edited by
          #4

          char* buffer;

          int fileSize = WhateverToGetFileSize();

          buffer = new char[fileSize];

          DoStuffWithBuffer(buffer);

          delete[] buffer;

          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