Solved

c++ output .txt file

Posted on 2009-05-03
2
866 Views
Last Modified: 2013-12-14
I need help quickly.  I am trying to get this program to do four things, using the command line prompt encrypt a .txt file, decrypt a .txt file, output the first five lines of text, output the last five lines of text.  I've got decrypting and encrypting going, but I can't figure out how to display the lines of the .txt file.
#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");
}
0
Comment
Question by:scuzzo
2 Comments
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
>>>> but I can't figure out how to display the lines of the .txt file.

To output the decrypted data you simply would use std::cout instead of outData to display it in a console.

For the encrypted data you may have unprintable characters. Hence you need to output them as hex code

   cout << hex << setw(2) << (unsigned char)s[i] << " ";

where you need to include <iomanip> additionally to <iostream> and <string>-


0
 

Author Comment

by:scuzzo
Comment Utility
Here is what I've got so far, show me where I'm going wrong and the 500 points are yours.I believe that it is the argc but I've been out too long.

#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;
      cout<<"      lab12 -h file1 See first five lines of file."<<endl;
      cout<<"      lab12 -t file1 See last five lines of file."<<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 (strcmp(argv[1], "-h")==0)//switch to see the first five lines of .txt file
      {
            int line = 0;
            while (!inData.eof())
            {
                        getline(inData, s);
                        if (line++ < 5)
                         {
                          std::cout << s << std::endl;
                         }
                  }

      }else
            cout<<"Please make another choice."<<endl;
      
      inData.close();
      outData.close();
      return 0;
      system("pause");
}
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sync project in eclipse locally 2 113
maven import project error 7 122
easiest way to find port number 12 103
VB.Net Reusable Form Layout 4 130
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now