B
B
bluebyte2011-08-21 07:54:29
C++ / C#
bluebyte, 2011-08-21 07:54:29

How to write a >4Gb file in C++?

In the process of solving one educational problem, I encountered the following problem.

You can add data to an existing empty file as follows: Files larger than 4 GB are created this way without problems. But as soon as the file size crosses the “cherished” figure, it is not possible to add data in this way. Three questions: 1. What is wrong? 2. How to fix? 3. What is usually used to work with large files? In addition: - NTFS - VC2010 32b - Windows 7 x64

ofstream ofs("file", ios_base::binary | ios_base::in | ios_base::ate);
long long pos = 5000000000;
ofs.seekp(pos, ios_base::beg);
ofs.write(as_bytes(pos), sizeof(pos));
ofs.close();




Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Next_Alex, 2011-08-21
@bluebyte

Something tells me that the problem is that the size of the int type, which is used in ofstream::seekp and in ofstream::write is 4 bytes. And this means that with the help of these methods it is fundamentally impossible to write more than 4 GB.
There is an answer here:
stackoverflow.com/questions/293672/reading-files-larger-than-4gb-using-c-stl

I
ixSci, 2011-08-21
@ixSci

1. If you are using ofstream, then the flag should be ios_base::out, not ios_base::in
2. To be honest, I don't know if it's possible to expand the file in the way you are using. I have not found any refutation or evidence for this. But given that seekp sets a pointer within a sequence, I wouldn't hope for a guaranteed behavior in this case.
3. VS 2010 STL works with 2^64 files, so there is no gag in this.

N
Next_Alex, 2011-08-22
@Next_Alex

In that case lies not only MSDN.
By the way, it's still unknown how seekg handles this value :)
In general, I still don't see the point in using standard input-output streams - with them there is only eternal dancing with a tambourine, plus in append mode you can't write data to an arbitrary position :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question