I've been reading quite a few threads about creating new files if one doesn't exist...but I need it specifically for windows, in the current directory we are in.
I've seen
1 2 3 4 5 6
ifstream file;
file.open("TehLeetFile");
if(!file.is_open())
cout<<"Tehleetness is busted!";
else
cout<<"it work!!!!11!!!11!one!";
That's just checking to see if the file is there.
Later on I will need to read the file...but not yet.
// checkForFile.cpp bitzoiD -- Create and Open Files
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
usingnamespace std;
void getFile()
{
vector<string> v;
string userFile;
cout << "Enter filename to access (i.e.. file.txt): ";
cin >> userFile;
ifstream File;
string line;
File.open(userFile);
if(!File.is_open()) {
cout << "There is no file by the name of: " << userFile << endl;
cout << endl;
} else {
cout << "Displaying file: "+ userFile +"." << endl;
cout << endl;
while(getline(File, line)) {
v.push_back(line);
}
for(int i = 0; i < v.size(); i++)
cout << v[i] << endl;
}
}
void makeFile()
{
string userFile;
cout << "To create a file, enter a filename (i.e.. file.txt): ";
cin >> userFile;
cout << endl;
cout << endl;
cout << endl;
ofstream File;
File.open(userFile);
string input;
cout << "Please enter the content to be saved into file." << endl;
cout << "This program can accept multiple lines." << endl;
cout << endl;
cout << "To save & close file, enter the text 'quit++' on a new line." << endl;
cout << endl;
cout << endl;
cout << "Enter Contents below:" << endl;
cout << endl;
while (true) {
getline (cin, input);
if (input == "quit++")
break;
File << input << "\n";
}
File.close();
cout << " " << endl;
cout << "Your file has been saved." << endl;
}
void menu() {
bool quit = false; // Flag for quitting
cout << "Select an option below:" << endl;
cout << endl;
while(quit == false) {
cout << endl;
cout << "a.) Create new document" << endl;
cout << "b.) View an existing document" << endl;
cout << "c.) Exit" << endl;
cout << endl;
cout << endl;
char option;
cin >> option;
cout << endl;
switch(option) {
case'a' : makeFile();
break;
case'b' : getFile();
break;
case'c' : cout << "Exiting" << endl;
quit = true;
break;
default : cout << "Please use a,b,or c!"
<< endl;
}
}
}
int main()
{
menu();
}
As for the specifically for windows aspect of your question, I'm not sure I understand and I'm not sure if I did understand, I would be able to help you on that.