|
-
May 3rd, 2009, 09:12 AM
#1
Should be the last time I bother everyone.
I'm trying to use the command line prompt to perform four operations. Encrypt a .txt file, decrypt a .txt file, output the first five lines on the screen, output the last five lines on the screen. I can encrypt and decrypt the file but I can't get the first five lines to print. Any help?
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int htlen=5;
const int ascLEn=256;
const int ecryptCode=10;
void print_usage()
{
cout<<"Usage"<<endl;
cout<<" lab12 -e file1 file2 Encrypt file1 and write cyphertext to file2"<<endl;
cout<<" lab12 -d file1 file2 Decrypt file1 and write paintext to file 2"<<endl;
exit(1);
}
int main(int argc, char* argv[])
{
if (argc != 4)
print_usage();
if (strcmp(argv[1], "-e") !=0&&strcmp(argv[1], "-d")!=0)
print_usage();
ifstream inData;
ofstream outData;
inData.open(argv[2]);
outData.open(argv[3]);
if (inData.fail()||outData.fail())
{
cout<<"File open error!"<<endl;
return 1;
}
string s;
if (strcmp(argv[1], "-e")==0)
{
while (!inData.eof())
{
getline(inData, s);
for (int i=0;i<s.length();i++)
s[i]=char(s[i] + ecryptCode)%ascLEn;
outData<<s;
if (!inData.eof())
outData<<endl;
}
}
else
{
while (!inData.eof())
{
getline(inData, s);
for (int i=0;i<s.length();i++)
s[i]=char(s[i]+ascLEn-ecryptCode)%ascLEn;
outData<<s;
if (!inData.eof())
outData<<endl;
}
}
inData.close();
outData.close();
return 0;
system("pause");
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|