Check If File Exists, If Not Create Blank Doc - Windows

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.

Any help?
This program allows you to Create a new file and open an existing file to read contents.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// checkForFile.cpp bitzoiD -- Create and Open Files

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <windows.h>
using namespace 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.
Last edited on
Topic archived. No new replies allowed.