Using I/O streams.

Mar 9, 2009 at 5:47pm
Kind of new to programming, and I'm wondering if I can open a file under the name inputed by the user? I was thinking that maybe I should use pointers, but I'm not really sure.My other issue is that I'm trying to open a file, right now called Registration.txt, and under a series of switch cases have the user input data, but ever time i run it, the file just remains blank. Any way I can keep on adding in information every time I run the switch case again without deleting anthing?
This is the area of code that I'm talking about:

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
void freshmenchoice()
{
	ofstream myfile("Registration.txt");
	int class1;
	cout<<"Which class would you like to add to your schedual?";
	cout<<"\n\n1.EML 1001/nIntro to Engineering\n3 credits\nMonday Wednesday Friday\n10:00AM to 10:50 AM.";
	cout<<"\n\n2.EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
	cout<<"\n\n3.EML 1333\nIntro to Programming\n3 credits\nTuesday Thursday\n9:00AM to 10:15AM\n";
	cout<<"\n\n4.EML 1525\nStructural Design for Engineers\n3 credits\nMonday Wednesday Friday\n2:00PM to 2:50PM \n";
	cout<<"\n\n5.EML 1303\nEngineering Theory\n3 credits\nTuesday Thursday \n12:00PM to 1:15PM"<<endl;
	cin>>class1;
	
			switch (class1)
			{
	case 1: {//ofstream myfile;
			myfile.open("Registration.txt");
			myfile <<"EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
			myfile.close();}
			break;
	case 2: {//ofstream myfile;
			myfile.open("Registration.txt");
			myfile <<"EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
			myfile.close();}
			break;
	case 3:{//ofstream myfile;
			myfile.open("Registration.txt");
			myfile <<"EML 1333\nIntro to Programming\n3 credits\nTuesday Thursday\n9:00AM to 10:15AM\n";
			myfile.close();}
			break;
	case 4: {//ofstream myfile;
			myfile.open("Registration.txt");
			myfile <<"EML 1525\nStructural Design for Engineers\n3 credits\nMonday Wednesday Friday\n2:00PM to 2:50PM \n";
			myfile.close();}
			break;
	case 5: {//ofstream myfile;
			myfile.open("Registration.txt");
			myfile <<"EML 1303\nEngineering Theory\n3 credits\nTuesday Thursday \n12:00PM to 1:15PM";
			myfile.close();}
			break;
			}
	int a;
	cout<<"Would yo;u like to register for another class?\a1.Yes\n2.No";
	cin>>a;
	switch (a)
	{
	case 1: freshmenchoice();
		break;
	case 2: 
		break;
	}
}


Thank you very much.
Mar 9, 2009 at 7:25pm
Yes you can. Just have the user enter a string and use that in place of the file name.

Note: It may have to be a c-style string. I forgot if a c++ string will work.

Edit: You can use a C++ string but you have to add the .c_str() to the end.
Last edited on Mar 9, 2009 at 7:32pm
Mar 10, 2009 at 3:07pm
How do I define the file? myfile.open("sname.txt") ?
And how do I keep all my information in the same file?
Mar 10, 2009 at 3:33pm
The File is already opened by the constructor ofstream myfile("Registration.txt");
You don't have to explicitly say open unless the file is closed.

Change the constructor to this to add all new data to the end of the file.
std::ofstream myfile("Registration.txt", std::ios::out | std::ios::app)

You can remove the std:: prefix if you are using the std namespace. I just placed the prefixes there so you would know.

To define the file name with a string just modify the constructor like this:
std::ofstream myfile(file_string.c_str, std::ios::out | std::ios::app

ios::app means append to the end of the file.


Here is a more simplified version of the freshmenchoice:
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
#include <iostream>
#include <fstream>

using namespace std;

void freshmenchoice()
{
	ofstream myfile("Registration.txt", ios::out | ios::app);

	int class1;
	cout<< "Which class would you like to add to your schedual?";
	cout << "\n\n1.EML 1001\nIntro to Engineering\n3 credits\nMonday Wednesday Friday\n10:00AM to 10:50 AM.";
	cout << "\n\n2.EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM";
	cout << "\n\n3.EML 1333\nIntro to Programming\n3 credits\nTuesday Thursday\n9:00AM to 10:15AM\n";
	cout << "\n\n4.EML 1525\nStructural Design for Engineers\n3 credits\nMonday Wednesday Friday\n2:00PM to 2:50PM \n";
	cout << "\n\n5.EML 1303\nEngineering Theory\n3 credits\nTuesday Thursday \n12:00PM to 1:15PM" << endl;
	cin >> class1;
	
	switch (class1)
	{
	case 1: 
		myfile <<"EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM\n";
		break;

	case 2: 
		myfile <<"EML 1234\nBeginning Principles of Engineering \n3 credits\nMonday Wednesday Friday\n12:00PM to 12:50 PM\n";
		break;

	case 3:			
		myfile <<"EML 1333\nIntro to Programming\n3 credits\nTuesday Thursday\n9:00AM to 10:15AM\n";
		break;

	case 4: 
		myfile <<"EML 1525\nStructural Design for Engineers\n3 credits\nMonday Wednesday Friday\n2:00PM to 2:50PM \n";
		break;

	case 5: 			
		myfile <<"EML 1303\nEngineering Theory\n3 credits\nTuesday Thursday \n12:00PM to 1:15PM\n";
		break;
	}

	myfile << endl << endl;

	myfile.close();

}


Two lines are placed after every entry in the Registration file.
Last edited on Mar 10, 2009 at 3:50pm
Mar 10, 2009 at 4:56pm
Thank you!
And can I globably define and open the file before comin to this function with the string name, and make it work like that? I'm just wondering how far i can push it.

Thank you for the help:)

While renaming the file, i wrote it as such:

ofstream myfile(sname.c_str, std::ios::out | std::ios::app)

as sname is my string that takes user inputs from their First and Last name to a complete name, but this is done in the main() function. When trying to call sname in this side function, it gives me an error

error C3867: 'std::basic_string<_Elem,_Traits,_Ax>::c_str': function call missing argument list; use '&std::basic_string<_Elem,_Traits,_Ax>::c_str' to create a pointer to member
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]

I'm guessing i should create a pointer in this function that calls the value of the actual variable in the main function before using it?
Last edited on Mar 10, 2009 at 5:39pm
Mar 10, 2009 at 7:26pm
Oops sorry. You have to have sname.c_str().

I forgot the parentheses.

Whenever you see function call missing arugment list that means that you didn't put the parentheses and/or the arguments.
In this case c_str doesn't need arguments but it needs parentheses to call the function.
Last edited on Mar 10, 2009 at 7:29pm
Topic archived. No new replies allowed.