Click to See Complete Forum and Search --> : ofstream and large files
tecknophreak
06-12-2003, 11:31 AM
Howdy, I'm trying to write large files 2g+ and the only way I've found how to do this is using open and write. I'm curious if there's a way to do this with the c++ ofstream. I keep coming up with documentation on the linux large filesystem support and the way to do it in C, but nothing about C++. :(
nickptar
06-12-2003, 12:18 PM
You don't need to do anything special. All the LFS support is in the kernel. It doesn't care what language you're using - eventually it's all the same system calls. Just open and write. Try this:
#include <fstream>
int main(void) {
std::#ofstream foo("big.test");
int i;
for(i = 1; i <= 1000000000; i++)
foo << "hello world!" << std::endl;
foo.close();
return 0;
}
(Delete the # between std:: and ofstream. It's only there to keep this damn forum from turning it into a smiley.)
tecknophreak
06-12-2003, 01:27 PM
I ran
#include <fstream>
int main(void) {
std::ofstream foo("big.test");
char array[1000001];
int i;
for (unsigned int j = 0; j < 1000000; ++j) {
array[j] = '0';
}
array[1000000] = '\0';
for(i = 1; i <= 10000; i++)
foo << array << std::endl;
foo.close();
return 0;
}
Since the hello world was taking too long. Then I ran into "File size limit exceeded" around 2GB. I'm running RH9.0 so it should have LFS(large file) in it.
Stuka
06-12-2003, 02:34 PM
nickptar: just a side note - there's a check box in the "Post" page that says "Disable Smilies in This Post". I used it here :) :D ;) :o
nickptar
06-12-2003, 03:50 PM
Oops. Do large files work in C?
bwkaz
06-12-2003, 10:35 PM
Large files work in C when you define some preprocessor variable (I believe you pass -DFILE_OFFSET_BITS=64 to the gcc command line). Doing this instructs the glibc header files to use off64_t instead of off_t in the file-offset functions, and also use open64 (or whatever it's called) instead of open. The thing is, it's not the same system calls, there are two sets of calls. One is for normal files, and the other is for 64-bit-offset files.
What I don't know is whether there's a way to do something similar in C++. I don't think so, because unless I'm mistaken (entirely possible...) most of iostream is implemented in libstdc++, which is already compiled, apparently without large file support.
Hmm...
tecknophreak
06-13-2003, 08:00 AM
That's pretty much what I figured. You can also open a large file by using open("largefile.dat", O_LARGEFILE) in C.
Thanks for the replys though.