creating a file

H

Hans Vlems

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
 
M

Mark Bluemel

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).
 
H

Hans Vlems

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
 
T

Tom St Denis

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
 
K

Keith Thompson

Hans Vlems said:
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?
 
H

Hans Vlems

Hans Vlems <[email protected]> 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
 
H

Hans Vlems

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
 
T

Tom St Denis

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
 
H

Hans Vlems

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
 
H

Hans Vlems

Tom,

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

jacob navia

Le 24/02/11 15:50, Hans Vlems a écrit :
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

Under windows you do that as follows:

(1) Create a File with CreateFile
(2) You move the file pointer to the desired size with SetFilePointerEx
(3) You set the end of the file with SetEndOfFile

You can find the documentation for those APIs in the documentation from
Microsoft.
 
H

Hans Vlems

Le 24/02/11 15:50, Hans Vlems a crit :


Under windows you do that as follows:

(1) Create a File with CreateFile
(2) You move the file pointer to the desired size with SetFilePointerEx
(3) You set the end of the file with SetEndOfFile

You can find the documentation for those APIs in the documentation from
Microsoft.

Which works for VisualC, right?
Merci,
Hans
 
M

Mark Bluemel

... Most people on c.l.c. either use Windows or at least are
familiar with it.

Cue for a debate (or even a flame war). I haven't taken a census, but
I'm far from convinced that the majority here have experience of
developing on Windows.
 
A

Anders Wegge Keller

Mark Bluemel said:
Cue for a debate (or even a flame war). I haven't taken a census, but
I'm far from convinced that the majority here have experience of
developing on Windows.

SCO and Linux for me. I've been working on an embedded MC860-based
platform before.
 
J

jacob navia

Le 25/02/11 09:33, Hans Vlems a écrit :
Which works for VisualC, right?
Merci,
Hans

No, it works for any compiler running windows, those are APIs of
the operating system
 
J

jacob navia

Le 25/02/11 00:19, Hans Vlems a écrit :
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.

Yes, but there is a vocal group of people that say that windows is the
empire of evil, etc.

I develop under windows and iphone/Apple. Lately, I got bored with
windows and changed to Apple, a great platform. It makes Unix
shine, it is a very good combination of a good GUI with a powerful
Unix system. Linux folks could learn something from them, after
more than a decade their system is as "usable" as SCO + Xwindows
was.

But surely I know something about windows, see my other answer
in this thread.
 
J

James Kuyper

On 02/24/2011 06:19 PM, Hans Vlems wrote:
....
first. Most people on c.l.c. either use Windows or at least are
familiar with it.

I use Windows only rarely - mostly when someone insists on using
Microsoft Word documents which rely upon features that Sun's OpenOffice
doesn't fully support when saving in M$ Word format. I've never written
a program for it; the closest I came was programs written for DOS, and
the last time I did that was 15 years ago.
 
C

Chris H

In message <[email protected]
s.com> said:
Cue for a debate (or even a flame war). I haven't taken a census, but
I'm far from convinced that the majority here have experience of
developing on Windows.

Majority here or the majority of commercial developers? (i.e. leaving
aside home/hobby use) Then the vast majority will be developing on
Windows
 
R

Rui Maciel

jacob said:
I develop under windows and iphone/Apple. Lately, I got bored with
windows and changed to Apple, a great platform. It makes Unix
shine, it is a very good combination of a good GUI with a powerful
Unix system. Linux folks could learn something from them, after
more than a decade their system is as "usable" as SCO + Xwindows
was.

When was the last time you gave any linux distro a try?


Rui Maciel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top