Create File of X size

This is a discussion on Create File of X size within the C++ Programming forums, part of the General Programming Boards category; I know how in Windows to create a file with an exact size x Using the Win32 API, CreateFile, SetFilePointerEx, ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    10

    Lightbulb Create File of X size

    I know how in Windows to create a file with an exact size x

    Using the Win32 API, CreateFile, SetFilePointerEx, SetEndOfFile, and CloseHandle in that same order but how could I do something similar in Linux to a NTFS partition (ntfs-3g driver) without having to write all zeros to the file. I see falloc.h but does this only work for ext filesystems.. not ntfs? any tricks that don't require writing all zeros first?

    another trick maybe see if there is adequate space on the disk for the file before writing so the data array of size t can be guaranteed to fit?

    free to use asm, c, c++... just looking for ideas.

    Thanks,
    John

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    To the second question - if you are using Win API - http://msdn.microsoft.com/en-us/libr...37(VS.85).aspx
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Just fseek to the offset you want and close the file.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by vart View Post
    To the second question - if you are using Win API - http://msdn.microsoft.com/en-us/libr...37(VS.85).aspx
    Thanks but I stated that above.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by Dino View Post
    Just fseek to the offset you want and close the file.
    Just seeking won't do it -- you have to write a byte.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by Dino View Post
    Just fseek to the offset you want and close the file.
    The only problem I see with this is that it doesn't reserve space on the disk, using the code below I was able to write 10 files of 640MB each to a 1GB pen drive. This method creates sparse files correct?.. is there a way to reserve the space on the disk without writing all zeros to the file (Linux on a NTFS partition using ntfs-3g driver or mod of such)?

    Code:
    /*simple file IO test*/
    #include <stdio.h>
    
    FILE* getFile(const char*,int);
    
    int main(void){
    	
    	
    		for (int i=0;i<10;i++){
    			char str[50];
    			int res = sprintf(str,"/mnt/destination/myTest%d.txt",i);
    			FILE *testFile = getFile(str,671088640);
    			if(testFile !=NULL){
    			fputs("good test",testFile);
    			fclose(testFile);
    			}else
    			  printf("error creating file %d \n",i);
    		}
    	
    		
    	
    		
    	
    
        return 0;
    }
    FILE* getFile(const char* path,int sizeBytes){
    	FILE *pfile;
    	int result;
    	
    	if(pfile = fopen(path,"w")){
    		fseek(pfile,sizeBytes,SEEK_SET);
    		fputs("\n",pfile);
    		fclose(pfile);
    		pfile = fopen(path,"r+");
    		fseek(pfile,0,SEEK_SET);
    	}
    
      return pfile;
    }

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    Just seeking won't do it -- you have to write a byte.
    yep..

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not entirely sure that even that is completely reliable - In the sense that the filesystem may still keep track of what you have written to, and "compress" all unwritten block into "nothing".

    The only absolutely sure way to guarantee that a file fits on the disk is to write the content onto the disk ([disk here means "any device that can hold a filesystem"]).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by jcas1411 View Post
    The only problem I see with this is that it doesn't reserve space on the disk, using the code below I was able to write 10 files of 640MB each to a 1GB pen drive. This method creates sparse files correct?.. is there a way to reserve the space on the disk without writing all zeros to the file (Linux on a NTFS partition using ntfs-3g driver or mod of such)?
    I don't know enough about NTFS to say for sure, but on most filesystems which support sparse files, the unallocated blocks are reserved in the available block count. Suppose you had 10000 blocks available, and you allocate 1000 sparse blocks. Even though the specific blocks are not allocated in the block map, the count of available blocks is reduced by 1000.

    If filesystems didn't work this way, there would never be a way to tell how much space you REALLY had available.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by matsp View Post
    I'm not entirely sure that even that is completely reliable - In the sense that the filesystem may still keep track of what you have written to, and "compress" all unwritten block into "nothing".

    The only absolutely sure way to guarantee that a file fits on the disk is to write the content onto the disk ([disk here means "any device that can hold a filesystem"]).

    --
    Mats
    completely understand.. does the Linux kernel prevent you from talking to an NTFS driver directly to check drivespace? or is there a better way in C to find out the available disk space, what/how would you hook into the kernel level driver in C .. basically I need to flip to an alternate path rather than fail...

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by brewbuck View Post
    I don't know enough about NTFS to say for sure, but on most filesystems which support sparse files, the unallocated blocks are reserved in the available block count. Suppose you had 10000 blocks available, and you allocate 1000 sparse blocks. Even though the specific blocks are not allocated in the block map, the count of available blocks is reduced by 1000.

    If filesystems didn't work this way, there would never be a way to tell how much space you REALLY had available.
    Thanks for the suggestion but NTFS is not a block based file system

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,243
    Quote Originally Posted by jcas1411 View Post
    Thanks for the suggestion but NTFS is not a block based file system
    I don't know where you're getting that information... It certainly is block based.

    A filesystem that would allow a user to completely mess things up simply by seeking and writing a byte is not a proper filesystem. I'm not sure what you're worried about.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcas1411 View Post
    completely understand.. does the Linux kernel prevent you from talking to an NTFS driver directly to check drivespace? or is there a better way in C to find out the available disk space, what/how would you hook into the kernel level driver in C .. basically I need to flip to an alternate path rather than fail...
    I'm pretty sure you can figure out a temporal value of the free space - I say temporal, since it will not necessarily stay that way if some other process is also writing to the same device. The command "df" in Linux/Unix tells you how much space is available on a disk, and you should be able to use whatever system operations that it uses to find the same information, and I'm sure the NTFS driver has a way to deliver how much free space it thinks there is on the disk. Sorry, I don't KNOW the answer to what system operation actually gets that information from the driver to the user-level code, and a quick google doesn't reveal much useful info - perhaps "strace df ." will give a better lead onto the "disk free space" quest - unfortunately, I haven't got a Linux system up and running right now.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    10
    Quote Originally Posted by matsp View Post
    I'm pretty sure you can figure out a temporal value of the free space - I say temporal, since it will not necessarily stay that way if some other process is also writing to the same device. The command "df" in Linux/Unix tells you how much space is available on a disk, and you should be able to use whatever system operations that it uses to find the same information, and I'm sure the NTFS driver has a way to deliver how much free space it thinks there is on the disk. Sorry, I don't KNOW the answer to what system operation actually gets that information from the driver to the user-level code, and a quick google doesn't reveal much useful info - perhaps "strace df ." will give a better lead onto the "disk free space" quest - unfortunately, I haven't got a Linux system up and running right now.

    --
    Mats
    sweet.. df does report that 0 mb is available (even though it lets me write to it).. now to find what df is using!

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcas1411 View Post
    sweet.. df does report that 0 mb is available (even though it lets me write to it).. now to find what df is using!
    So it seems like NTFS support in Linux is a little bit broken in that respect...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21