By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

vector<char> -> file advice

P: n/a
Hi,

If i have a vector<charmaybe 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<stringi would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<charand
now need a fast way to output the buffer.

Thanks for any help.
Jack

Aug 13 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a

JackC <je******@gmail.comwrote in message...
Hi,
If i have a vector<charmaybe 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<stringi would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<charand
now need a fast way to output the buffer.
Thanks for any help. Jack
A simple solution could be:

// includes here

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

std::vector<charBuffer( 50, 'Z' ); // for test

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

return 0;
} // main()

--
Bob R
POVrookie
Aug 13 '07 #2

P: n/a
On 13 Aug, 19:13, "BobR" <removeBadB...@worldnet.att.netwrote:
JackC <jeche...@gmail.comwrote in message...
Hi,
If i have a vector<charmaybe 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<stringi would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<charand
now need a fast way to output the buffer.
Thanks for any help. Jack

A simple solution could be:

// includes here

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

std::vector<charBuffer( 50, 'Z' ); // for test

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

return 0;
} // main()

--
Bob R
POVrookie
Thanks Bob, exactly what i was after.

Aug 13 '07 #3

P: n/a

JackC <je******@gmail.comwrote in message...
On 13 Aug, 19:13, "BobR" <removeBadB...@worldnet.att.netwrote:
JackC <jeche...@gmail.comwrote in message...
Hi,
If i have a vector<charmaybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?
A simple solution could be:

// includes here
int main(){
std::ofstream MyFile( "somefile.txt" );
std::vector<charBuffer( 50, 'Z' ); // for test
std::copy( Buffer.begin(), Buffer.end(),
std::ostream_iterator<char>( MyFile, "" ) );
return 0;
} // main()

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() );

--
Bob R
POVrookie
Aug 13 '07 #4

P: n/a
BobR wrote:
JackC <je******@gmail.comwrote in message...
>On 13 Aug, 19:13, "BobR" <removeBadB...@worldnet.att.netwrote:
>>JackC <jeche...@gmail.comwrote in message...
Hi,
If i have a vector<charmaybe 15mb in size, whats the most efficient
way to write out all the elements to an output file?
A simple solution could be:

// includes here
int main(){
std::ofstream MyFile( "somefile.txt" );
std::vector<charBuffer( 50, 'Z' ); // for test
std::copy( Buffer.begin(), Buffer.end(),
std::ostream_iterator<char>( MyFile, "" ) );
return 0;
} // main()
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
Aug 13 '07 #5

P: n/a
"JackC" <je******@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
Hi,

If i have a vector<charmaybe 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<stringi would do, but my preceding code processes lots of
individual chars so i just ended up adding them to a vector<charand
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() );
Aug 13 '07 #6

P: n/a
Hi!

Gianni Mariani schrieb:
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
Aug 13 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.