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. ifstream question

ifstream question

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutorial
6 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.
  • R Offline
    R Offline
    Rassul Yunussov
    wrote on last edited by
    #1

    Does anyone know how to get file size, if i work with files using ifstream class?

    M A 2 Replies Last reply
    0
    • R Rassul Yunussov

      Does anyone know how to get file size, if i work with files using ifstream class?

      M Offline
      M Offline
      mylzw
      wrote on last edited by
      #2

      #include #include using namespace std; void main() { ifstream in("c:/boot.ini"); in.seekg(0, ios::end); //set file-stream pos at end streampos ps = in.tellg(); //get the pos cout << "File size: " << ps << endl; //file size by "byte" in.close(); //close... }

      R 1 Reply Last reply
      0
      • R Rassul Yunussov

        Does anyone know how to get file size, if i work with files using ifstream class?

        A Offline
        A Offline
        Aqueel
        wrote on last edited by
        #3

        Following should work. int fh = _open(_file name_, _O_APPEND , _S_IREAD | _S_IWRITE ); int filesize = _filelength(fh); Above code needs following header files 1. io.h 2. fcntl.h 3. sys\stat.h I don't now how can we get file size using ifstream.h. I used above to get it. We Believe in Excellence

        K 1 Reply Last reply
        0
        • A Aqueel

          Following should work. int fh = _open(_file name_, _O_APPEND , _S_IREAD | _S_IWRITE ); int filesize = _filelength(fh); Above code needs following header files 1. io.h 2. fcntl.h 3. sys\stat.h I don't now how can we get file size using ifstream.h. I used above to get it. We Believe in Excellence

          K Offline
          K Offline
          kakan
          wrote on last edited by
          #4

          Or use good old stat()/_stat() Then you won't have to open and close the file. And you don't have to worry about text/binary mode.

          1 Reply Last reply
          0
          • M mylzw

            #include #include using namespace std; void main() { ifstream in("c:/boot.ini"); in.seekg(0, ios::end); //set file-stream pos at end streampos ps = in.tellg(); //get the pos cout << "File size: " << ps << endl; //file size by "byte" in.close(); //close... }

            R Offline
            R Offline
            Rassul Yunussov
            wrote on last edited by
            #5

            Thanks a lot!!! Now i want to read the file into the memory and work with this memory like i worke as with file, i think using istream, but i can't make the istream object... Here is my code: ifstream shapefilef(ShapeFileName.c_str(),ios::binary); shapefilef.seekg(0,ios::end); streampos ps = shapefilef.tellg(); int size = ps.seekpos(); char* data = new char [size]; shapefilef.seekg(0,ios::beg); shapefilef.read(data,size); istream shapefile(data); Where is the mistake? I can't compile it... Something in constructor.

            M 1 Reply Last reply
            0
            • R Rassul Yunussov

              Thanks a lot!!! Now i want to read the file into the memory and work with this memory like i worke as with file, i think using istream, but i can't make the istream object... Here is my code: ifstream shapefilef(ShapeFileName.c_str(),ios::binary); shapefilef.seekg(0,ios::end); streampos ps = shapefilef.tellg(); int size = ps.seekpos(); char* data = new char [size]; shapefilef.seekg(0,ios::beg); shapefilef.read(data,size); istream shapefile(data); Where is the mistake? I can't compile it... Something in constructor.

              M Offline
              M Offline
              mylzw
              wrote on last edited by
              #6

              Have a try: istrstream shapefile(data); // you must #include The istream not constructor like that istream(char* ...) I think that you need istrstream

              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