0

The program almost runs but i am not sure how to make the .txt file for this , its not giving me an error.

the project asks me to:

" File encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file.

The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to the ASCII code of each character before it is written to the second file. "

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    char ch;
    fstream fin, fout;
    fin.open("testone.txt", ios::in);
    fout.open("encrypted.txt", ios::out);

    while (!fin.eof())
    {
        fin.get(ch);
        fout.put(ch + 10);
    }

    fin.close();
    fout.close();
    system("pause");
    return 0;
}
kingerick
  • 31
  • 6
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Steephen May 23 '15 at 03:31
  • Not sure about your question. Do you get the output file or not? – Tim3880 May 23 '15 at 03:52
  • its giving me ( errorLNK1561 ), which is telling me entry point must be defined , but im not sure what exactly is wrong i have the file in the same place as the program and its named correctly – kingerick May 23 '15 at 04:03
  • When you write code, try to build up from the simple to the complex. Does `HelloWorld` work? How about code that reads a single character from a file and writes it to the screen? How about code that writes a hard-coded character to a file? Small steps. – Beta May 23 '15 at 04:13
  • You said **its not giving me an error** in your post but said **its giving me ( errorLNK1561 ),** in the comment above, which one is it? Are you getting any error or is it just not writing to a file? – sam May 23 '15 at 04:19
  • sorry it was a typo sam – kingerick May 23 '15 at 04:48

2 Answers2

0

Not up on my Visual C, but you may need #include <cstdlib> to get system

LNK1561 means your main function can't be found. Clearly the main function is present, so this should compile. Follow Beta's suggestion and ensure you can compile and run a trivial program.

Putting Compiling issues aside, This code won't work.

Overarching Problem: You are not checking for any errors along the way, so there is no way for your program to tell if anything has gone wrong.

For example, what if the file didn't open? The while (!fin.eof()) becomes an infinite loop. If the file is not open, you can never read EOF. Trying to use EOF as a loop condition is a bad idea anyway. Definitely read the link in @Steephen's comment.

If you fail to read a character with fin.get(ch); then what? The current code tries to use the character anyway. Bad idea.

Testing a stream is pretty simple. if (!fin) does the job. Read up on how streams work to learn why. Thius simple test doesn't tell you what went wrong, but at least you know something went wrong.

To make things easier, most stream functions return the stream. This lets you chain stream operations together and makes if (!fin.get(ch)) an easy way to tell if get worked.

So your IO loop can be as simple as

while (fin.get(ch) && fout.put(ch + 10))
{
}

If get couldn't get ch for any reason--unopened file, end of file, unreadable file--the while loop exits. Afterwards you can query fin to find out why. If EOF, awesome. If not EOF, the output file's probably wrong.

The same applies to put. If put failed, the loop ends. Test for why and decide if you want to keep the file.

I also recommend dropping a quick test at the end of main to print out a check.

fin.open("encrypted.txt", ios::in);
while (fin.get(ch) && std::cout.put(ch - 10))
{
}

A better test would be to read the character, undo the encryption, and compare against the original input.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54