4 Replies - 305 Views - Last Post: 19 November 2012 - 03:26 PM Rate Topic: -----

#1 shelly11  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-November 12

Changing C++ codes to simple C codes.

Posted 19 November 2012 - 12:36 PM

Hi there, I am really new to learning codes. I have a problem that i need help with. I found this code online and I have been trying to run it in Dev++ but to no avail :(/>. I'm thinking that since the codes are C++ and not just C codes, there might be the problem. so my question is how can i convert the following codes to C codes in order for it to run with dev++

The aim is to write an XOR cipher program using the C programming language. Your program must accept as input from the user a value between 0 and 255 to be used as the secret key, the name of the input file and the name of the output file. No line in the input file should contain more than 4096 characters. After the user would have provided their secret key, your program should read and perform an XOR cipher on the contents of the input file and write the result to the output file. If the input file has already been encrypted and the identical secret key that was used to perform the initial encryption is provided, then the contents of the output file should be deciphered into its original plain text.


Thank you so very much for all the help. Do appreciate it.

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;


int main(int argc, char* argv[])
{
    try
    {
        if (argc < 4)
        {
            cout << argv[0] << "\n";
            cout << "Usage: cipher key infile outfile\n";
        }
        else
        {
            string line;
            stringstream ss;
            ss << argv[1];
            unsigned int key = 0;
            ss >> key;
            cout << "Key is " << key << "\n";
            if (key > 255)
            {
                cerr << "Key value is to high 255 < " << key << "\n";
                return 1;
            }
            ifstream infile(argv[2]);
            ofstream outfile(argv[3]);
            if (infile.is_open())
            {
                while (infile.good())
                {
                    getline(infile, line);
                    int lineLen = line.length();
                    if (lineLen > 4096)
                    {
                        cerr << "String in file is too long 4096 < " << lineLen << "\n";
                        infile.close();
                        outfile.close();
                        return 1;
                    }


char toCipher[lineLen];
                    for (int ii = 0; ii < lineLen; ii++)
                    {
                        toCipher[ii] = line[ii] ^ key;
                    }
                    outfile << toCipher;
                }
                infile.close();
                outfile.close();
            }
        }
        return 0;
    }
    catch(...)
    {
        cerr << "An unknown error occured." << endl;
        return 1;
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Changing C++ codes to simple C codes.

#2 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5382
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Changing C++ codes to simple C codes.

Posted 19 November 2012 - 12:39 PM

View Postshelly11, on 19 November 2012 - 02:36 PM, said:

Your program...

So is it safe to assume this is your project, & you just found a C++ example, & want it converted to C because that is the language the project was assigned to be written in?

View Postshelly11, on 19 November 2012 - 02:36 PM, said:

I found this code online

Why are you taking a class if you don't want to learn? Finding projects online isn't going to 'help' you learn, it only completes the project.
Was This Post Helpful? 2
  • +
  • -

#3 Switters  Icon User is offline

  • D.I.C Head

Reputation: 25
  • View blog
  • Posts: 110
  • Joined: 03-June 12

Re: Changing C++ codes to simple C codes.

Posted 19 November 2012 - 12:48 PM

View Postshelly11, on 19 November 2012 - 12:36 PM, said:

I'm thinking that since the codes are C++ and not just C codes, there might be the problem. so my question is how can i convert the following codes to C codes in order for it to run with dev++


You've answered your own question. You need to convert the code into C from C++.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,293
  • Joined: 25-December 09

Re: Changing C++ codes to simple C codes.

Posted 19 November 2012 - 01:13 PM

You would be better off writing this program from scratch rather than trying to convert this C++ code to C code.


Jim

This post has been edited by jimblumberg: 19 November 2012 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

#5 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Changing C++ codes to simple C codes.

Posted 19 November 2012 - 03:26 PM

Seriously, this is something you can easily do in C - and you NEED to practice your skills in writing code. You can't learn to swim by just trying to imitate what others have done in the water, and never get wet yourself.

What do you want to XOR here? Every char in the string?

You need to use fgets with a char buffer of 4098 (4096 + 1 and one more because computers love even numbers).

Get started, and you'll find few problems here. EZ shmeezy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1