user466534
user466534

Reputation:

calculate size of file

i have following program to calculate size of file

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){

    string line;
    ifstream myfile ("C:\\Users\\7\\Desktop\\example\\text.txt",ios::in | ios::out |ios::binary);
    if (!myfile){
        cout<<"cannot open file";
         exit (1);

    }

     while (!myfile.eof()){
        getline(myfile,line);
        cout<<line<<endl;

     }

     long l,m;
     l=myfile.tellg();
     myfile.seekg(0,ios::end);
     m=myfile.tellg();
     cout<<"size of  text file is:";
     cout<<(m-l)<<"bytes"<<endl;
     myfile.close();

     return 0;

}

for make more clarify in text.txt file i have wrote some copy of information from this site http://en.wikipedia.org/wiki/List_of_algorithms but it shows me 0 bytes and why? what is wrong?

Upvotes: 0

Views: 8052

Answers (7)

Jeff Dege
Jeff Dege

Reputation: 11680

Just a warning - be aware that some OS's provide for sparse files. If you open a file, write a byte, seek to start-of-file + 1,000,000,000, write another byte, then close the file, the intermediate bytes may not actually be written to disk. So, which is the size of the file? The two blocks actually allocated on disk, or the 1,000,000,000 bytes that is the offset of the final byte? Either could be the correct answer, depending upon what you are using the result for,

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490028

Once you read to the end of the file, its fail bit gets set, and until you reset that, nothing else you do with the file will really accomplish much. Your loop for copying the file is also wrong (like virtually all that start with while (!file.eof())).

I'd try something like this:

std::string line;
while (getline(myfile, line))
    std::cout << line << "\n";

// allow further use of the stream object to work:
myfile.clear();    

// since we already read to the end, the current position is the length:
length = myfile.tellg();    

Upvotes: 0

Sam
Sam

Reputation: 2969

Ok, another dopey question, why not use FileInfo('file name') and use the length value stored?

Upvotes: 1

Sam
Sam

Reputation: 2969

It appears your while loop reads the file completely. Then you capture the position in l. Then you seek to m, which is also the position of l. Then you print their difference.

Did I miss something here??

Upvotes: 0

Lavir the Whiolet
Lavir the Whiolet

Reputation: 1016

#include <stdio.h>

int main(int argc, char** argv) {
  FILE *f = fopen("x.txt", "r");
  fseek(f, 0, SEEK_END);
  printf("%ld\n", ftell(f));
  fclose(f);
  return 0;
}

Upvotes: 2

Bertrand Marron
Bertrand Marron

Reputation: 22210

while (!myfile.eof()){
   getline(myfile,line);
   cout<<line<<endl;
}

Reads the whole file, so the get pointer is already at the end of the file. myfile.seekg(0,ios::end) will not move it, so m-l will return 0.

Upvotes: 1

Jason Williams
Jason Williams

Reputation: 57892

You are subtracting the current-file-position (l) from the end-of-file position (m) to get the size. This will work as you expect if the current-file-position is at the start of the file, but as you have just read the entire contents of the file, (l) is "starting" at the end of the file.

Just use the value of (m) rather than (m-l), as files always start at 0.

(Alternatively, before using ftell to get (l), use fseek to move to the start of the file)

Upvotes: 8

Related Questions