473,382 Members | 1,512 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,382 software developers and data experts.

File Space Allocation + Writing (C++/Linux)

Hi,

My program takes a series of encoded file segments, and decodes each
segment and writes it to the output file.

I am trying to approach this as follows:

I know the size of the output file in bytes so i pre-allocate the
space for it and fill it with NULL bytes using the linux truncate
function, I do this incase one of segments is missing i just want null
bytes in its place with the other segments around the missing block.

I also know the position (start and end) in bytes where each decoded
segment needs to go in the output file, so using ofstream i am
'attempting' to use seekp to set the file pointer at the position on
the truncated file and write my decoded segment out.

The problem is that if I don't open the file in append mode (ios::app)
then it automatically overwrites the output file contents, so all
previous written segments are deleted. And if i use append mode, then
seekp doesn't seem to work, and after the truncate() is performed, the
segments just get appended onto the end of the file instead of
overwriting the null bytes at the position specified.

Now for some code:

ofstream ostr(ofile.c_str(), ios::app); // Open output file in append
mode
....
truncate(ofile.c_str(), file_size_in_bytes); // Fill the file to
specified size with NULL bytes
....
ostr.seekp(begin_bytes - 1);
// Move to segment start position in the file
....

My decoding/file output

//Clean up
ostr.close();

Anyone know where im going wrong? If i call ostr.tellp() it suggests
im at the correct place in the file, but when i actually write stuff
out with ostr.put() it goes at the wrong place in the file

Thanks

Aug 7 '07 #1
6 2682
JackC wrote:
My program takes a series of encoded file segments, and decodes each
segment and writes it to the output file.
[..]
The problem is that if I don't open the file in append mode (ios::app)
then it automatically overwrites the output file contents, so all
previous written segments are deleted. And if i use append mode, then
seekp doesn't seem to work, and after the truncate() is performed, the
segments just get appended onto the end of the file instead of
overwriting the null bytes at the position specified.
Have you tried opening in read+write mode?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '07 #2
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
My program takes a series of encoded file segments, and decodes each
segment and writes it to the output file.
[..]
The problem is that if I don't open the file in append mode (ios::app)
then it automatically overwrites the output file contents, so all
previous written segments are deleted. And if i use append mode, then
seekp doesn't seem to work, and after the truncate() is performed, the
segments just get appended onto the end of the file instead of
overwriting the null bytes at the position specified.

Have you tried opening in read+write mode?
[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, How exactly would i do that with ofstream? I have tried using
ios::out but with no success is that the mode you meant?

Cheers,
Jack

Aug 7 '07 #3
JackC wrote:
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>JackC wrote:
>>My program takes a series of encoded file segments, and decodes each
segment and writes it to the output file.
[..]
The problem is that if I don't open the file in append mode
(ios::app) then it automatically overwrites the output file
contents, so all previous written segments are deleted. And if i
use append mode, then seekp doesn't seem to work, and after the
truncate() is performed, the segments just get appended onto the
end of the file instead of overwriting the null bytes at the
position specified.

Have you tried opening in read+write mode?
>>[..]

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Thanks, How exactly would i do that with ofstream? I have tried using
ios::out but with no success is that the mode you meant?
Obviously you can't do that with an *o*fstream. Use std::fstream.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 7 '07 #4
On 7 Aug, 21:32, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
My program takes a series of encoded file segments, and decodes each
segment and writes it to the output file.
[..]
The problem is that if I don't open the file in append mode
(ios::app) then it automatically overwrites the output file
contents, so all previous written segments are deleted. And if i
use append mode, then seekp doesn't seem to work, and after the
truncate() is performed, the segments just get appended onto the
end of the file instead of overwriting the null bytes at the
position specified.
Have you tried opening in read+write mode?
>[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, How exactly would i do that with ofstream? I have tried using
ios::out but with no success is that the mode you meant?

Obviously you can't do that with an *o*fstream. Use std::fstream.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks, i couldn't get it working with fstream, but i switched to the
native linux file I/O functions i.e open(), write() etc and everything
works fine so i might as well stick with these as portability isn't an
issue.

Aug 7 '07 #5
JackC wrote:
[..] i couldn't get it working with fstream, but i switched to the
native linux file I/O functions i.e open(), write() etc and everything
works fine so i might as well stick with these as portability isn't an
issue.
Whatever works for you. Sorry I couldn't help.
Aug 7 '07 #6
On Aug 7, 10:32 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
On 7 Aug, 20:45, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
JackC wrote:
My program takes a series of encoded file segments, and decodes each
segment and writes it to the output file.
[..]
The problem is that if I don't open the file in append mode
(ios::app) then it automatically overwrites the output file
contents, so all previous written segments are deleted. And if i
use append mode, then seekp doesn't seem to work, and after the
truncate() is performed, the segments just get appended onto the
end of the file instead of overwriting the null bytes at the
position specified.
Have you tried opening in read+write mode?
>[..]
Thanks, How exactly would i do that with ofstream? I have tried using
ios::out but with no success is that the mode you meant?
Obviously you can't do that with an *o*fstream.
Why not? According to the standard:

std::ofstream f( filename, std::ios::in ) ;

is supposed to work. In such cases, I'd explicitly or in
std::ios::out as well, just to be clearer, even though
ofstream::open does it for you. In fact, I'd probably add some
sort of comment to the effect that I was doing this because it
is the only way to get an update mode, and not because I wanted
to read.

Also, of course, seeking to arbitrary positions only works if
the file is opened in binary. So the final declaration would
actually be:

std::ofstream f( filename,
std::ios::out | std::ios::in |
std::ios::binary ) ;

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Aug 8 '07 #7

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

Similar topics

7
by: Patrick Useldinger | last post by:
Hi, I think I found a bug in the write method of file objects. It seems as if before writing each block, a check was done in order to verifiy that there is enough space left for the *whole*...
7
by: Atila Olah | last post by:
I'm working on a project to implement a simple cross-platform file sharing protocol (using Python) that is similar to HTTP, and I have to write a GUI for Windows and Linux. But let's start with the...
9
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to...
3
by: asm | last post by:
Hello all, I need your help on this problem. I wrote a little program as follows. (BTW, I worked on a new dell latitude, runing Linux kernel 2.4.19, i686). Program was compiled with gcc 3.2 ...
14
by: Otto Meijer | last post by:
Hi everyone, for one of my projects I need to figure out the size of the swap file(s) of a certain system. The problem is I need to do this on a host of platforms like: HP_UX, Solaris, Linux,...
7
by: Dan Nilsen | last post by:
Hi! I'm writing a small piece of software that basically runs on an embedded system with a Power-PC cpu. This runs on a stripped down version of Linux - Busybox. As I'm writing a piece of...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
7
by: ramasubramanian.rahul | last post by:
hi i was trying to see how the compiler hides the static golbals from the linker and allows golbal varibale to be visable to the linker.i managed to figure out how it did that ( the .lcomm and...
116
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.