fout multiple lines from a file to a new one

first things first, i searched this site extensively -- i may have missed the answer, so maybe someone could point be in the right direction.

i am trying to make a program that takes input from a user specified file, and "encrypts" it -- then fout'ing the encrypted version to the console, and to a pre-designated file. so far i have everything working, except for some reason -- i'm only able to get the first line of the encrypted text saved to the output file.

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
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
  ifstream fin;
  ofstream fout;
  string fileName;
  int i;
  
  cout << "This program was made to encode secret NSA messages to keep our country's \nsecrets safe!\n\n";
  cout << "File to encode: ";
  getline(cin, fileName);
  fin.open(fileName.c_str());
  if (!fin.good()) throw "I/O error";
  
  while (true)
  {
    if (!fin.good()) break;
	
	string lineFromFile;
	getline(fin, lineFromFile);

	for (i = 0; i < lineFromFile.length(); i++)
	  lineFromFile[i]++; // bump ascii code by 1 (pseudo encryption)
	  
	cout << lineFromFile << endl;	  
	fout.open("secret.txt");
	fout << lineFromFile << endl;

  } // while
  fin.close();
  fout.close();
  
  return 0;
} // main 


any ideas how i could get the entirety of the message outputted to the file? thanks in advance!
Try moving the open()ing of fout outside of the while loop so it is only opened once. You could also just make the loop say while(!fin) instead of checking it inside an if statement and breaking.
Topic archived. No new replies allowed.