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. fstream problem

fstream problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
2 Posts 2 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.
  • I Offline
    I Offline
    Imtiaz Murtaza
    wrote on last edited by
    #1

    Friends, I am opening a file using std::fstream. The purpose is to read all the contents of file into a buffer and then close the file. Program uses this buffer later. The problem is that i am not an expert of fstream. I am using a function Read(), to copy data from file to buffer. The problem is the allocation of buffer. Lets see what i am doing: fstream file("somefile.txt"); char *pBuffer = CreateNewBuffer(100) // 100 is buffer size while(!file.eof()) { file.read(buffer,100); // Blah Blah } I don't know the number of bytes actually present in the file and so i am creating the buffer of size 100. Is there any way to find out that how many bytes are there in the file OR how many bytes left, so that there will only be one Read() call instead of loop?? And most important for me is that how may bytes are actually read into the buffer. Imtiaz

    A 1 Reply Last reply
    0
    • I Imtiaz Murtaza

      Friends, I am opening a file using std::fstream. The purpose is to read all the contents of file into a buffer and then close the file. Program uses this buffer later. The problem is that i am not an expert of fstream. I am using a function Read(), to copy data from file to buffer. The problem is the allocation of buffer. Lets see what i am doing: fstream file("somefile.txt"); char *pBuffer = CreateNewBuffer(100) // 100 is buffer size while(!file.eof()) { file.read(buffer,100); // Blah Blah } I don't know the number of bytes actually present in the file and so i am creating the buffer of size 100. Is there any way to find out that how many bytes are there in the file OR how many bytes left, so that there will only be one Read() call instead of loop?? And most important for me is that how may bytes are actually read into the buffer. Imtiaz

      A Offline
      A Offline
      Andrew Walker
      wrote on last edited by
      #2

      std::ifstream isn't designed to allow the reading of the entire filesize. The function _filelength in <io.h> will give you the length of a file. Be warned that this function is limited to the size of a long, so for very large files you'll need to use a platform dependant API (See MSDN). The following function taken from the boost::regex sample code (regex_grep_example1.cpp) is one example of how to efficiently and safely allocate space using std::string. This is not the simplest solution, it is flexible. However without profiling this is micro-optimisation that probably isn't going to help. Note the use of reserve and capacity to ensure appropriate growth system for you buffers. You may be able to tweak initial size and growth rates if you know roughly what size the file will be.void load_file(std::string& s, std::istream& is) { s.erase(); if(is.bad()) return; s.reserve(is.rdbuf()->in_avail()); char c; while(is.get(c)) { if(s.capacity() == s.size()) s.reserve(s.capacity() * 3); s.append(1, c); } }


      If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts your aim; Yours is the Earth and everything that's in it. Rudyard Kipling

      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