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
Printable View
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
I dunno if that'll work, but I don't really wanna try it :pCode:#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;
}
if you try it, please lemme know if it works, thanks! :)
Why did you comment "// filepointer"? I think the "FILE *" kind of gives it away.
</rant>
Quote:
Originally Posted by ahluka
to justify the name loll but your right, it was lame ,)
:) Thanks. This gets me started in the right direction. Add some conditionals and off I go.
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;
}
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: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().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;
}
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;
}
Depending on the environment you're running this program in, you may find that your technique does not work as expected.Quote:
Originally Posted by mlupo
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]