Writing an encrypted file to another file?

Write a C++ program that reads text from a file and encrypts the file by adding
4 to the ASCII value of each character.
Your program should:
1. Read the provided plain.txt file one line at a time. Because this file has
spaces, use getline (see section 3.8).
2. Change each character of the string by adding 4 to it.
3. Write the encoded string to a second file, such as coded.txt
4. The encoded file should have the same structure as the original file, if the
second line of the original file has 24 characters (including spaces) then
there should be 24 characters on the second line of the encoded file
(spaces will now look like '$').
Hints:
1. In Visual Studio Express, put your plain.txt file in the project directory. The
same folder that your C++ source (.cpp) file is in.
2. One of the challenges is to keep reading lines from the plain.txt file until
the end. Sections 12.4 and 12.5 describe how you can do that

Okay so the only problem that I have is trying to figure out how to write the encrypted code to the new file...please help with an explanation would be extremely helpful!!

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 #include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()

{

	string input;
	fstream inputFile;
	fstream outFile;

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

	// If the file opened successfully

	if (inputFile)
	{

		while (inputFile)

		{

			cout << input << endl;
			getline(inputFile, input);

			for (int i = 0; i < input.size(); i++)
			{

				 input[i] += 4;

			}

		}

		inputFile.close();

	}
	else
	{

		cout << "Error: Unable to open file." << endl;

	}



	system("pause");

	return 0;

}
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
#include <iostream>
#include <fstream>
#include <string>
#include <limits>

int main()
{
    // the largest value that an object of type char can hold
    const int max_val = std::numeric_limits<char>::max() ;
    const int new_line = '\n' ; // value of the new-line character
    const int delta = 4 ; // value to be added to each char

    const std::string input_file_name = "plain.txt" ;
    const std::string output_file_name = "cipher.txt" ;

    std::ifstream in_file(input_file_name) ; // open in_file for reading
    std::ofstream out_file(output_file_name) ; // and out_file for writing

    std::string line ;
    while( std::getline( in_file, line ) ) // for each line in in_file
    {
        for( char& c : line ) // for each char in line
        {
            int new_value = c + delta ; // candidate new value for the character

            if( new_value < max_val && // new value is within range
                new_value != new_line ) // and won't result in a spurious new line
            { c = new_value ; } // update the character in the line with the new value

            else { /* can't change this character; report error */ }
        }

        out_file << line << '\n' ; // write the modified line to the output file
    }
}
For some reason my code is not working. My file coded.txt reads like it should, but when I run it in Visual, it doesn't have the last line on it. My professor says I need a getline after the IF statement, but it doesn't work when I try it. HELP!!

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

using namespace std;

int main()

{

string input;
ifstream inputFile;
ofstream outputFile;

//Open the file for input mode
inputFile.open("plain.txt");
outputFile.open("coded.txt");

// If the file opened successfully

if (inputFile)
{


while (!inputFile.eof())

{
cout << input << endl;
getline(inputFile, input);

for (int i = 0; i < input.size(); i++)
{

input[i] += 4;

}

outputFile << input << endl; //Write to coded.txt file

}

}
//If file doesn't open successfully
else
{
cout << "Error: Unable to open file." << endl;
}
inputFile.close(); //Close plain.txt file
outputFile.close(); //Close coded.txt file
system("pause");

return 0;

}
Last edited on
> My professor says I need a getline after the IF statement,

You need getline to be the condition for the while statement.
1
2
3
4
while( getline(inputFile, input) )
{
    // ...
}


This operator makes it possible to use streams and functions that return references to streams as loop conditions, resulting in the idiomatic C++ input loops such as
while(stream >> value) {...} or while(getline(stream, string)){...}.
Such loops execute the loop's body only if the input operation succeeded.
http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
If I do that it still doesn't work. It doesn't encrypt it like it's suppose to and it doesn't keep going til you get to the end of the plain.txt file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::ifstream file( __FILE__ ) ; // open this file for input

    std::string line ;
    while( std::getline( file, line ) ) // for each line read from the file
    {
        // print tit out so that we can verify that all lines were read
        std::cout << line << '\n' ;

        // do what ever with the line
    }

    std::cout << "----------- done reading all lines from file --------------\n" ;
}
// *** this is the last line *** 

Windows (Microsoft compiler/library): http://rextester.com/TQIXL75173
Unix-like (LLVM compiler/library): http://coliru.stacked-crooked.com/a/19c405238a60faac
Thanks for your help, I think I finally figured it out.
Topic archived. No new replies allowed.