Opening/creating a file help

One of the things i need to do in my switch statement is open/create a file if it doesn't exist, I have it all coded but I'm just not sure if it does what it is supposed to do.

Also if i have any other minor/major errors in any other cases if you pointed them out if would help a lot, in most of the case functions I've just wrote cout's of what I'm going to do in them but I'm mostly worried about case1 for now, also in case 5 i found some code to do what is needed for that case which is displaying the largest number, but i just have to re-write it for my program.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <fstream>
#include <string>

using namespace std;	

string case1();
int case2();
int bubbleSort();
int case4();
int case5();
int case6();
void exit();

int main()
{
	char repeatMenu = 0;
	string fileName = "";

	do {

		system("cls");

		cout << "** MENU **" << endl << endl;

		cout << "Current Data File: " << fileName;

		cout << endl << endl;

		int menuChoice;

		cout << "(1) Select / Create data file (.txt file extension will be added automatically)\n";
		cout << "(2) Display all numbers, total and average\n";
		cout << "(3) Display all numbers sorted\n";
		cout << "(4) Search for a number and display how many times it occurs\n";
		cout << "(5) Display the largest number\n";
		cout << "(6) Append a random number(s)\n";
		cout << "(7) Exit the program\n";

		cout << endl;

		cout << "Menu choice: ";
		cin >> menuChoice;

		if (menuChoice < 1 || menuChoice > 7)
		{
			do
			{
				cout << "Menu choice: ";
				cin >> menuChoice;
			} while (menuChoice < 1 || menuChoice > 7);
		}

		cout << endl << endl;

		switch (menuChoice)

		{
		
		case 1:		fileName = case1();
					break;
		case 2:		fileName = case2();
					break;
		case 3:		fileName = bubbleSort();
					break;
		case 4:		fileName = case4();
					break;
		case 5:		fileName = case5();
					break;
		case 6:		fileName = case6();
					break;
		case 7:		exit();
					break;

		}

		cout << endl;
		cout << "Would you like to return to the main menu ? [Y/N]: ";
		cin >> repeatMenu;

	} while (repeatMenu == 'Y' || repeatMenu == 'y');

	("pause");
	return 0;
}

string case1()
{
	string fileName;
	fstream fileName1;

	cout << "Please select the name of the file you would like to open: ";
	cin >> fileName;

	fileName = fileName + ".txt";

	fileName1.open(fileName, ios::in);

	if (fileName1.fail())
	{
		cout << "The file you selected does not exist.\n";
		cout << "Creating file named the filename you selected.\n\n";

		fileName1.open(fileName);
	}

	else
	{
		cout << "The filename you selected exists and is opened.\n";
	}
	return fileName;
}

int case2()
{
	cout << "Display all the numbers, total and average";
	cout << " in the selected filename";
	cout << endl << endl;

	return 0;
}

int bubbleSort()
{
	cout << "Create a bubble sort and display ";
        cout << "all the numbers sorted.\n\n";

	return 0;
}

int case4()
{
	cout << "Sort array and use binary search";

	return 0;
}

int case5()
{
	//int numbers[];
	//int count;
	//int highest;
	//highest = numbers[0];

	//for (count = 1; count < SIZE; count++)
	{
	//	if (numbers[count] > highest)
	//		highest = numbers[count];
	}

	system("pause");
	return 0;
}

int case6()
{
	cout << "Append a random number and open the file using ios::app";
	cout << endl << endl;

	return 0;
}

void exit()
{
	cout << "Bye, have a great day!";
	cout << endl << endl;

	system("pause");

	exit(0);

	return;
}
You can use std::ofstream to create the file for you if it doesn't already exist. std::ifstream doesn't create files if they don't exist.

http://www.cplusplus.com/reference/fstream/ofstream/

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
#include <fstream>
#include <iostream>
#include <string>

int main()
{
	std::string filename = "test.txt";
	std::string extracted = "";
	std::ifstream infile;
	std::ofstream outfile;

	std::cout << "Enter file name: ";
	std::cin >> filename;

	infile.open(filename);

	if (infile.fail())
	{
		std::cerr << "No file found matching name: " << filename << std::endl;
		//Create file
		infile.close();
		outfile.open(filename);
		outfile << "New test file content\n";
		outfile.close();
	}

	infile.open(filename);

	while (infile)
	{
		std::getline(infile, extracted);
		std::cout << extracted << std::endl;
	}
	infile.close();
}
Last edited on
When i put that in case 1 and compile the program and call the case1 function by selecting 1 after input a file name it gives me a invalid null pointer error, even if the file name exists and makes me abort.
Don't just take my code and smack it in. Use it as an example so that you can do the same for your project. Adapt my code to fit your needs.
Topic archived. No new replies allowed.