CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2007
    Posts
    34

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

  2. #2
    Join Date
    Dec 2008
    Posts
    56

    Re: Should be the last time I bother everyone.

    Have you tried outputting the string 's' to the screen using std::cout?

    Code:
    ...
                int line = 0;
                while (!inData.eof())
                {
                  ...
                  if (line++ < 5)
                  {
                    std::cout << s << std::endl;
                  }
                }
    P.S. If your string data contains unprintable characters, then iterate over the length of the string, outputting each character. You might find it easier to read these character if they are outputted in hex. Something like:
    Code:
    ...
    for (size_t i = 0; i < s.size(); ++i)
    {
      std::cout << std::setw(2) << std::setfill('0') << std::hex << (int) s[i] << std::dec << ' ';
    }
    std::cout << std::endl;
    Last edited by dwhitney67; May 3rd, 2009 at 10:08 AM.

  3. #3
    Join Date
    Jul 2007
    Posts
    34

    Re: Should be the last time I bother everyone.

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

  4. #4
    Join Date
    Dec 2008
    Posts
    56

    Re: Should be the last time I bother everyone.

    Would it not be helpful to have one of these statements inside that new while-loop you added:
    Code:
    getline(inData, s);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured