Sign in with
Sign up | Sign in
Your question

Help with changing C++ codes to C codes

Last response: in Windows XP
Share
November 19, 2012 5:29:47 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 :( . im 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;
}
}

More about : changing codes codes

November 19, 2012 6:24:20 PM

Sorry, but we will not do your homework assignments for you ;) 
m
0
l
!