[Source Code] File/Directory Generator

Charles

Member
Reputation
0
I had some spare time so I decided to mess around with the generation of files and directories in C++. Here's what I made:

http://pastebin.com/5HdtJBMy

[C]#include <Windows.h>
#include <iostream>
#include <fstream>
#include <ostream>

void main(void)
{
char test_1;
char test_2;
std::eek:fstream created_file;

while(true)
{
std::cout << "Hello! This program will create a directory in the Program Files (x86) folder\non your computer." << std::endl;
std::cout << "Do you wish to continue? Y for yes, N for no." << std::endl;
std::cin >> test_1;

switch(test_1)
{
case 'Y':
case 'y':
break;

case 'N':
case 'n':
std::cout << "The program will now exit then." << std::endl;
system("pause");
exit(0);

default:
std::cerr << "Invalid Input" << std::endl;
exit(0);
}

CreateDirectory(L"C:\\Program Files (x86)\\Created_Directory", NULL);
std::cout << "The directory has been created. This program will now create a file in said\ndirectory. Do you wish to continue?\n Y for yes, N for no." << std::endl;
std::cin >> test_2;

switch(test_2)
{
case 'Y':
case 'y':
break;

case 'N':
case 'n':
std::cout << "The program will now exit then." << std::endl;
system("pause");
exit(0);

default:
std::cerr << "Invalid Input" << std::endl;
exit(0);
}

created_file.open("C:\\Program Files (x86)\\Created_Directory\\Created_file.txt");
created_file << "Automatically generated text. Thank you for using this program. :3";
created_file.close();

std::cout << "The file has been generated." << std::endl;
break;
}

std::cout << "The program will now shut down." << std::endl;
system("pause");
}[/c]

The program informs the user that there will be a directory generated and then asks the user if they wish to continue. They input either Yes or No and the program does a specified action depending on which the user chooses. If the user chooses yes, the program will create a directory in C:\Program Files (x86). If the user chooses no the program exits. Then the program informs the user a file will be generated. It has the same process as above. If the user chooses yes, a text file will be generated in the created directory. After the text file is generated, the program closes.

:dirol:
 
Good job! If you make so the user specifies the directory, that would be awesome.
 
I don't program C++ but this would be very useful for anyone.
 
It isn't the most advanced, it only generates a directory and a text file, but it is the basis of file generation. You could potentially make an installer by expanding on this base concept.
 
It doesnt have to be advanced to be popular ;)
No doubt it would be a required snippet of code for 90% of any program, good job :)
 
Yes! Great work caro. I'm working hard to learn c++. Maybe I can be on your level some day.
 
Never use system("pause")!!!! Use cin.get()!
 
Back
Top