first prog - first post

T

Theron NightStar

I am trying to teach myself c++. This is the first program I have ever
written that might have an practical use to me. I rather proud of it since
like implied - I have no real knowledge of c++.

Anyways, here is my problem - This prog does what it is supposed to (a very
very crude encryption/decryption of text files) but for some reason it
always duplicates the last character 2 extra times. If I were to run the
word

this

through it, it would come out

thisss

Please spare me the flames of wrath for mine stupidity - again, this was
simply to learn something about file i/o

Here is the prog source

// This is an encyrption/decryption program. Nothing fancy - it is a file
i/o
// learning exercise. it is a VERY VERY crude version. I will make it more
// modular and "proper" as I learn more.

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[])
{
string iFilename;
string oFilename;
char in_ch;
char out_ch;

ifstream iFile;
cout << "Please tell me the name of the Input File:\n";
cin >> iFilename;
iFile.open(iFilename.c_str());
if (! iFile.is_open())
{
cout << iFilename << " does not exist. Exiting now.\n\n";
cout << "Press enter to exit...";
cin.get();
return 1;
}

ofstream oFile;
cout << "Give me a name for the output file:\n";
cin >> oFilename;
oFile.open(oFilename.c_str());
if (! oFile.is_open())
{
cout << oFilename << "is not getting opened for some reason.\n";
cout << "Exiting now.\n\n";
cout << "Press enter to exit...";
cin.get();
return 1;
}

while (! iFile.eof())
{
iFile.get(in_ch);
out_ch = ~in_ch;
oFile << out_ch;
}

iFile.close();
oFile.close();

cout << "Press enter to exit...";
cin.get();
return EXIT_SUCCESS;
}
 
A

Alf P. Steinbach

* Theron NightStar:
while (! iFile.eof())
{
iFile.get(in_ch);
out_ch = ~in_ch;
oFile << out_ch;
}

Consider an in-file that contains a single character 'o'. First
time around there's no end-of-file, the loop body executes, and
the character is thereby copied to the out-file. End-of-file has
still not been detected because no attempt has been made to read
beyond the last character, so the loop goes one more time. This
time the 'get' operation does not change 'in_ch' because there's
no more data in the file. Instead the internal end-of-file flag
is set. But the loop body proceeds to copy the character to the
out-file. Now end-of-file is set and the loop terminates.
 
N

nooneinparticular

sorry bout the double post. Apparently one of my news readers had a
copy of this lying around waiting to go out and I didn't catch it - my
apologies
 
P

philip.goh

This is the offending section of code:
while (! iFile.eof())
{
iFile.get(in_ch);
out_ch = ~in_ch;
oFile << out_ch;
}

Instead of using iFile.eof(), you should rewrite the loop like so:
while (iFile >> in_ch)
{
out_ch = ~in_ch;
oFile << out_ch;
}

Alf P. Steinbach has provided a very good explanation of why this is
the case.

HTH,
Philip
 
J

Jon Bell

Instead of using iFile.eof(), you should rewrite the loop like so:
while (iFile >> in_ch)
{
out_ch = ~in_ch;
oFile << out_ch;
}

Or if he doesn't want to skip whitespace (as >> does), he can use

while (iFile.get(in_ch))
{
// ... etc.
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top