What i think is you are opening an uncreated file for reading. Suppose "data.txt" exists
fin.open("data.txt");
this will open file.
In the next code segment:
string fileName;
cout << "What file do you want to use for input? ";
getline(cin, fileName);
fin.open(fileName.c_str());
if fileName does not exists then how can you open a file which does not exist.
In case of
fout.open("secret.txt", ios::app);
if file doesn't exist a new one will be created.
Based on my understanding of what you want here is the sample code.
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream fin;
string fileName;
ofstream fout;
fout.open("secret.txt",ios::out);
// encode string s by adding 1 to the ASCII code of each character
string s = "Hello, World"; // an example
for (int i = 0; i < s.length(); i++) // for each char in the string...
s[i]++; // bump the ASCII code by 1
fout.write(s.c_str(),sizeof(s));
fout.close();
fin.open("secret.txt",ios::in);
string line;
while (getline(fin, line))
{
string lineFromFile;
cout << line << endl;
if (!fin.good()) break;
} // while
fin.close();
return 0;
}
So you better read
http://cplusplus.com/doc/tutorial/files/