Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > creating a file

Reply
Thread Tools

creating a file

 
 
Hans Vlems
Guest
Posts: n/a
 
      02-24-2011
I was asked for a program that creates a file with a predermined
filesize.
Filesize is specifcied in bytes, but it is acceptable if the filesize
ought to be a multiple of, say, the blocksize of the disk.
On a VMS system creat has a few platform specific enhancements. But
this needs to run on a WIndows XP system. Is there a function to do
that, for either Visual C or the djgpp environment?
Hans
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      02-24-2011
On 02/24/2011 02:50 PM, Hans Vlems wrote:
> I was asked for a program that creates a file with a predermined
> filesize.
> Filesize is specifcied in bytes, but it is acceptable if the filesize
> ought to be a multiple of, say, the blocksize of the disk.
> On a VMS system creat has a few platform specific enhancements. But
> this needs to run on a WIndows XP system. Is there a function to do
> that, for either Visual C or the djgpp environment?


That's really a platform, rather than a C language, question - somewhere
like "comp.os.ms-windows.programmer.win32" may be a better place to look.

However...
A quick google suggests that there is already a utility to do this -
fsutil (see the "file createnew" options).
 
Reply With Quote
 
 
 
 
Hans Vlems
Guest
Posts: n/a
 
      02-24-2011
On 24 feb, 16:10, Mark Bluemel <mark_blue...@pobox.com> wrote:
> On 02/24/2011 02:50 PM, Hans Vlems wrote:
>
> > I was asked for a program that creates a file with a predermined
> > filesize.
> > Filesize is specifcied in bytes, but it is acceptable if the filesize
> > ought to be a multiple of, say, the blocksize of the disk.
> > On a VMS system creat has a few platform specific enhancements. But
> > this needs to run on a WIndows XP system. Is there a function to do
> > that, for either Visual C or the djgpp environment?

>
> That's really a platform, rather than a C language, question - somewhere
> like "comp.os.ms-windows.programmer.win32" may be a better place to look.
>
> However...
> A quick google suggests that there is already a utility to do this -
> fsutil (see the "file createnew" options).


Actually I wasn't sure whether this was a platform or a C language
question.
IO is of course platform dependent, though basic operations are
available in
stdio.h or unistd.h. I did RTFM< couldn't find it and then asked the
question here.
Now at least I've got two options: connect the disk to a VMS system or
try the utility you googled.
Thanks!
Hans
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      02-24-2011
On Feb 24, 9:50*am, Hans Vlems <hvl...@freenet.de> wrote:
> I was asked for a program that creates a file with a predermined
> filesize.
> Filesize is specifcied in bytes, but it is acceptable if the filesize
> ought to be a multiple of, say, the blocksize of the disk.
> On a VMS system creat has a few platform specific enhancements. But
> this needs to run on a WIndows XP system. Is there a function to do
> that, for either Visual C or the djgpp environment?
> Hans


On EXT2 and beyond you can make sparse files with the truncate tool.

There is no facility in portable C to accomplish this since not all
filesystems support it.

Tom
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-24-2011
Hans Vlems <> writes:
> I was asked for a program that creates a file with a predermined
> filesize.
> Filesize is specifcied in bytes, but it is acceptable if the filesize
> ought to be a multiple of, say, the blocksize of the disk.
> On a VMS system creat has a few platform specific enhancements. But
> this needs to run on a WIndows XP system. Is there a function to do
> that, for either Visual C or the djgpp environment?


Create the file, then write the specified number of bytes to it.

Or did you have something else in mind?

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      02-24-2011
On Feb 24, 7:59*pm, Keith Thompson <ks...@mib.org> wrote:
> Hans Vlems <hvl...@freenet.de> writes:


> Create the file, then write the specified number of bytes to it.
>

That was my first idea too, before the platform was revealed, Windows
and NTFS.
Windows routinely uses disks of 500 GB and more, so writing records is
probably slow.

> Or did you have something else in mind?


So the idea of just writing enough (large) records to a file to match
a prerequisite size might turn out as
a very slow process. I don't know yet what filesizes will be desired.
Anything < 1 GB and it wouldn't matter
much probably. Creating a file of 1 TB that way is something else
again.
What I had in mind was open a file, allocate the desired number of MB
and close it. All the filesystem has to
do is put an EOF marker in it. On VMS this works quite well, can be
done from the commandline.
There is no demand to make the file contiguous, at least not yet,
because that is also fairly slow even on
empty disks.
So instead of disenganging my brain completely and go for the direct
approach, I thought I'd better ask here
first. Most people on c.l.c. either use Windows or at least are
familiar with it.
Portability is not an issue, as said I don't need this for my own
systems.
Hans
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      02-24-2011
On Feb 24, 7:53*pm, Tom St Denis <t...@iahu.ca> wrote:
> On Feb 24, 9:50*am, Hans Vlems <hvl...@freenet.de> wrote:
>
> > I was asked for a program that creates a file with a predermined
> > filesize.
> > Filesize is specifcied in bytes, but it is acceptable if the filesize
> > ought to be a multiple of, say, the blocksize of the disk.
> > On a VMS system creat has a few platform specific enhancements. But
> > this needs to run on a WIndows XP system. Is there a function to do
> > that, for either Visual C or the djgpp environment?
> > Hans

>
> On EXT2 and beyond you can make sparse files with the truncate tool.
>
> There is no facility in portable C to accomplish this since not all
> filesystems support it.
>
> Tom


IIRC ext2 is a linux filesystem, right?
The IDE disks are on a Windows box. Once I decide to put them on
another system
then I'll move them to an Alpha. No programming needed on VMS (see my
reply to Keith's post).
Hans
 
Reply With Quote
 
Tom St Denis
Guest
Posts: n/a
 
      02-25-2011
On Feb 24, 6:22*pm, Hans Vlems <hvl...@freenet.de> wrote:
> On Feb 24, 7:53*pm, Tom St Denis <t...@iahu.ca> wrote:
>
>
>
> > On Feb 24, 9:50*am, Hans Vlems <hvl...@freenet.de> wrote:

>
> > > I was asked for a program that creates a file with a predermined
> > > filesize.
> > > Filesize is specifcied in bytes, but it is acceptable if the filesize
> > > ought to be a multiple of, say, the blocksize of the disk.
> > > On a VMS system creat has a few platform specific enhancements. But
> > > this needs to run on a WIndows XP system. Is there a function to do
> > > that, for either Visual C or the djgpp environment?
> > > Hans

>
> > On EXT2 and beyond you can make sparse files with the truncate tool.

>
> > There is no facility in portable C to accomplish this since not all
> > filesystems support it.

>
> > Tom

>
> IIRC ext2 is a linux filesystem, right?
> The IDE disks are on a Windows box. Once I decide to put them on
> another system
> then I'll move them to an Alpha. No programming needed on VMS (see my
> reply to Keith's post).
> Hans


Well assuming what you want to do is just create a blank file of a
given size that's typically called a "Sparse File." NTFS supports it
[iirc] so does EXT2 [and up]. FAT does not. To allocate a file of a
given size you'd need to allocate enough actual clusters which means
writing recording keeping info to disk.

Sparse files are different in that creating them takes next to no
time, and you can write them out of order without first creating the
first sectors/inodes/clusters/whatever.

There is no C portable way to do it. The most common function would
be truncate() which is from BSD but exists in Linux. I don't know how
you create sparse files in Windows but there is an API for that I
imagine.

Tom
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      02-25-2011
>
> Well assuming what you want to do is just create a blank file of a
> given size that's typically called a "Sparse File." *NTFS supports it
> [iirc] so does EXT2 [and up]. *FAT does not. *To allocate a file of a
> given size you'd need to allocate enough actual clusters which means
> writing recording keeping info to disk.
>
> Sparse files are different in that creating them takes next to no
> time, and you can write them out of order without first creating the
> first sectors/inodes/clusters/whatever.
>
> There is no C portable way to do it. *The most common function would
> be truncate() which is from BSD but exists in Linux. *I don't know how
> you create sparse files in Windows but there is an API for that I
> imagine.
>
> Tom- Tekst uit oorspronkelijk bericht niet weergeven -


This is a one off so portability is not an issue.
But I wouldn't have thought of the name 'truncate()' to create a
sparse file
It's in unistd.h so I'll give it a go.
Thanks Tom!
Hans
 
Reply With Quote
 
Hans Vlems
Guest
Posts: n/a
 
      02-25-2011
Tom,

truncate works like a charm. Creatung a 1 GB empty file takes less
than a second.
Hans
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a File object out of the contents of jar/zip file kotauk@gmail.com Java 5 04-06-2006 08:36 PM
"Error Creating Control" when creating a custom control (Design Time). Can't see tooltip message. Ravi Ambros Wallau ASP .Net Web Controls 0 06-01-2005 02:36 PM
"Error Creating Control" when creating a custom control (Design Time). Can't see tooltip message. Ravi Ambros Wallau ASP .Net 0 06-01-2005 02:36 PM
Any way to rename a current File without creating a new File object? C-man Java 9 04-11-2004 04:21 AM
What is better /standard for creating files. a cpp file with header or cpp and seperate file for header DrUg13 C++ 1 02-10-2004 09:20 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57