473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with ofstream::seekp

Hi all!

I'm using ofstream and seekp to write a file. The problem is I have to
write a very big file, more than 4 Gb, but the streamoff that I have to
pass to seekp isn't a 64 bit int (it's a long int) so I can't use it!
Is it normal or there is some workaround for this?

Thank you

Sep 26 '06 #1
4 7148
I'm going to explain better:

I have to write a big file to disk. I used ofstream and something like

int dim = 1024 * 1024;
ch = new char[dim];
os.write(ch,dim);

Then I had to use seekp to find the position of a certain buffer,
because I receive packets with the buffer to write, the lenght of the
buffer and the offset from the beginning of the file.
The problem is that the file could be bigger than 4 Gb, so when I use
seekp I get an error, or better it doesnt work. For example

long long int big = (long long int)5 * 1024 * 1024 * 1024;
os.seekp(big,ios_base::beg);
cout << os.tellp();

The output of this piece of code is -1!!!!!

Is there a way to use seekp with 64 bit integers?
If not, I think I'll have to try to use os.open(filename,
ios_base::ate) (it gets me to the end of file rigth??), but first I
wanted to try seekp, because I wouldn't have to save a non aligned
packet in memory...

Thank you for your help!

Paolo

Sep 27 '06 #2
Paolo wrote:
I'm going to explain better:

I have to write a big file to disk. I used ofstream and something
like

int dim = 1024 * 1024;
ch = new char[dim];
os.write(ch,dim);

Then I had to use seekp to find the position of a certain buffer,
because I receive packets with the buffer to write, the lenght of
the
buffer and the offset from the beginning of the file.
The problem is that the file could be bigger than 4 Gb, so when I
use
seekp I get an error, or better it doesnt work. For example

long long int big = (long long int)5 * 1024 * 1024 * 1024;
os.seekp(big,ios_base::beg);
cout << os.tellp();

The output of this piece of code is -1!!!!!
That indicates a failure.
Is there a way to use seekp with 64 bit integers?
Only on systems that has a 64 bit long. The long long type isn't (yet)
part of the C++ standard, so the library can't really use it.
If not, I think I'll have to try to use os.open(filename,
ios_base::ate) (it gets me to the end of file rigth??), but first I
wanted to try seekp, because I wouldn't have to save a non aligned
packet in memory...
How large is the file really? Is it *much* larger that 4 GB? If not,
you could perhaps divide your offset into a few separate seeks, moving
4 GB at a time. Or seek from ios_base::cur, or ios_base::end?
Bo Persson
Sep 27 '06 #3

Bo Persson ha scritto:
Paolo wrote:
I'm going to explain better:

I have to write a big file to disk. I used ofstream and something
like

int dim = 1024 * 1024;
ch = new char[dim];
os.write(ch,dim);

Then I had to use seekp to find the position of a certain buffer,
because I receive packets with the buffer to write, the lenght of
the
buffer and the offset from the beginning of the file.
The problem is that the file could be bigger than 4 Gb, so when I
use
seekp I get an error, or better it doesnt work. For example

long long int big = (long long int)5 * 1024 * 1024 * 1024;
os.seekp(big,ios_base::beg);
cout << os.tellp();

The output of this piece of code is -1!!!!!

That indicates a failure.
Is there a way to use seekp with 64 bit integers?

Only on systems that has a 64 bit long. The long long type isn't (yet)
part of the C++ standard, so the library can't really use it.
If not, I think I'll have to try to use os.open(filename,
ios_base::ate) (it gets me to the end of file rigth??), but first I
wanted to try seekp, because I wouldn't have to save a non aligned
packet in memory...

How large is the file really? Is it *much* larger that 4 GB? If not,
you could perhaps divide your offset into a few separate seeks, moving
4 GB at a time. Or seek from ios_base::cur, or ios_base::end?
Bo Persson
Thank you fr your answer. The file could be much bigger than 4 Gb,
maybe twice or three times. I think I'll use a 32 bit int for offset,
and another one to check if it'sin the first 4 Gb, the second one and
so on. This way I will have tha same result as using 64 bit integers.
Thank you for your clarification about seekp!

Bye

Paolo

Sep 27 '06 #4

Bo Persson ha scritto:
Paolo wrote:
I'm going to explain better:

I have to write a big file to disk. I used ofstream and something
like

int dim = 1024 * 1024;
ch = new char[dim];
os.write(ch,dim);

Then I had to use seekp to find the position of a certain buffer,
because I receive packets with the buffer to write, the lenght of
the
buffer and the offset from the beginning of the file.
The problem is that the file could be bigger than 4 Gb, so when I
use
seekp I get an error, or better it doesnt work. For example

long long int big = (long long int)5 * 1024 * 1024 * 1024;
os.seekp(big,ios_base::beg);
cout << os.tellp();

The output of this piece of code is -1!!!!!

That indicates a failure.
Is there a way to use seekp with 64 bit integers?

Only on systems that has a 64 bit long. The long long type isn't (yet)
part of the C++ standard, so the library can't really use it.
If not, I think I'll have to try to use os.open(filename,
ios_base::ate) (it gets me to the end of file rigth??), but first I
wanted to try seekp, because I wouldn't have to save a non aligned
packet in memory...

How large is the file really? Is it *much* larger that 4 GB? If not,
you could perhaps divide your offset into a few separate seeks, moving
4 GB at a time. Or seek from ios_base::cur, or ios_base::end?
Bo Persson
Thank you for your answer. The file could be much bigger than 4 Gb,
maybe twice or three times. Thank you for your answer anyway, I'll try
ios_base::app!

Bye

Paolo

Sep 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
11
by: Ebi | last post by:
It's a main function of club program in Borland c++ 5; There is a film class in my club program... But I have a problem with it: whenever I add a film by addfilm function to film.dat file,...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
1
by: Maitre Bart | last post by:
The following simple program overwrites the content of an ostriungstream object. #include <iostream> #include <sstream> using namespace std; int main() {
5
by: wobudui | last post by:
Hi everyboday, I have some trouble in dealing with the file stream. My souce code Listed hear: int main() { char buffer={0}; ofstream ofile.open("mydata.in",ios::app); ofile.seekp(10);...
2
by: pankaj255143 | last post by:
i cannot get the output by using the function showdata()........plz run this code and send the solution on my ID <ID removed by weaknessforcats> // A PROGRAME TO OPERATE SOME FILE RELATED...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
2
by: brixton | last post by:
Hello, I've got the following code: wxString path = filepath; wxString newpath = filepath; fstream f(path.Append("/tests/tests.bin"), ios::in | ios::binary); fstream...
5
by: ishould | last post by:
My actual problem is much larger but figuring out how to do this example will help me. I want to overwrite data that already exists in a text file but I can't seem to do this. ex. I have five...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.