+ 5

How do I get the file size in c++?

I know how to open a file, but I don't know how to get the file size to terminate a while loop that reads every single character and uses it. I tried this: char ReadFile () { char look ; ifstream SLOPE_File (test.slope) ; while (len(SLOPE_File)) { SLOPE_File.get (look) ; } } Is that correct? Thanks!

5th Apr 2018, 12:26 PM
Edwin Pratt
Edwin Pratt - avatar
4 odpowiedzi
+ 2
Use ios_base::ate and fstream::tellg(). If you want to go back to the start of the file, use fstream::seekg(0).
5th Apr 2018, 1:59 PM
Timon Paßlick
+ 4
Timon Paßlick Thank you!
5th Apr 2018, 2:00 PM
Edwin Pratt
Edwin Pratt - avatar
+ 3
Timon Paßlick Yes, in the main.cpp file. With: using namespace std;
5th Apr 2018, 1:57 PM
Edwin Pratt
Edwin Pratt - avatar
+ 2
This is what I have done: char look ; char ReadFile (string fDir, string fName) {    string file = fDir + "/" + fName + ".slope" ;   ifstream SLOPE_File (file) ;    while (SLOPE_File.get(look) ; }
5th Apr 2018, 12:46 PM
Edwin Pratt
Edwin Pratt - avatar