Parsing command line parameters.
May 19, 2013 at 4:41pm UTC
I have a school assignment: I have to make a c++ program, in which with an algorithm I have to code a text from a file and write it to another file. The input should like this: "code forCoding.txt toBeWritten.txt" ; or like this: "decode toBeReadFor.txt toBeWrittenIn". I have done everything except one thig: It is says I have to be able to input parameter. How should i write this? I read
http://www.cplusplus.com/articles/DEN36Up4/, but still dont get. The input of my program has to have 3 strings, so I guess argc should be 3, but I dont really get it. Can someone write it for me, or at least to say what should I have in my main about this parsing command line parameters?
May 19, 2013 at 4:47pm UTC
Can you show your code so far right now? I want more of an idea of what you're doing.
May 19, 2013 at 5:25pm UTC
Sure! This is my main class and i have a another 2 classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <iostream>
#include "include/coder.h"
#include "include/decoder.h"
using namespace std;
int main()
{
string nameIn, nameCd,type;
cin>>type;
cin>>filename1>>filename2;
if (type=="code" )
{
coder *c= new coder(filename1,filename2);
}
else if (type=="decode" )
{
decoder *dcdr=new decoder(filename2,filename1);
}
else
{
cout<<"not a right input" ;
}
return 0;
}
The class for the coding
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include "../include/coder.h"
#include<iostream>
#include<string.h>
#include <fstream>
using namespace std;
coder::coder()
{
}
coder::coder(string nameIn,string nameOut)
{
setFileIn(nameIn);
codirai();
setFileOut(nameOut);
}
void coder:: setFileIn(string filename)
{
ifstream reader;
reader.open(filename.data(), ios::in);
if (reader.is_open())
{
reader.seekg(0, ios::end);
int length = reader.tellg();
reader.seekg(0, ios::beg);
char qwe[length];
reader.read(qwe, length);
string result(qwe);
int k = reader.gcount();
result = result.substr(0, k);
reader.close();
setText(result);
}
else
{
cout<<"Greshka pri otvarqneto na file za kodirane" ;
}
}
void coder:: setFileOut(string filename)
{
ofstream myfile;
myfile.open(filename.c_str());
myfile<<getCoded();
myfile.close();
}
void coder::setText(string niki)
{
if (niki!="" )text=niki;
else {
cout<<"Tekstut za kodiranet e vuveden greshno!" ;
text="BLQH" ;
}
}
string coder::getCoded()
{
return coded;
}
void coder::setCoded(string gshit)
{
if (gshit!="" ) coded=gshit;
else {
cout<<"Greshka pri kodiraneto!" ;
coded="BLQH" ;
}
}
void coder::codirai()
{
string coded1;
int n,k,t;
n=getText().size();
cout<<getText();
//get nai-blizko
int razlika=n-1,naiblizko=1;
for (int i=2; i<n; i++)
{
if (n-(i*i)<0) break ;
if (n-(i*i)<razlika)
{
naiblizko=i;
razlika=n-(i*i);
}
}
k=naiblizko;
//ostanala
t=n/k+1;
char a[k][t];
int l=0;
for (int i=0; i<k; i++)
{
for (int j=0; j<t; j++)
{
if (l<n)
{
a[i][j]=getText()[l];
}
else
{
a[i][j]=' ' ;
}
l++;
}
}
char b[t][k];
for (int i=0; i<t; i++)
{
for (int j=0; j<k; j++)
{
b[i][j]=a[j][i];
}
}
for (int i=0; i<t; i++)
{
for (int j=0; j<k; j++)
{
coded1=coded1+b[i][j];
setCoded(getCoded()+b[i][j]);
}
}
}
The decoding class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#include "../include/decoder.h"
#include<iostream>
#include<string.h>
#include <fstream>
using namespace std;
decoder::decoder(string input, string output)
{
setFileIn(input);
decode();
setFileOut(output);
}
void decoder::setFileIn(string filename)
{
ifstream reader;
reader.open(filename.data(), ios::in);
if (reader.is_open())
{
reader.seekg(0, ios::end);
int length = reader.tellg();
reader.seekg(0, ios::beg);
char text1[length];
reader.read(text1, length);
string result(text1);
int k = reader.gcount();//?
result = result.substr(0, k);//?
reader.close();
cout<<" rezila: " <<result;
setCoded(result);
}
else
{
cout<< "ne e otvoreno" ;
}
}
void decoder::setFileOut(string filename)
{
ofstream myfile;
myfile.open(filename.c_str());
myfile<<getText();
myfile.close();
}
void decoder:: setCoded(string coded1)
{
if (coded1!="" ) coded=coded1;
else
{
cout<<"Kodiraniqt text ne e vuveden pravilno!" ;
coded="BLQH" ;
}
}
void decoder::setText(string text1)
{
if (text1!="" ) text=text1;
else
{
cout<<"Greshka pri kodiraneto!" ;
text="BLQH" ;
}
}
string decoder:: getText()
{
return text;
}
string decoder::getCoded()
{
return coded;
}
void decoder::decode()
{
string text1;
int n,k,t;
n=getCoded().size();
cout<<getCoded();
//get nai-blizko
int razlika=n-1,naiblizko=1;
for (int i=2; i<n; i++)
{
if (n-(i*i)<0) break ;
// cout<<endl<<"za i="<<i<<" ee="<<n-(i*i);
if (n-(i*i)<razlika)
{
// cout<<endl<<"tuk sum";
naiblizko=i;
razlika=n-(i*i);
// cout<<endl<<"razlikata e"<<razlika;
}
}
k=naiblizko;
//ostanala
t=n/k+1;
// cout<<"n="<<n<<" k="<<k<<" t="<<t<<endl;
char a[t][k];
int l=0;
for (int i=0; i<t; i++)
{
for (int j=0; j<k; j++)
{
if (l<n)
{
a[i][j]=getCoded()[l];
}
else
{
a[i][j]=18;
}
l++;
}
}
char b[k][t];
for (int i=0; i<k; i++)
{
for (int j=0; j<t; j++)
{
b[i][j]=a[j][i];
}
}
for (int i=0; i<k; i++)
{
for (int j=0; j<t; j++)
{
if (b[i][j]==18) continue ;
//cout<<b[i][j]<<" ";
text1=text1+b[i][j];
}
setText(text1);
}
}
Last edited on May 19, 2013 at 5:26pm UTC
May 19, 2013 at 6:45pm UTC
1 2 3 4 5 6 7 8 9 10 11
int main(int argc, char **argv)
{
if (argc<4){ //the program name counts as an argument
usage(); //how they are supposed to use it
return 1; //error
}
string
input_filename = argv[2],
output_filename = argv[3],
type = argv[1];
Also
1 2
//coder *c= new coder(filename1,filename2);//leak
coder c(filename1,filename2);
Last edited on May 19, 2013 at 6:46pm UTC
May 19, 2013 at 6:51pm UTC
Thanks so much for the help!
Topic archived. No new replies allowed.