Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
how to preallocate/resize file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
rych  
View profile  
 More options Dec 15 2008, 12:52 pm
Newsgroups: comp.soft-sys.matlab
From: rych <rych...@gmail.com>
Date: Mon, 15 Dec 2008 09:52:01 -0800 (PST)
Local: Mon, Dec 15 2008 12:52 pm
Subject: how to preallocate/resize file
I'd like to preallocate a large disk space for a file that I'm then
going to memory-map and write part by part. In C I can use a run-time
function like _chsize_s to do it instantaneously. But I can't figure a
way to do it in Matlab. Writing a huge zeros vector, I could go out of
memory. Writing a block by block in a for loop, until the file gets
resized to a required size,  -- this doesn't sound elegant at all.
Igor

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Tursa  
View profile  
 More options Dec 16 2008, 6:03 am
Newsgroups: comp.soft-sys.matlab
From: "James Tursa" <aclassyguywithakno...@hotmail.com>
Date: Tue, 16 Dec 2008 11:03:02 +0000 (UTC)
Local: Tues, Dec 16 2008 6:03 am
Subject: Re: how to preallocate/resize file

rych <rych...@gmail.com> wrote in message <76c761cf-5089-4efb-a897-071193319...@v5g2000prm.googlegroups.com>...
> I'd like to preallocate a large disk space for a file that I'm then
> going to memory-map and write part by part. In C I can use a run-time
> function like _chsize_s to do it instantaneously. But I can't figure a
> way to do it in Matlab. Writing a huge zeros vector, I could go out of
> memory. Writing a block by block in a for loop, until the file gets
> resized to a required size,  -- this doesn't sound elegant at all.
> Igor

Since you want the exact functionality of the _chsize_s routine in C, you could just write a mex file to do this using that function. It was simple enough, so I wrote it below. It has a reasonable amount of input error checking, but I did not include a check for huge requested filesize values. That would be something you might want to add. If you are not familiar with mex files, simply place this file somewhere on your MATLAB path, call it some name like preallocatefile.c, set your default directory to that directory, and then do this at the MATLAB prompt:

mex -setup
(then pick your Microsoft C/C++ compiler)
mex preallocatefile.c

Now you have a function that takes two inputs, filename and filesize, and preallocates the file to that size. e.g., at the MATLAB prompt:

preallocatefile("myfile",1024)

would create a 1K file.

James Tursa

----------------------------------------------------------------

// A MATLAB mex function to preallocate (or reallocate/truncate)
// a file to a requested size.
//
// Inputs:
//     1st input: Char string file name
//     2nd input: Requested file size in bytes (any numeric class)
//
// Outputs:
//     (none)
//
// Programmer: James Tursa

#include <stdio.h>
#include <io.h>
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    char *filename;
    __int64 filesize;
    FILE *fp;
    errno_t errno;

    if( nrhs != 2 ) {
        mexErrMsgTxt("Need exactly two inputs: filename, size");
    }
    if( nlhs != 0 ) {
        mexErrMsgTxt("There are no outputs allowed");
    }
    if( !mxIsChar(prhs[0]) ) {
        mexErrMsgTxt("1st input must be filename char string");
    }
    if( !mxIsNumeric(prhs[1]) || mxGetNumberOfElements(prhs[1]) != 1 ) {
        mexErrMsgTxt("2nd input must be numeric scalar file size");
    }
    filename = mxArrayToString(prhs[0]);
    filesize = mxGetScalar(prhs[1]);
    if( filesize < 0 ) {
        mexErrMsgTxt("2nd input must be non-negative");
    }
    fp = fopen( filename, "w" );
    mxFree(filename);
    if( fp == NULL ) {
        mexErrMsgTxt("Unable to open file");
    }
    errno = _chsize_s( _fileno(fp), filesize );
    fclose(fp);
    if( errno != 0 ) {
        mexErrMsgTxt("Unable to set size of file");
    }


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
rych  
View profile  
 More options Dec 16 2008, 1:09 pm
Newsgroups: comp.soft-sys.matlab
From: rych <rych...@gmail.com>
Date: Tue, 16 Dec 2008 10:09:46 -0800 (PST)
Local: Tues, Dec 16 2008 1:09 pm
Subject: Re: how to preallocate/resize file
On Dec 16, 11:03 am, "James Tursa" <aclassyguywithakno...@hotmail.com>
wrote:

Thank you James, you're very kind. I'm going to try and compile your
mex file then.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »