vector<char> -> file advice

J

JackC

Hi,

If i have a vector<char> maybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?

I have tried this:

for(int z = 0; z < Output_Buffer.size(); z++)
{
write(ostr, (char *) Output_Buffer[z], 1);
}

But with 15mb in the buffer, that takes a good 30 seconds to write
out :(, any ideas on how i can speed it up? If i could use a
vector<string> i would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<char> and
now need a fast way to output the buffer.

Thanks for any help.
Jack
 
A

Alf P. Steinbach

* JackC:
Hi,

If i have a vector<char> maybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?

I have tried this:

for(int z = 0; z < Output_Buffer.size(); z++)
{
write(ostr, (char *) Output_Buffer[z], 1);
}

This code is not consistent with vector<char>.

It's not consistent with anything, really, it's just Undefined Behavior.

Instead of the cast, which you should recognize as a symptom of buggy
code, did you perhaps mean to write "&Output_Buffer[z]"?

Advice: stay away from casts.

They're Evil(TM), especially the C style ones.

But with 15mb in the buffer, that takes a good 30 seconds to write
out :(, any ideas on how i can speed it up?

On big gulp instead of 15+ million small nibbles.

If i could use a
vector<string> i would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<char> and
now need a fast way to output the buffer.

This is not meaningful to me. Perhaps you meant something?
 
B

BobR

JackC said:
Hi,
If i have a vector<char> maybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?
I have tried this:

for(int z = 0; z < Output_Buffer.size(); z++){
write(ostr, (char *) Output_Buffer[z], 1);
}

But with 15mb in the buffer, that takes a good 30 seconds to write
out :(, any ideas on how i can speed it up? If i could use a
vector<string> i would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<char> and
now need a fast way to output the buffer.
Thanks for any help. Jack

A simple solution could be:

// includes here

int main(){
std::eek:fstream MyFile( "somefile.txt" );

std::vector<char> Buffer( 50, 'Z' ); // for test

std::copy( Buffer.begin(), Buffer.end(),
std::eek:stream_iterator<char>( MyFile, "" ) );

return 0;
} // main()
 
J

JackC

JackC said:
Hi,
If i have a vector<char> maybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?
I have tried this:
for(int z = 0; z < Output_Buffer.size(); z++){
write(ostr, (char *) Output_Buffer[z], 1);
}
But with 15mb in the buffer, that takes a good 30 seconds to write
out :(, any ideas on how i can speed it up? If i could use a
vector<string> i would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<char> and
now need a fast way to output the buffer.
Thanks for any help. Jack

A simple solution could be:

// includes here

int main(){
std::eek:fstream MyFile( "somefile.txt" );

std::vector<char> Buffer( 50, 'Z' ); // for test

std::copy( Buffer.begin(), Buffer.end(),
std::eek:stream_iterator<char>( MyFile, "" ) );

return 0;
} // main()

Thanks Bob, exactly what i was after.
 
B

BobR

JackC said:
Thanks Bob, exactly what i was after.

Another thing you could try:

// note: only for std::vector<char> (and array[]).
MyFile.write( &Buffer.at(0), Buffer.size() );
 
G

Gianni Mariani

BobR said:
JackC said:
Thanks Bob, exactly what i was after.

Another thing you could try:

// note: only for std::vector<char> (and array[]).
MyFile.write( &Buffer.at(0), Buffer.size() );

Why not accomodate zero sized vectors ?

MyFile.write( &Buffer.front(), Buffer.size() );

- I'm not sure what front() does for zero sized vectors... probably UB
 
J

Jim Langston

JackC said:
Hi,

If i have a vector<char> maybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?

I have tried this:

for(int z = 0; z < Output_Buffer.size(); z++)
{
write(ostr, (char *) Output_Buffer[z], 1);
}

But with 15mb in the buffer, that takes a good 30 seconds to write
out :(, any ideas on how i can speed it up? If i could use a
vector<string> i would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<char> and
now need a fast way to output the buffer.

Why are you writing one character at a time? Why not just write the whole
1.5m? I.E.

write( ostr, &Output_Buffer[0], Output_Buffer.size() );
 
F

Frank Birbacher

Hi!

Gianni said:
Why not accomodate zero sized vectors ?

MyFile.write( &Buffer.front(), Buffer.size() );

- I'm not sure what front() does for zero sized vectors... probably UB

Right, UB. I cannot think of a portable, legal way, but sometimes the
iterator is just a regular pointer. So begin() would suffice.

Frank
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top