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

vector<char> -> file advice

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
6 2719

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
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

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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Lieve | last post by:
Hello, I set up a database a few months ago and placed it on our company network. In the beginning, there was no problem opening it, even when someone else was working in it at that time. The...
6
by: iris | last post by:
Hi all, What is the simplest way to get a list of all the files in a directory, (on an ipaq) something equivalent to "dir *.* > file" . No need to list directories. I have visual studio 2003...
4
by: news | last post by:
I've just started to test/learn python. I've got Linux > mandrake9 > python & documentation. What I'll initially want to be doing needs file I/O, so I wanted to confirm file I/O early in my...
2
by: DeanL | last post by:
Hi Guys, I've been trawling through this group for an answer to this but can't find one that works. I have an Access 97 db sitting on a Novell server that is accessed via a front end with...
3
by: George0726 | last post by:
This happens regardless of the Version of Access. Users accessing a database on a shared folder get the "Couldn't use <filename>; file already in use." Everyone without exception has access to...
1
by: kgk | last post by:
I would like to concatenate several file-like objects to create a single file-like object. I've looked at fileinput, however this returns a fileinput object that is not very file-like. ...
5
by: =?Utf-8?B?R2VyaGFyZA==?= | last post by:
I have a control on a page in .net 2.0: <input id="AddReport" type="file" name="AddReport" runat="server" /> which I use to get the path, etc. of a file. I then have a button: <asp:Button...
3
by: MLH | last post by:
The specified file is currently being used by another user or session. Wait for the other user or session to finish working with the file, and then try the operation again. So, I've got db1.mdb...
0
by: Michaeli Yair | last post by:
Hi all, I imported a comma seperated data to PyGridTableBase which is a abstract Grid, I parse and display the information. After update i want to save it back to the comma seperated file, eith...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.