file program

1
2
3
4
5
6
7
8
9
int main ()
{

	string filename;
	cout << "Enter the file name: ";
	cin >> filename;
	ofstream myfile( filename.c_str(), ios::app);

}


Here is the code and my original question was how do I make it so that the user can read or write into a file that is not hard coded.

hard code example:

string filename;
	cout << "Enter the file name: ";
	cin >> filename;
	ofstream myfile( "file.txt", ios::app);
Last edited on
Move lines 15 and 16 to above line 11? I'm not sure what you're confused about here.

As for your loop, you seem to be very very confused about the difference between a while loop and a for loop, as well as the syntax for a such loops.

I didn't say I was confused. I was asking how I can ask for the user to input the file he wants to append to instead of just hardcoding it to work for 1 file.

I also wanted to know why my loop wasn't working and you answered that I think.

Also about making an option for the user to tell me whether they wanted to read or write to their file and how I go about them going into one of those modes.
Last edited on
cppnoob25 wrote:
I didn't say I was confused. I was asking how I can ask for the user to input the file he wants to append to instead of just hardcoding it to work for 1 file.
Well, then I am confused why you are still asking.
cppnoob25 wrote:
I also wanted to know why my loop wasn't working and you answered that I think.
Review the looping structures and choose either the while-loop or the for-loop - you can't mix them ;)
cppnoob25 wrote:
Also about making an option for the user to tell me whether they wanted to read or write to their file and how I go about them going into one of those modes.
If you use std::fstream, then you can read or write depending on whether you specify std::ios::out or std::ios::in as one of the flags. You can specify both, but it's not recommended to do so.
Ok I am confused then. I know I ask the user in my code but it doesn't matter what the user inputs because the file that my code reads and writes to is just "file.txt". I don't know how to put the users input into ofstream.

Something obvious to you isn't obvious to me ha.


Thanks for the other info I will review it and come back to that.
Last edited on
like everyone's saying, move lines 13, 15 and 16 above line 11 and then line 11 should be:
 
ofstream myfile( filename, ios::app);


That kind of thing.
removed code thanks for the help.

dude put your code back, it might help someone with a similar problem.
dude put your code back, it might help someone with a similar problem.

I'm wondering whether it's legitimate to report people who do this? It's utterly selfish - the OP has gotten what they want, but they're not prepared to leave their code as a lasting contribution to this community in return.
Last edited on
I would say do report it, as it actually subtracts from the forum because now someone has to see this and find out that they wasted their time and then continue searching.
Fair point. Done!
I will return the code and I didn't think it was a big deal. For some reason I don't get email updates or my code would have been up a lot earlier.

I didn't remove it with the intention of being selfish but merely because I think the people who take time to actually register to this website to ask about their code are here to learn. As opposed to someone just looking for answers without putting much thought into it.
That is why we post help and we don't post solutions. Glad to see we can make this topic helpful again - thanks :)
No problem L B. You do a great service here.

Any idea why I'm not receiving the emails? I am subscribed to this thread.

Also this programs unfinished since I can't recall how I was going to let the user select either read or write mode.
Maybe the emails are going to your spam folder? I don't know, I've never used the subscribe option, only the bookmark option. (I'm anti-email, haha.)
Topic archived. No new replies allowed.