Compiled the following code to obtain the file size for: example.txt, and
the file size was not returned when ./myfile was executed. ./myfile returned the prompt $.
myfile.cpp was compiled using the following commands:
$ g++ -c myfile.cpp created >> myfile.o
$ g++ myfile.o -o myfile created >> executable
$ ./myfile created >> example.txt
// obtaining file size
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
#include <fstream>
usingnamespace std;
int main() {
long begin, end;
ifstream myfile ("example.txt");
begin = myfile.tellg();
myfile.seekg (0, ios::end);
end = myfile.tellg();
myfile.close();
cout << "size is: " << (end-begin) << "bytes.\n";
return 0;
}
Code tags please, even for short code. And, to state the obvious, did you look in example.txt to see if the size was actually appended? You're redirecting the output to append to that file.
blackcoder41 the file does open. I recompiled and now the program returns "size is: 38 bytes."
Honestly I don't have a clue what happened to make it work. I did not change or correct the code in any manner???