Help with ifstream/ofstream program homework

Good Evening! I am working on a homework assignment that is having me take a text file, that has been modified by adding 5 to the ASCII code, and re-modifying it to read the actual file (5 less ASCII). I have tried so many different options!!! I finally got the code to decode all of the file, but I cannot seem to get the lines separated. I tried using getline, but when I do use it, it only decodes every other line. Someone please help me with this! Any help would be much appreciated!!
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
	char ch;
	string str;

	//for file input
	ifstream inputFile;

	//for file output
	ofstream outputFile;


	//define input and output files
	inputFile.open("plain.code");
	outputFile.open("plain2.txt");


	//decode file - until you reach the file end, decode each character
	while (!inputFile.eof())
	{
//*		getline(inputFile, str);

		inputFile.get(ch);


		//decode each character within the line
//*		while (ch != '\n')
//*		{
			
			ch = ch - 5;
	
			outputFile.put(ch);
	
//*			inputFile.get(ch);
//*		}
	}

	//close both files
	inputFile.close();
	outputFile.close();

	return 0;
}


The areas with the '//*' comments, is where I tried calling each line and decoding that way, but only every other line appears in the file... Help! Anyone?
The best approach is to use getline in my opinion This involves getting a line into str, examining the character in str and output those to the outputFile. The problem with the way you did that is that if you first read a line with getline, inputFile goes to the next line which you then read with get(ch). You should examine str instead. That's why you missed lines.

Read here: http://www.cplusplus.com/reference/istream/istream/getline/
getline: "Extracts characters from the input sequence"

The other option is to use get(ch), but output '\n' if the ch is '\n'. You should skip ch = ch - 5; if ch == '\n' evaluates to true and get rid of the while statement.
Last edited on
So I updated my code to get the string, and then look at the character within the string. (I think...) this is what I updated it to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	while (!inputFile.eof())
	{
		getline(inputFile, str);


		//decode each character within the line
		while (str.length())
		{
			inputFile.get(ch);

			ch = ch - 5;
	
			outputFile.put(ch);
		}

Is that what you meant for me to look at again? Or did I misunderstand? (I am sorry - I am still very new to this...)
Becuase when I do, the output looks something like this:" 獡楳湧敭瑮⌠〱‮潙牵瀠潲牧浡猠潨汵⁤湥潣敤琠楨ճ楦敬戠⁹摡楤杮㔠琠桴⁥獁䥃⁉慶畬⁥景攠捡⁨档牡捡整⁲湩椠⹴䤅潹牵攠据摯摥映汩"
getline: "Extracts characters from the input sequence"

Once you do inputFile.open("plain.code");, the inputFile contains all the characters that it found in the file.

Doing getstring extracts the characters of one line (that ends with '\n') and puts them in str. So you will never find the characters of this line in inputFile anymore. They are gone. They are now in str.
When you do inputFile.get(ch) it operates in the same way. It extracts not one line, but one character from inputFile. So the char is gone from inputFile. It is now in ch.

One thing you should not do then is use both getline and get(ch). The first removes a line, the other removes a character from inputFile.

I suppose you should give my second option a try:
The other option is to use get(ch), but output '\n' if the ch is '\n'. You should skip ch = ch - 5; if ch == '\n' evaluates to true and get rid of the while statement.


So what should you do:
1. get rid of getstring.
2. get rid of while (str.length())
3. figure out a way to avoid the ch = ch - 5 if you are dealing with the endline character '\n'.

A line break is simply one character in C. You don't want to do minus 5 on the line breaks. You only want to do that with the other characters.

1
2
3
4
 if (ch != '\n') //if the character read by inputFile.get(ch) is NOT a line break
{
      ch = ch - 5;
}
Last edited on
Your first code works (almost) fine.
What do you mean by
I cannot seem to get the lines separated
?
Thank you for all your help methodos! It finally works!!! ^_^
@ggirl87

while (!inputFile.eof())

The use of eof like that is not recommended. Use the good bit instead. Also if you open a file check that it worked.

See the examples here.


http://www.cplusplus.com/doc/tutorial/files/


HTH
Topic archived. No new replies allowed.