?
Solved

c++ output .txt file

Posted on 2009-05-03
2
Medium Priority
?
890 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 1500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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 connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

752 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