C++ to c converter

shellyann11

Honorable
Nov 19, 2012
2
0
10,510
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;
}
}
 

cl-scott

Honorable
I can't find a "dev++" via a simple Google search, but I do come up with a dev-C++.

Either way, short of basically rewriting that entire program, you're not going to be able to convert it from C++ to C. It's making pretty heavy use of C++ input and output operators which would have to be replaced with C in/output operators. Once you do that, you've replaced about 75% of the code listed.

So what exactly is the error you're getting when you try and compile that code you listed with a C++ compiler? And have you tried other free C++ compilers like GCC?