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");
}

So it is outputting correctly to a file, but you want everything that is outputted to the file to also be outputted to the screen?

Yes, I can get the file to be correct, but I can't figure out how to view it on the screen. If I was to use the -h switch I would want to be able to see the first five lines of the .txt file.

Yes, I can get the file to be correct, but I can't figure out how to view it on the screen. If I was to use the -h switch I would want to be able to see the first five lines of the .txt file.

Use the same lines as you did when you outputted to the file, but replace them with cout?

outData<<s;
                  if (!inData.eof())
                        outData<<endl;
cout<<s;
                  if (!inData.eof())
                        cout<<endl;

Here is what I'm trying now, but I'm still not getting any output. I'm sure it's something obvious but I can't see it. Even if I use the -h switch in the command prompt I still get no output.

#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 if (strcmp(argv[1], "-d")==0)
	{
		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;
		}
	}
	else if (argv[1], "-h")
	{
		int line = 0;
            while (!inData.eof())
            {
              if (line++ < 5)
              {
                std::cout << s << std::endl;
              }
			}

	}
	
	inData.close();
	outData.close();
	return 0;
	system("pause");
}

try this

#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;
                                                cout << s;    // this will display to the screen
			if (!inData.eof())
				outData<<endl;
		}
	}
	else if (strcmp(argv[1], "-d")==0)
	{
		while (!inData.eof())
		{
			getline(inData, s);
				for (int i=0;i<s.length();i++)
					s[i]=char(s[i]+ascLEn-ecryptCode)%ascLEn;
			outData<<s;
                                                cout << s;  //this will display to the screen
			if (!inData.eof())
				outData<<endl;
		}
	}
	else if (argv[1], "-h")
	{
		int line = 0;
            while (!inData.eof())
            {
              if (line++ < 5)
              {
                std::cout << s << std::endl;
              }
			}

	}
	
	inData.close();
	outData.close();
	return 0;
	system("pause");
}

you use cout to display to the screen

This article has been dead for over six months. Start a new discussion instead.