Yes it should. Your code (unchanged) results in a file name test.txt being created in my main project folder containing the string entered via the console on my computer.
I don't know why it isn't working for you. Are you looking for the file in the right place? Where it is created may depend on what IDE you're using, so check various folders in your project folder.
I can't tell from just that. That code works fine for me.
Can you post the actual error message? The details can be important.
A bit more of your code may help too, for judging context.
in the file.
Later, this would appear as a single string. You will want to insert newlines '\n' between elements.
eg: DataSave << member.Age << '\n';
Inserting a blank space instead would also separate the values enough so they could be read later.
Also should I be outputting the data type to the file too eg if its the postcode should I put post code before it so when it gets red it saves to the appropriate place? I don't know.
Yes, the file will be overwritten each time the program is run. If you rename the file between runs then a new "test.txt" will be created.
Also, you could easily modify that code to prompt the user for a filename to create.
1 2 3
string filename;
cout << "Enter a name for the output file to create: "; cin >> filename;
ofstream DataSave( filename.c_str() );
If I prompted the user for a filename would I be able to prompt user to later load chosen file?
I suppose you could keep a file with the names of all files previously created by this program and then present this list (of filenames) to the user, but this complication should come later.
should I somehow be making it so the file stores it with
Name:John etc
or would I be able read this information fine later without having to do that.
No. You should place only the data to be read in the file (otherwise you get into complicated data parsing).
The data format you showed first should work fine.
Great.
What it seems you are trying to do is to read data from a file if it already exists, or get the data from the user if no file exists yet, and create a file from that.
Something like this should work:
1 2 3 4 5 6 7 8 9 10 11 12 13
ifstream File_in("test.txt");// attempt to open file for reading
if( File_in.is_open() )
{
// the file exists and you can read data from it
}
else// opening "test.txt" for reading failed because "test.txt" does not exist
{
ofstream File_out("test.txt");// create the file and open it for writing
if( File_out.is_open() )
{
// prompt user for data and write it to File_out.
}
}