In: Computer Science
As a challenge question i was asked to make a program that can make a newly made Directory that has the name input from the user. I am to create a program that can make a txt file in the newly created directory.
I am doing this in windows and this is what i got.
#include <windows.h>
#include <string>
#include <iostream>
#include <string.h>
#include <fstream>
#include <TlHelp32.h>
using namespace std;
int main()
{
string Text = "Test string";
ofstream file;
cout << " Type Text here: ";
getline(cin, Text);
cout << "\n\n";
CreateDirectory (("C:\\Users\\Filepath" + Text).c_str(),
NULL);
cout << Text << endl;
return 0;
}
Tried many methods, but was not able to. I am suppose to determined if it can be done or not.
Thank you for your time!
Check with the below code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <windows.h>
#include <string>
#include <TlHelp32.h>
using namespace std;
int main() {
string Text = "Test string";
ofstream file;
cout << " Type Text here: ";
getline(cin, Text);
cout << "\n\n";
file.open("RandimName.txt");
file << Text;
file.close();
return 0;
}
Result:
Please rate it if the above solution helps you in any way or if you have any concerns comment it, I will help you through again