creating a file

Discussion in 'C Programming' started by Hans Vlems, Feb 24, 2011.

  1. Hans Vlems

    Hans Vlems Guest

    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
    Hans Vlems, Feb 24, 2011
    #1
    1. Advertising

  2. Hans Vlems

    Mark Bluemel Guest

    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).
    Mark Bluemel, Feb 24, 2011
    #2
    1. Advertising

  3. Hans Vlems

    Hans Vlems Guest

    On 24 feb, 16:10, Mark Bluemel <> 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
    Hans Vlems, Feb 24, 2011
    #3
  4. Hans Vlems

    Tom St Denis Guest

    On Feb 24, 9:50 am, 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?
    > 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
    Tom St Denis, Feb 24, 2011
    #4
  5. 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) <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"
    Keith Thompson, Feb 24, 2011
    #5
  6. Hans Vlems

    Hans Vlems Guest

    On Feb 24, 7:59 pm, Keith Thompson <> wrote:
    > Hans Vlems <> 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
    Hans Vlems, Feb 24, 2011
    #6
  7. Hans Vlems

    Hans Vlems Guest

    On Feb 24, 7:53 pm, Tom St Denis <> wrote:
    > On Feb 24, 9:50 am, 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?
    > > 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
    Hans Vlems, Feb 24, 2011
    #7
  8. Hans Vlems

    Tom St Denis Guest

    On Feb 24, 6:22 pm, Hans Vlems <> wrote:
    > On Feb 24, 7:53 pm, Tom St Denis <> wrote:
    >
    >
    >
    > > On Feb 24, 9:50 am, 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?
    > > > 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
    Tom St Denis, Feb 25, 2011
    #8
  9. Hans Vlems

    Hans Vlems Guest

    >
    > 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
    Hans Vlems, Feb 25, 2011
    #9
  10. Hans Vlems

    Hans Vlems Guest

    Tom,

    truncate works like a charm. Creatung a 1 GB empty file takes less
    than a second.
    Hans
    Hans Vlems, Feb 25, 2011
    #10
  11. Hans Vlems

    jacob navia Guest

    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.
    jacob navia, Feb 25, 2011
    #11
  12. Hans Vlems

    Hans Vlems Guest

    On 25 feb, 09:08, jacob navia <> wrote:
    > 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.


    Which works for VisualC, right?
    Merci,
    Hans
    Hans Vlems, Feb 25, 2011
    #12
  13. Hans Vlems

    Mark Bluemel Guest

    On Feb 24, 11:19 pm, Hans Vlems <> wrote:
    > ... 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.
    Mark Bluemel, Feb 25, 2011
    #13
  14. Mark Bluemel <> writes:

    > On Feb 24, 11:19 pm, Hans Vlems <> wrote:
    >> ... 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.


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

    --
    /Wegge

    Leder efter redundant peering af dk.*,linux.debian.*
    Anders Wegge Keller, Feb 25, 2011
    #14
  15. Hans Vlems

    Mark Bluemel Guest

    On Feb 25, 8:38 am, Mark Bluemel <> wrote:
    > On Feb 24, 11:19 pm, Hans Vlems <> wrote:
    >
    > > ... 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.


    It seems to me that Hans is in danger of breaking at least the 10th of
    Spencer's commandment <http://www.lysator.liu.se/c/ten-
    commandments.html>
    Mark Bluemel, Feb 25, 2011
    #15
  16. Hans Vlems

    jacob navia Guest

    Le 25/02/11 09:33, Hans Vlems a écrit :
    > On 25 feb, 09:08, jacob navia<> wrote:
    >> 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.

    >
    > Which works for VisualC, right?
    > Merci,
    > Hans


    No, it works for any compiler running windows, those are APIs of
    the operating system
    jacob navia, Feb 25, 2011
    #16
  17. Hans Vlems

    jacob navia Guest

    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.
    jacob navia, Feb 25, 2011
    #17
  18. Hans Vlems

    James Kuyper Guest

    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.

    --
    James Kuyper
    James Kuyper, Feb 25, 2011
    #18
  19. Hans Vlems

    Chris H Guest

    In message <
    s.com>, Mark Bluemel <> writes
    >On Feb 24, 11:19 pm, Hans Vlems <> wrote:
    >> ... 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.


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


    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris H, Feb 25, 2011
    #19
  20. Hans Vlems

    Rui Maciel Guest

    jacob navia wrote:

    > 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
    Rui Maciel, Feb 25, 2011
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,085
    Ravi Ambros Wallau
    Jun 1, 2005
  2. C-man
    Replies:
    9
    Views:
    640
    Roedy Green
    Apr 11, 2004
  3. Replies:
    5
    Views:
    907
    Roedy Green
    Apr 6, 2006
  4. DrUg13
    Replies:
    1
    Views:
    466
    DrUg13
    Feb 10, 2004
  5. Ravi Ambros Wallau
    Replies:
    0
    Views:
    306
    Ravi Ambros Wallau
    Jun 1, 2005
Loading...

Share This Page