okay, after a lot of here and there. i finally got my previous program to work.
i copy and pasted my current program below. if someone can run and it see what my issue is, itd be truly helpful.
user input: data.txt
my problem now: how to encode the output on console and text file "Secret"
the ASCII coding below, i feel, is in the wrong spot. i've tried every way i could think of to put it elsewhere, but the problem is still the same. any suggestions??
#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
ifstream fin;
string fileName;
cout << "What file do you want to use for input? ";
getline(cin, fileName);
fin.open(fileName.c_str());
ofstream fout;
fout.open("secret.txt", ios::app);
if( !fin.is_open() )
{
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 << "Can't open the file\n";
fout.close();
return 1;
}
string line;
while (getline(fin, line))
{
cout << line << endl;
fout << line << endl;
fout.close();
} // while
The code to modify each line you read from the file should be between lines 30 and 33. The stuff on lines 19 through 21 only happen if the file was not opened.
#include <fstream>
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
ifstream fin;
string fileName;
cout << "What file do you want to use for input? ";
getline(cin, fileName);
fin.open(fileName.c_str());
ofstream fout;
fout.open("secret.txt", ios::app);
if( !fin.is_open() )
{
fout << "Can't open the file\n";
fout.close();
return 1;
}
string s;
while (getline(fin, s))
{
string s = "(data.txt)"; // an example
for (unsignedint i = 0; i < s.length(); i++) // for each char in the string...
s[i]++; // bump the ASCII code by 1
cout << s << endl;
fout << s << endl;
} // while
fout.close();
fin.close();
cin.ignore();
cin.get();
return 0;
}
This program will always append "Can't open the file" to "secret.txt". As u are returning form the program on a condition that will be always true if user enters a file name that does not exist in the directory.
string fileName;
cout << "What file do you want to use for input? ";
getline(cin, fileName);
fin.open(fileName.c_str());
ofstream fout;
fout.open("secret.txt", ios::app);
if( !fin.is_open() )
{
fout << "Can't open the file\n";
fout.close();
return 1;
}
Use already created file for opening a file to read for ifstream object.
and also use system("pause"); instead of cin.ignore();
cin.get();
if you want to give a pause in the program to see the output on screen.