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;
}
|