Create filke with a specified dimension

P

Paolo

Hi!

I'm writing an application that receives data from internet. I usually
receive big files, few hundred MB to Gb and the packets I receive could
be not in a correct order. I though I could send a packet that specify
the lenght of the file before any other packet. The receiver creates
this file, with the specified dimension and then using ofstream's write
and seekp I would write the packets in the correct position.

My problem is, how can I create a file with a specific dimension? Do I
have to use something like

while(lenght < the_lenght_i_want)
write(new char[1000], 1000)

???
Then close the file and open it every time I receive a packet? I can't
use a buffer, at least not a buffer that contains the whole file!
Any suggestion?

Thank you!
 
S

Spoon

Paolo said:
I'm writing an application that receives data from internet. I
usually receive big files, few hundred MB to Gb and the packets I
receive could be not in a correct order.

Why don't you use TCP?
 
J

Jacek Dziedzic

Paolo said:
Hi!

I'm writing an application that receives data from internet. I usually
receive big files, few hundred MB to Gb and the packets I receive could
be not in a correct order. I though I could send a packet that specify
the lenght of the file before any other packet. The receiver creates
this file, with the specified dimension and then using ofstream's write
and seekp I would write the packets in the correct position.

My problem is, how can I create a file with a specific dimension? Do I
have to use something like

while(lenght < the_lenght_i_want)
write(new char[1000], 1000)

This allocates the 1000 chars everytime you write it
to the file, why not allocate it once, _before_ you write
to the file. Do you come from a Java background? :)
Any suggestion?

Try
1) opening the file
2) lseek()ing to the position you want minus one byte
3) write one byte.

I have no idea if its portable or if it works at all,
but it's worth a try. Your file will initially contain
garbage, but the method you proposed has the same
drawback and is way slower.

HTH,
- J.
 
P

Paolo

Thank youn for your reply!
I'll try that!

PS: That was just a piece of code, I wouldn't write something like
that. Another drawback would be that I don't free allocated memory, so
with a 1 Gb file I would halt or crash the system!!


Jacek Dziedzic ha scritto:
Paolo said:
Hi!

I'm writing an application that receives data from internet. I usually
receive big files, few hundred MB to Gb and the packets I receive could
be not in a correct order. I though I could send a packet that specify
the lenght of the file before any other packet. The receiver creates
this file, with the specified dimension and then using ofstream's write
and seekp I would write the packets in the correct position.

My problem is, how can I create a file with a specific dimension? Do I
have to use something like

while(lenght < the_lenght_i_want)
write(new char[1000], 1000)

This allocates the 1000 chars everytime you write it
to the file, why not allocate it once, _before_ you write
to the file. Do you come from a Java background? :)
Any suggestion?

Try
1) opening the file
2) lseek()ing to the position you want minus one byte
3) write one byte.

I have no idea if its portable or if it works at all,
but it's worth a try. Your file will initially contain
garbage, but the method you proposed has the same
drawback and is way slower.

HTH,
- J.
 
P

Paolo

Paolo ha scritto:
Thank youn for your reply!
I'll try that!

PS: That was just a piece of code, I wouldn't write something like
that. Another drawback would be that I don't free allocated memory, so
with a 1 Gb file I would halt or crash the system!!


Jacek Dziedzic ha scritto:
Paolo said:
Hi!

I'm writing an application that receives data from internet. I usually
receive big files, few hundred MB to Gb and the packets I receive could
be not in a correct order. I though I could send a packet that specify
the lenght of the file before any other packet. The receiver creates
this file, with the specified dimension and then using ofstream's write
and seekp I would write the packets in the correct position.

My problem is, how can I create a file with a specific dimension? Do I
have to use something like

while(lenght < the_lenght_i_want)
write(new char[1000], 1000)

This allocates the 1000 chars everytime you write it
to the file, why not allocate it once, _before_ you write
to the file. Do you come from a Java background? :)
Any suggestion?

Try
1) opening the file
2) lseek()ing to the position you want minus one byte
3) write one byte.

I have no idea if its portable or if it works at all,
but it's worth a try. Your file will initially contain
garbage, but the method you proposed has the same
drawback and is way slower.

HTH,
- J.

Sorry Brian, I got it now!
 
T

Thomas Matthews

Paolo said:
Hi!

I'm writing an application that receives data from internet. I usually
receive big files, few hundred MB to Gb and the packets I receive could
be not in a correct order. I though I could send a packet that specify
the lenght of the file before any other packet. The receiver creates
this file, with the specified dimension and then using ofstream's write
and seekp I would write the packets in the correct position.

My problem is, how can I create a file with a specific dimension? Do I
have to use something like

while(lenght < the_lenght_i_want)
write(new char[1000], 1000)
BTW, use a named constant, rather than magic numbers:
const unsigned int BUFFER_SIZE = 1000;
//...
buffer = new unsigned char[BUFFER_SIZE];
//...
write(buffer, BUFFER_SIZE);
???
Then close the file and open it every time I receive a packet? I can't
use a buffer, at least not a buffer that contains the whole file!
Any suggestion?

Thank you!

The old school method is to write to the file, then sort and
analyze later. You can flush the file after receiving each
packet. BTW, files don't have dimensions.

Other tricks involve "double buffering" (hint use Google).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top