I need to output the first five lines or the last five lines of a .txt file the user provides while using switches in a command prompt. I've got it other than printing the lines to the screen.
#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");
}