Thread: Help with writing a disk filler applet

  1. #1
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72

    Help with writing a disk filler applet

    I need to write a small exe applet to fill a hard disk for testing purposes.

    Does anyone have anything that does this already?
    OR some suggestions how I might accomplish this with just a few lines of C code.

    Thanks,
    Mike
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  2. #2
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Code:
    #include <stdio.h>
    int main(void)
    {
    FILE *fp; //filepointer
    while(1)
    {
       fp = fopen("overkill.txt","a");
       fputs("1", fp)   //replace 1 with some long text of your choice...
       fclose(fp);
    }
    return 0;
    }
    I dunno if that'll work, but I don't really wanna try it

    if you try it, please lemme know if it works, thanks!
    Last edited by mako; 01-18-2006 at 09:32 AM.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Why did you comment "// filepointer"? I think the "FILE *" kind of gives it away.
    </rant>
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by ahluka
    Why did you comment "// filepointer"? I think the "FILE *" kind of gives it away.
    </rant>

    to justify the name loll but your right, it was lame ,)

  5. #5
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    Thanks. This gets me started in the right direction. Add some conditionals and off I go.
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  6. #6
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    I've modified this to some extent.
    What I am hoping to do is to double the size of the file each time through the loop.
    The file is increasing in size but not exponentially as I thought that it should.
    Can some one please instruct me to what I'm doing wrong?

    Code:
    // Filler_attempt2.cpp : Defines the entry point for the console application.
    //
    
    #include <stdio.h>
    #include <windows.h>
    int main(void)
    {
    	double getFreeDiskSpace(void);
    	FILE *fp; //filepointer
    	double diskSpace = getFreeDiskSpace();
    	long lSize;
    	char * buffer = NULL;
    	printf ("Free space: %.2f Bytes\n", diskSpace);
    
    
    	//First thing we do is seed a file. 
    		fp = fopen("00filler","a");
    		fputs("1111111111111111111111111111111111111111111111111111111111", fp);
    		fclose(fp);
    
    	//Next we begin to fill up a file. We use the free disk space as a 
    	//condition to stop filling the drive when there is ~2MB left available. 
    
    	while ( diskSpace>(2*(1024*1024)) )
    	{
    		diskSpace=getFreeDiskSpace();
    		printf("Free Disk Space available: %.2f Bytes\n", diskSpace);
    		fp = fopen("00filler","r");
    		
    		//obtain the file size
    		fseek (fp , 0 , SEEK_END);
    		lSize = ftell (fp);
    		rewind (fp);
    		printf("File size is %li\n", lSize);
    	
    		buffer = (char *)malloc(lSize + 1);
    		fgets(buffer, lSize+1, fp);
    		puts(buffer);
    		freopen("00Filler", "a", fp);
    		rewind(fp);
    		//File should double in size here
    		fputs(buffer, fp);
    		
    		fclose(fp);
    		free(buffer);
    	}
    	
    
    
    	return 0;
    
    
    }
    
    	double getFreeDiskSpace(void){
    
    	DWORD dwSectorsPerCluster,
    		dwBytesPerSector,
    		 dwNumberOfFreeClusters,
    		 dwTotalNumberOfClusters;
    	void loop();
    	double cdSpace=0; //current disk Space
    	double idSpace=0; //initial disk space
    	double mdSpace=0; //max disk Space used
    	double tempSpace=0;
    
    		//get the free disk space available.
    		if (GetDiskFreeSpace (NULL, &dwSectorsPerCluster,
    			&dwBytesPerSector, &dwNumberOfFreeClusters,
    			&dwTotalNumberOfClusters))
    			{
    				idSpace = (double)dwBytesPerSector;
    				idSpace *= dwSectorsPerCluster;
    				idSpace *= dwNumberOfFreeClusters;
    			}
    		tempSpace = idSpace;
    		mdSpace=idSpace;
    
    		return (double)idSpace;
    	}
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  7. #7
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    If all you want to do is to fill up the disk as quickly as possible, there are other ways to do it. I won't go into great detail, but something like:
    Code:
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    
    #define BIGBUFSZ (8192 * sizeof(char))
    
    int main(void)
    {
        char *fbuff;
    
        if ((fbuff = calloc(BIGBUFSZ, 1)) != NULL)
        {
            int fd;
    
            if ((fd = open("bigfile.bin", O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) < 0)
            {
                perror("bigfile.bin");
                fprintf(stderr, "Unable to open file to grab all available space\n");
                free(fbuff);
                exit(1);
            }
            else
            {
                int written;
                unsigned long chunks = 0;
                unsigned long long total = 0;
    
                printf("starting to fill up the drive\n");
                while ((written = write(fd, fbuff, BIGBUFSZ)) > 0)
                {
                    ++chunks;
                    total += written;
                    /* you may want to display a "." or some such character to show progress... */
                }
                close(fd);
                printf("Wrote %lu chunks totalling %llu bytes, thus filling up the drive\n",
                          chunks, total);
            }
            free(fbuff);
        }
        return 0;
    }
    I've not tested this, or even run it through the compiler, so there may be some typos that I didn't catch, but the overhead of using write() is much lower than that of fputs().
    Last edited by filker0; 01-18-2006 at 11:45 AM. Reason: Fix indenting and such.
    Insert obnoxious but pithy remark here

  8. #8
    Registered User mlupo's Avatar
    Join Date
    Oct 2001
    Posts
    72
    This is even faster. And clever too!
    It does not actually write to the file. It only creates/opens a file then seeks out to the end of it, then closes. I actually write "end" as the last 3 bytes but it's totally not necessary.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    int main ()
    {
      FILE * pFile;
      long getFreeDiskSpace(void);
      long freeDiskSpace;
    
      freeDiskSpace = getFreeDiskSpace();
      //subtract 2MB from this value. 
      freeDiskSpace = freeDiskSpace -2 * 1024 * 1024;
      printf("fseeking to %l", freeDiskSpace);
    
      pFile = fopen ("myfile.txt","w");
      //fputs ("This is an apple.",pFile);
    
      fseek (pFile,freeDiskSpace,SEEK_SET);
      fputs ("end",pFile);
      fclose (pFile);
      return 0;
    }
    
    long getFreeDiskSpace(void)
    {
    	DWORD dwSectorsPerCluster,
    		dwBytesPerSector,
    		dwNumberOfFreeClusters,
    		dwTotalNumberOfClusters;
    	void loop();
    	double ifdSpace=0; //initial free disk space
    	//get the free disk space available.
    	if (GetDiskFreeSpace (NULL, &dwSectorsPerCluster,
    		&dwBytesPerSector, &dwNumberOfFreeClusters,
    		&dwTotalNumberOfClusters))
    		{
    			ifdSpace = (long)dwBytesPerSector;
    			ifdSpace *= dwSectorsPerCluster;
    			ifdSpace *= dwNumberOfFreeClusters;
    		}
    
    	return (long)ifdSpace;
    }
    NEVER PET YOUR DOG WHILE IT'S ON FIRE!

  9. #9
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Quote Originally Posted by mlupo
    This is even faster. And clever too!
    It does not actually write to the file. It only creates/opens a file then seeks out to the end of it, then closes. I actually write "end" as the last 3 bytes but it's totally not necessary.
    Depending on the environment you're running this program in, you may find that your technique does not work as expected.

    For one, some file systems support sparse files, where empty blocks don't actually take up space. That case is rare, but I've written such file systems, so I know that they exist.

    Another way in which this may not behave as expected is when you're on a multi-user system, or even a multi-programmed system; the amount of free space that you get from your getFreeDiskSpace() function may no longer be valid. Also, simply creating a new file in a directory may extend the size of the directory, and in that case, the amount of free space will change as well.

    [edit]
    Also, on large drives, the maximum file size may be quite a bit smaller than the free space available. On some versions of Windows (maybe all), GetFreeDiskSpace() maxes out at 2GB; you have to use GetFreeDiskSpaceEx() to get the correct free space.
    [/edit]
    Last edited by filker0; 01-18-2006 at 02:50 PM. Reason: Add another point
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linux writing often on main disk
    By mynickmynick in forum Linux Programming
    Replies: 17
    Last Post: 04-07-2009, 02:05 PM
  2. Writing to Disk
    By Jesdisciple in forum C Programming
    Replies: 7
    Last Post: 03-20-2009, 11:59 AM
  3. Replies: 2
    Last Post: 05-09-2008, 07:27 AM
  4. Writing Directly to Hard Disk Sectors
    By ali_aslam in forum Linux Programming
    Replies: 2
    Last Post: 07-27-2007, 02:29 AM
  5. Formatting Output
    By Aakash Datt in forum C++ Programming
    Replies: 2
    Last Post: 05-16-2003, 08:20 PM