-
Writing to a file
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main()
{
string playername;
cout << "What is the players name";
cin >> playername;
ofstream fin(playername + ".txt");
fin << "Hello World";
}
can anyone tell me what is wrong with the above code? When I try to compile it my compiler(I tried MSVC++ 6.0, Borland c+ 5.02, MinW, and Cygwin) always complaines about ofstream fin(playername + ".txt"); is this not the correct way to do this?
-
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string playername;
cout << "What is the players name";
cin >> playername;
playername += ".txt";
ofstream fin(playername.c_str());
fin << "Hello World";
fin.close();
return 0;
}
I see what you mean....but if you turned it to a C-Style string as above it will work
-
.