开发者

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<&开发者_StackOverflow中文版lt;(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?


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)


#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;
}


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.


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


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??


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();    


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,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜