Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-07-2007, 02:12 PM   #1
Registered: Sep 2003
Distribution: Arch
Posts: 89

Rep: Reputation: 15
File Space Allocation + Writing (C++)


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

Anyone know where im going wrong?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard Drive space allocation Sheesh SUSE / openSUSE 15 02-13-2006 01:57 PM
kernel memory + slabinfo.. + possible reasons for kernel space allocation failures!! premenjit Linux - Software 1 09-28-2004 05:13 AM
Space Allocation for File Systems majicrobot Linux - Hardware 1 09-20-2004 06:06 PM
dynamic allocation of memory in kernel space appas Programming 1 08-04-2004 07:32 AM
Large tar file taking huge disk space in ext3 file system pcwulf Linux - General 2 10-20-2003 08:45 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:22 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration