Solved

c++ output .txt file

Posted on 2009-05-03
2
879 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
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: …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

740 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