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

writing to file

Hi,

I have an application where i want to write data to a file, the data is
being sent from an external source. I know the total size of the data and
then i retrieve the data in small segments from the source. This data is
written to file in a loop.

My question.
1. Will it be useful to increase the file size initially then seek to 0 and
start writing to file. whether there will be any performance improvements
because of the allocation of the file earlier and due to less io? or this
doesnt matter because every fwrite writes to disk(OS may schedule writing to
disk).
2. If this is helps performance, do i have an api to initialise the file
with some size and empty strings?

TFI
ishekara.
Jul 22 '05 #1
3 3365
ishekar wrote:
Hi,

I have an application where i want to write data to a file, the data
is being sent from an external source. I know the total size of the
data and then i retrieve the data in small segments from the source.
This data is written to file in a loop.

My question.
1. Will it be useful to increase the file size initially then seek to
0 and start writing to file. whether there will be any performance
improvements because of the allocation of the file earlier and due to
less io? or this doesnt matter because every fwrite writes to disk(OS
may schedule writing to disk).
This is very platform specific, so you might want to ask in a newsgroup
about your OS. I think that usually, the performance when writing is
better, since the kernel doesn't need to search for free clusters on
the disk. But of course, creating the file initially will take time,
too. So if you need your hard drive to be as fast as possible so you
can keep up with a high data rate, it's probably a good idea to create
the file initially. If not, you won't probably gain anything or even
make the total run time worse due to the additional "initialization" of
the file.
2. If this is helps performance, do i have an api to initialise the
file with some size and empty strings?


Exept for just writing it yourself (which is actually very simple),
there is none in standard C++.

Jul 22 '05 #2

"ishekar" <is******@vsnl.net> wrote in message
news:c1**********@news.mch.sbs.de...
Hi,

I have an application where i want to write data to a file, the data is
being sent from an external source. I know the total size of the data and
then i retrieve the data in small segments from the source. This data is
written to file in a loop.

My question.
1. Will it be useful to increase the file size initially then seek to 0 and start writing to file. whether there will be any performance improvements
because of the allocation of the file earlier and due to less io? or this
doesnt matter because every fwrite writes to disk(OS may schedule writing to disk).
2. If this is helps performance, do i have an api to initialise the file
with some size and empty strings?

TFI
ishekara.


The answer to this sort of question is always

1) Do it the simplest way first (in this case don't precreate the file).
2) Once the program is working to some profiling to see if the file I/O
speed is a problem or not. If not then you've saved yourself the bother of
writing the more complex code.
3) If it is a problem then try your alternate method, profile that and see
if it is any faster. Bear in mind that the results on one computer may not
be reflected on another computer.

There is no way in standard C++ of creating a file so big, except writing
out the required number of bytes. Your operating system may have a more
efficient way of doing this.

john
Jul 22 '05 #3

"ishekar" <is******@vsnl.net> wrote in message
news:c1**********@news.mch.sbs.de...
Hi,

I have an application where i want to write data to a file, the data is
being sent from an external source. I know the total size of the data and
then i retrieve the data in small segments from the source. This data is
written to file in a loop.

My question.
1. Will it be useful to increase the file size initially then seek to 0 and start writing to file. whether there will be any performance improvements
because of the allocation of the file earlier and due to less io? or this
doesnt matter because every fwrite writes to disk(OS may schedule writing to disk).
2. If this is helps performance, do i have an api to initialise the file
with some size and empty strings?

1. Keep it simple - don't try to optimize until you know there is a problem
2. There is no way in C++
3. In unix like OSes the function you want is truncate or possibly ftruncate
4. On many file systems it will make no difference because although it will
say that the
file is so big it doesn't necessarily allocate the blocks until you
write to them.
5. The fastest way will almost certainly to use truncate and then map the
file into memory with mmap as it
will remove a layer of copying.
6. I think you really should just use ofstream TFI
ishekara.

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.