Solved

c++ output .txt file

Posted on 2009-05-03
2
871 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
ID: 24292907
>>>> 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
ID: 24294259
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
edit html 3 132
Tomcat6 in eclipse 2 39
error when doin maven update 2 94
IronPython and Visual Studio 2013+ 2 91
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: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 use and create keystrokes in Netbeans IDE 8.0 for Windows.

895 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

16 Experts available now in Live!

Get 1:1 Help Now