any idea how to Write the encoded string to a second file, such as coded.txt

closed account (9G8MoG1T)
Not sure if I'm right


#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
char ch;
string input;
fstream inputFile;
fstream outputFile("Encrypted.txt");

//Open the file in input mode.
inputFile.open("plain.txt", ios::in);

//If the file was successfully opened, continue.
if (inputFile)
{
while (inputFile) //While last read operation successful, continue
{
cout << input << endl; //Display last read line
getline(inputFile, input);//Read next line
for (size_t i = 0; i < input.size(); ++i)
{
input[i] += 4;//Adding 4 to the ASII value each character
int input;
ch = input;
outputFile.put(ch);
}
}
inputFile.close();//Closes the file
}
else cout << "ERROR: File cannot be opened.";
}
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    char ch;
    string input;
    ofstream outputFile("Encrypted.txt");

    //Open the file in input mode.
    ifstream inputFile("plain.txt");

    //If the file was successfully opened, continue.
    if(inputFile.is_open())
    {
        while(getline(inputFile, input)) //While last read operation successful, continue
        {
            cout << input << endl; //Display last read line
            for (size_t i = 0; i < input.size(); ++i)
                input[i] += 4;//Adding 4 to the ASII value each character
            outputFile << input;
        }
        inputFile.close();//Closes the file
    }
    else
        cout << "ERROR: File cannot be opened.";

    return 0;
}


I changed your for loop part.
1
2
3
4
5
6
7
for (size_t i = 0; i < input.size(); ++i)
{
input[i] += 4;//Adding 4 to the ASII value each character
int input;
ch = input;
outputFile.put(ch);
}


Finalize your code. Make sure that all files open/used were closed properly.
Last edited on
Please use these tags to separate your code:
http://www.cplusplus.com/articles/z13hAqkS/

-It makes it easier to read and I can play with the code to adjust things to better explain.

Are you getting an error? If you are those will help us help you. Another reason I ask is because your syntax looks wrong in a few places.

1
2
fstream inputFile;                             //  ifstream is for files you will read in.
fstream outputFile;         // ofstream is for files that will store the out going data. 

ifstream and ofstream are file stream variables
1
2
3
//Explained above
ifstream variable;
ofstream variable;


1
2
//Open the file in input mode.
inputFile.open("plain.txt", ios::in);

Not sure if the( ios::in ) is necessary; but the following should work :
inputFile.open("fileName.txt");

Another option:
 
inputFile.open(file_Name.c_str());

//Note that the file_Name can be a string variable that the user defines.

Also it might be easier to control the loop with a do while rather than an if this then while this..
For Example:
1
2
3
4
5
6
do{
    inputFile.open("fileName.txt");
    
}while(!inputFile.is_open());//notice the explanation point in the condition...

getline(inputFile, input);


You kinda lost me here, buddy. I would think that an int would suffice. i < input.length() "might" work better.
If someone else see's this and agrees or disagrees I would like to hear which would be better.

- Please read comments next to variables and please explain.
1
2
3
4
5
6
7
for (size_t i = 0; i < input.size(); ++i)
{
input[i] += 4;//Adding 4 to the ASII value each character
int input;   //This does not make since to me. Is this suppose to be a static cast?
ch = input;        /* Im confused here but maybe its just me... */
outputFile.put(ch);
}



Hope this helps, let me know if this does not work or does not help and Ill take another look.

Last edited on
Topic archived. No new replies allowed.