Crash/fill harddrive with simple program?

I am currently learning C++. Today I took a break and thought of what I had learnt so far. The last days I have learnt to create files and to send arguments to the main() function through the CMD.

I just wonder since I do not want to try this:

If you make a program that first creates a .txt file and then fill it with tons of data. Then you make an infinite while loop that copies the file continiously. Will this finally result that the harddrive in a short time will become full and crash if your PC is fast enough? I wonder this, since I became curious about it and can not stop thinking of it, and I do not want to try it.
The hard drive will fill up, but it's not likely to lead to a crash (most modern OS wouldn't). And it may take quite a while to fill the hard-drive, especially if the file you are copying is quite small.
All ofstream operations fail in the case that the disk is full, so you wont be able to crash the computer.

Go ahead and try it, this program uses up a teraByte. It will take a while. Also note that the file opened are all opened to a sub-folder named "test". You need to make this folder yourself before running the program. The purpose of the dub-folder is that it is much faster to delete a folder than have the computer try to select 10000 files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

int main(void)
{
	ofstream output;
	int i = 0;
	int j = 0;
	char name[256];
	char number[256];
	
	const unsigned int size = 999999;
	char filler[size];
	for (int r = 0; r < size; r++)
		filler[r] = 'a';
	
	
	while (j < 100)
	{
		strcpy(name, "test/");
		strcat(name, itoa(j, number, 16));
		strcat(name, "_");
		strcat(name, itoa(i, number,16));
		strcat(name, ".txt");
	
		output.open(name);
		if (!output.good())
		{
			cout << "Disk Full When Opened\n";
			return 0;
		}
		for (int r = 0; r < 100; r++)
		{	
			output.write(filler, size);	
			if (!output.good())
			{
				cout << "Disk Full While Writing\n";
				output.close();
				return 0;
			}	
		}
		output.close();
		
		i++;
		if (i > 100)
		{
			j++;
			i = 0;		
		}	
	}
	
	return 0;
}
Last edited on
Ok thanks, I will probably try it out :).
Topic archived. No new replies allowed.