T
tvn007
I wrote the code below to read data from file into structure using C.
However, I would like to convert it to C++.
Could someone please give me some hints. I am not that famaliar with
C++
Thanks in advance
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct test {
char enabled,
put[20];
int type;
char desc[20];
} *results_ptr, Result[50];
/////////////////////////////////////////////////////////
void input (string& inputfile){
const int size = 2048;
char *ptr, buffer[size];
ifstream in(inputfile.c_str());
if (!in.is_open()) {
cerr <<"Cannot open file" "" <<inputfile<<endl;
exit (EXIT_FAILURE);
}
results_ptr = Result;
while (in.getline (buffer, size)){
ptr = strtok(buffer,"-,\t\n");
if (*ptr == '#')
continue;
results_ptr->enabled = *ptr;
ptr = strtok(NULL,"-,\t\n");
while (*ptr == ' ') ptr++;
(void)strcpy(results_ptr->put,ptr);
ptr = strtok(NULL,"-,\t\n");
while (*ptr == ' ') ptr++;
results_ptr->type = atoi(ptr);
ptr = strtok(NULL,",\t\n");
while (*ptr == ' ') ptr++;
(void)strcpy(results_ptr->desc,ptr);
results_ptr++;
}
in.close();
return ;
}
///////////////////////// INPUT //////////////////////////////////////
##############################################
N, banana, 1, yellow
Y, grass, 2, green
However, I would like to convert it to C++.
Could someone please give me some hints. I am not that famaliar with
C++
Thanks in advance
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct test {
char enabled,
put[20];
int type;
char desc[20];
} *results_ptr, Result[50];
/////////////////////////////////////////////////////////
void input (string& inputfile){
const int size = 2048;
char *ptr, buffer[size];
ifstream in(inputfile.c_str());
if (!in.is_open()) {
cerr <<"Cannot open file" "" <<inputfile<<endl;
exit (EXIT_FAILURE);
}
results_ptr = Result;
while (in.getline (buffer, size)){
ptr = strtok(buffer,"-,\t\n");
if (*ptr == '#')
continue;
results_ptr->enabled = *ptr;
ptr = strtok(NULL,"-,\t\n");
while (*ptr == ' ') ptr++;
(void)strcpy(results_ptr->put,ptr);
ptr = strtok(NULL,"-,\t\n");
while (*ptr == ' ') ptr++;
results_ptr->type = atoi(ptr);
ptr = strtok(NULL,",\t\n");
while (*ptr == ' ') ptr++;
(void)strcpy(results_ptr->desc,ptr);
results_ptr++;
}
in.close();
return ;
}
///////////////////////// INPUT //////////////////////////////////////
##############################################
N, banana, 1, yellow
Y, grass, 2, green