By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,484 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,484 IT Pros & Developers. It's quick & easy.

first prog - first post

P: n/a
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;
}
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
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
Jul 23 '05 #3

P: n/a
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

Jul 23 '05 #4

P: n/a
In article <11********************@g47g2000cwa.googlegroups.c om>,
<ph********@macosx.com> wrote:

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

--
Jon Bell <jt****@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.