Reading and writing different files

So the goal is to use ROT13 and read from a file with the words "JULIUS CAESAR" or "Now is the time for all good men
and women to come
to the aid of their party."

and write it to another file but encrypted, and it needs to keep the formatting. I did read that if I use " << " or " >> " operators it will just ignore the spaces and newline "\n". So how should I write my code to make sure it keeps spaces in tact and other stuff?

Line 57 "while(getline (plainTextFile, ch)" doesn't work because I changed it from "while(plainTextFile >> ch)" and that obviously skips spaces.
output: (ignore the 0 and the test word)
input2.txt data read:
-------------------------------------
JULIUS CAESAR
-------------------------------------
0
input2.txt data converted and stored:
-------------------------------------
WHYVHFPNRFNE testWHYVHFPNRFNE
-------------------------------------
Great!

*It's probably possible to not have my code go from "char ch" to "encrypt" then to only add it to my "outputString" in my switch statement but I was using some old code, so I'm gonna leave it for now.
I just need a space such as: "WHYVHF PNRFNE". Then I just need to take that encryption and write it to another 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    // Defines the input as a character, and an encrypted character
    char ch, encrypt;

    // Makes a flag for any input errors
    bool errorFlag;
    string inFileName, outFileName;



    cout << "*****************************************\n";
    cout << "ROT13 - A Caesar cipher program\n";
    cout << "*****************************************\n";

    cout << "Enter the name of a text file with alpha characters to convert: ";
    cin >> inFileName;
    cout << "Enter the data output file to store Caesar cipher translated text: ";
    cin >> outFileName;

    ifstream plainTextFile;
    string inputString, outputString;

    plainTextFile.open(inFileName); //(put input of cin filename here)
    while(!plainTextFile.is_open())
    {
        cout << "Unable to open file. Enter correct filename: ";
        cin >> inFileName;
        plainTextFile.open(inFileName);
    }
    if(plainTextFile.is_open())
    {
        cout << "file open \n";
        while(getline (plainTextFile, inputString))
        {
            cout << "\n";
            cout << inFileName << " data read:\n";
            cout << "-------------------------------------\n";
            cout << inputString << "\n";
            cout << "-------------------------------------\n";
        }
        cout << inputString.length() << "\n";
        plainTextFile.close();
    }

    plainTextFile.open(inFileName);

    cout << inFileName << " data converted and stored:\n";
    cout << "-------------------------------------\n";

    while(getline (plainTextFile, ch))
    {
        // Converts
        ch = toupper(ch);
        switch (ch)
        {
            case 'A' :
                encrypt = 'N';
                    break;
            case 'B' :
                encrypt = 'O';
                    break;
            case 'C' :
                encrypt = 'P';
                    break;
            case 'D' :
                encrypt = 'Q';
                    break;
            case 'E' :
                encrypt = 'R';
                    break;
            case 'F' :
                encrypt = 'S';
                    break;
            case 'G' :
                encrypt = 'T';
                    break;
            case 'H' :
                encrypt = 'U';
                    break;
            case 'I' :
                encrypt = 'V';
                    break;
            case 'J' :
                encrypt = 'W';
                    break;
            case 'K' :
                encrypt = 'X';
                    break;
            case 'L' :
                encrypt = 'Y';
                    break;
            case 'M' :
                encrypt = 'Z';
                    break;
            case 'N' :
                encrypt = 'A';
                    break;
            case 'O' :
                encrypt = 'B';
                    break;
            case 'P' :
                encrypt = 'C';
                    break;
            case 'Q' :
                encrypt = 'D';
                    break;
            case 'R' :
                encrypt = 'E';
                    break;
            case 'S' :
                encrypt = 'F';
                    break;
            case 'T' :
                encrypt = 'G';
                    break;
            case 'U' :
                encrypt = 'H';
                    break;
            case 'V' :
                encrypt = 'I';
                    break;
            case 'W' :
                encrypt = 'J';
                    break;
            case 'X' :
                encrypt = 'K';
                    break;
            case 'Y' :
                encrypt = 'L';
                    break;
            case 'Z' :
                encrypt = 'M';
                    break;
            case ' ' :
                encrypt = ch;
                    break;
            case '.' :
                encrypt = '.';
                    break;
            case '\n' :
                encrypt = ch;
                    break;
            default :
                errorFlag = true;
                cout << ch << " not found in the ROT13 coding scheme." << endl;     // Default catch all other characters
        }
        if (!errorFlag)
        {

            cout << encrypt;
            outputString += encrypt;
            ch++;
        }
    }
    cout << " test" << outputString << "\n";
    cout << "-------------------------------------\n";
    cout << "Great! ";


    //cout << str.length() << "\n";
    // make sure to close file plainTextFile.close();


    //ofstream translatedText;
    // translatedText.open (put name of output file here)
    // validation if(translatedText.is_open())
    // close file translatedText.close();


    return 0;
}
Last edited on
you can read with getline and put the new lines back when you print to screen or back into a new file or whatever.
so it can read a line, spaces and all, and only the newline is removed, but you know that each line needs a new line, so its easy to just put one back.
Ya I'm trying to use getline but I get an error "no matching function call to 'getline...'" I think it's because I'm using plainTextFile to ch?

So I can just add a cout << "\n"; after line 160?
getline is getline(std::ifstream, std::string). The second parameter should be a string, not a char. If you need to extract the char out of it, do bounds checking and then ch = line[0]; But I think you want to loop through every character, so you'd loop through the string.
Last edited on
That's what I thought, it doesn't work with char. I can't use arrays, but I have a hard time remembering how to loop through a string at the moment.
there are 2 getlines. use the one above.

don't read a char at a time, even if you process it that way.
just read into a string.
you can loop through a string like an array, using []
for(char &c : stringvar)
c = c^17;

or
for(i = 0; i < stringvar.length(); i++)
stringvar[i] +=42;

etc.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream file_in(...);

string line;
whlie (getline(file_in, line))
{
    for (size_t i = 0; i < line.length(); i++)
    {
        // use line[i], the individual character
        // printing just an example
        cout << line[i];
    }
    cout << '\n';
}
Last edited on
@OP
getline() is not a simple way to do it.
Just read the input file character by character.
Here's a cut down version of your code for you to take another look at what you are doing. When you're testing a good move is to hard code parts instead of typing in the same old stuff each time.

My input text file is:

JULIUS CAESAR or Now is the time for all good men
and women to come
to the aid of their party.


The output displayed (but not filed) is:

file open 
WHYVHF PNRFNE BE ABJ VF GUR GVZR SBE NYY TBBQ ZRA
NAQ JBZRA GB PBZR
GB GUR NVQ BS GURVE CNEGL.
 testWHYVHF PNRFNE BE ABJ VF GUR GVZR SBE NYY TBBQ ZRA
NAQ JBZRA GB PBZR
GB GUR NVQ BS GURVE CNEGL.

-------------------------------------
Great! Program ended with exit code: 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    // Defines the input as a character, and an encrypted character
    char ch, encrypt;
    
    // Makes a flag for any input errors
    bool errorFlag;
    string inFileName{"text_in.txt"}, outFileName{"text_out.txt"};
    
    ifstream plainTextFile;
    string inputString, outputString;
    
    plainTextFile.open(inFileName); //(put input of cin filename here)
    
    while(!plainTextFile.is_open())
    {
        cout << "Unable to open file. Enter correct filename: ";
        cin >> inFileName;
        plainTextFile.open(inFileName);
    }
    
    if(plainTextFile.is_open())
    {
        cout << "file open \n";
    }

    while(plainTextFile >> std::noskipws >> ch) // <--
    {
        // Converts
        ch = toupper(ch);
        switch (ch)
        {
            case 'A' :
                encrypt = 'N';
                break;
            case 'B' :
                encrypt = 'O';
                break;
            case 'C' :
                encrypt = 'P';
                break;
            case 'D' :
                encrypt = 'Q';
                break;
            case 'E' :
                encrypt = 'R';
                break;
            case 'F' :
                encrypt = 'S';
                break;
            case 'G' :
                encrypt = 'T';
                break;
            case 'H' :
                encrypt = 'U';
                break;
            case 'I' :
                encrypt = 'V';
                break;
            case 'J' :
                encrypt = 'W';
                break;
            case 'K' :
                encrypt = 'X';
                break;
            case 'L' :
                encrypt = 'Y';
                break;
            case 'M' :
                encrypt = 'Z';
                break;
            case 'N' :
                encrypt = 'A';
                break;
            case 'O' :
                encrypt = 'B';
                break;
            case 'P' :
                encrypt = 'C';
                break;
            case 'Q' :
                encrypt = 'D';
                break;
            case 'R' :
                encrypt = 'E';
                break;
            case 'S' :
                encrypt = 'F';
                break;
            case 'T' :
                encrypt = 'G';
                break;
            case 'U' :
                encrypt = 'H';
                break;
            case 'V' :
                encrypt = 'I';
                break;
            case 'W' :
                encrypt = 'J';
                break;
            case 'X' :
                encrypt = 'K';
                break;
            case 'Y' :
                encrypt = 'L';
                break;
            case 'Z' :
                encrypt = 'M';
                break;
            case ' ' :
                encrypt = ' ';
                break;
            case '.' :
                encrypt = '.';
                break;
            case '\n' :
                encrypt = ch;
                break;
            default :
                errorFlag = true;
                cout << ch << " not found in the ROT13 coding scheme." << endl;     // Default catch all other characters
        }
        if (!errorFlag)
        {
            
            cout << encrypt;
            outputString += encrypt;
            ch++;
        }
    }
    cout << " test" << outputString << "\n";
    cout << "-------------------------------------\n";
    cout << "Great! ";
    
    return 0;
}


Ya I tried using Ganado's code, but the "<<" operator ignores the newlines when I write. It works fine for the display output, but I only get the very last line of "to the aid of their party." which translates to "GB GUR NVQ BS GURVE CNEGL." when it writes.

Alas, it's very late for me, and the homework is 2 hours overdue so I have most of it working and have to turn it in now. I have no idea how to write to a file without using "<<" because it's going to ignore the newline every time.

@againtry Unless you are saying the char method forces the newline to be written.
Look at L133 above. That outputs a char. If the input char is '\n' then '\n' is also output as required. You need to open a file for output. So L32 becomes something like:

1
2
3
std::ofstream cipherFile(outFileName);
if (!cipherFile)
    return (std::cout << "Cannot open output file\n"), 1;


and then L133 becomes:

 
cipherFile << ch;


and the encrypted text is written to the 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    // Defines the input as a character, and an encrypted character
    char ch, encrypt;
    
    // Makes a flag for any input errors
    string outputString;
    bool errorFlag;

    
    string inFileName{"text_in.txt"};
    ifstream plainTextFile;
    plainTextFile.open(inFileName);
    
    string outFileName{"text_out.txt"}; // <--
    ofstream translatedTextFile; // <--
    translatedTextFile.open(outFileName);  // <--
    
    while(!plainTextFile.is_open())
    {
        cout << "Unable to open file. Enter correct filename: ";
        cin >> inFileName;
        plainTextFile.open(inFileName);
    }
    
    if(plainTextFile.is_open())
    {
        cout << "file open \n";
    }

    while(plainTextFile >> std::noskipws >> ch) // <--
    {
        // Converts
        ch = toupper(ch);
        switch (ch)
        {
            case 'A' :
                encrypt = 'N';
                break;
            case 'B' :
                encrypt = 'O';
                break;
            case 'C' :
                encrypt = 'P';
                break;
            case 'D' :
                encrypt = 'Q';
                break;
            case 'E' :
                encrypt = 'R';
                break;
            case 'F' :
                encrypt = 'S';
                break;
            case 'G' :
                encrypt = 'T';
                break;
            case 'H' :
                encrypt = 'U';
                break;
            case 'I' :
                encrypt = 'V';
                break;
            case 'J' :
                encrypt = 'W';
                break;
            case 'K' :
                encrypt = 'X';
                break;
            case 'L' :
                encrypt = 'Y';
                break;
            case 'M' :
                encrypt = 'Z';
                break;
            case 'N' :
                encrypt = 'A';
                break;
            case 'O' :
                encrypt = 'B';
                break;
            case 'P' :
                encrypt = 'C';
                break;
            case 'Q' :
                encrypt = 'D';
                break;
            case 'R' :
                encrypt = 'E';
                break;
            case 'S' :
                encrypt = 'F';
                break;
            case 'T' :
                encrypt = 'G';
                break;
            case 'U' :
                encrypt = 'H';
                break;
            case 'V' :
                encrypt = 'I';
                break;
            case 'W' :
                encrypt = 'J';
                break;
            case 'X' :
                encrypt = 'K';
                break;
            case 'Y' :
                encrypt = 'L';
                break;
            case 'Z' :
                encrypt = 'M';
                break;
            case ' ' :
                encrypt = ' ';
                break;
            case '.' :
                encrypt = '.';
                break;
            case '\n' :
                encrypt = ch;
                break;
            default :
                errorFlag = true;
                cout << ch << " not found in the ROT13 coding scheme." << endl;     // Default catch all other characters
        }
        
        translatedTextFile << encrypt; // <--
        
        if (!errorFlag)
        {
            
            cout << encrypt;
            outputString += encrypt;
            ch++;
        }
    }
    cout << " test" << outputString << "\n";
    cout << "-------------------------------------\n";
    cout << "Great! ";
    
    return 0;
}
@OP Leaving it to the last minute to make a couple of simple changes as I've shown is not a good move for you to make.
Yeah, I should have read OP's code closer, character by character is simpler than dealing with getline.

The switch statements could also be simplified with arithmetic/modulus.
Last edited on
@againtry well I'm not sure if you are saying I've waited till late before, but I was in a tutor zoom with a new tutor, and we actually wasted about 2 hours on one problem on another program that was due but had higher awarded points. I had planned on getting through maybe both programs during the session, so I ended up finding the answer myself then ended the meeting which left me only a few hours to finish the second one.

The one thing I did learn from that mess, was that I can't assume I can functionally decomp a program, just by breaking up already written code into functions, and if it breaks it after that change, making reference variables to pass important data doesn't always help either.
Topic archived. No new replies allowed.