Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write large chunks(around 50MB) of data to a file

52 views
Skip to first unread message

sops

unread,
Jan 13, 2011, 2:05:35 PM1/13/11
to
Hi,

i want to write around 50MB of data to a raw file to verify my file
system. i have done it in rough way by writing chunk of character
bytes up to 50MB in a while loop.

something like storing a string in a buffer and writing into a file
and repeating this several times.


sorry for asking silly Question, is there any good way of doing it?

-sops
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Patrick

unread,
Jan 14, 2011, 2:53:51 PM1/14/11
to
On Jan 13, 9:05 pm, sops <swapnamsf...@gmail.com> wrote:
> Hi,
>
> i want to write around 50MB of data to a raw file to verify my file
> system. i have done it in rough way by writing  chunk of character
> bytes up to 50MB in a while loop.
>
> something like storing a string in a buffer and writing into a file
> and repeating this several times.
>
> sorry for asking silly Question, is there any good way of doing it?
>
> -sops
> --
> comp.lang.c.moderated - moderation address: c...@plethora.net -- you must

> have an appropriate newsgroups line in your header for your mail to be seen,
> or the newsgroup name in square brackets in the subject line.  Sorry.


Check out POSIX mmap. You can map your file/device into memory and
work with the buffer directly.

Cheers,
Patrick

Dag-Erling Smørgrav

unread,
Jan 20, 2011, 12:58:54 PM1/20/11
to
Patrick <pkug...@gmail.com> writes:
> Check out POSIX mmap. You can map your file/device into memory and
> work with the buffer directly.

That's a very bad idea if the file is newly created.

DES
--
Dag-Erling Smørgrav - d...@des.no

Jeremy Hall

unread,
Jan 20, 2011, 12:59:24 PM1/20/11
to
On Jan 13, 7:05 pm, sops <swapnamsf...@gmail.com> wrote:
> Hi,
>
> i want to write around 50MB of data to a raw file to verify my file
> system. i have done it in rough way by writing  chunk of character
> bytes up to 50MB in a while loop.
>
> something like storing a string in a buffer and writing into a file
> and repeating this several times.
>
> sorry for asking silly Question, is there any good way of doing it?

You could buffer it up and write in big chunks (preferably an exact
multiple of the
file systems block size - say 4k, 8k or more). (Thats what fwrite()
etc does for you).

However, for small amounts of data like this, and from the users
perspective, writing it out usually will take no time at all, as the
system will not immediately write the stuff to the disk.
Modern systems use "delayed write" where they keep the data in system
buffers for as long as possible.
Then, when the write is actually done, the waiting blocks may be
sorted in disk position order before the write so that the head
movement is minimised. Also other programs may want to the read the
data and its handy if its in memory already. Finally, your program
may want to add to a partially written block.

mmap is great for reading data, (CreateFileMapping/MapViewOfFile in
windows). But you generally cannot grow a file with mmap which makes
normal output difficult. Altering an existing file OTOH is dead easy.

Jeremy

0 new messages