Please help with passing (char * name[])
Please help, I'm new to C++ and struggling with this problem.
I have an array of char and an array of double declared in main as follow: (Please ignore uc1601 and UF_UI codes because they are Ug/Open API user function.)
int flag;
char *variables[36];
double values[36];
then a call to read from file in main:
flag = read_variables(variables,value);
In the read_variables function I have:
#include <cstdlib>
#include <fstream>
#include <iostream>
using std::ios;
using std::ifstream;
using std::istream;
using std::fstream;
#include <string>
#include <sstream>
int read_variables(char*[],double []);
int read_variables (char *variables_name,double variables_value[])
{
const char textfile[133] = {"C:\\My
Documents\\Project_VC\\Key_retainer\\variables_fil e.txt"};
char name[133], msg[133];
double value;
int ok;
ifstream OpenToRead(textfile, ios::in);
if (!OpenToRead)
{
sprintf(msg ,"Can not open this %s \n file to
read ",textfile );
uc1601(msg, 1);
return (1);
}
else
{
UF_UI_open_listing_window();
for (int i = 0; i < variable_count; i++)
{
OpenToRead >> name >> value;
variables_name[i] = name;
variables_value[i] = value;
}
OpenToRead.close();
for (int i = 0; i<variable_count; i++)
{
sprintf(msg ,"variable %s = %2.4f\n", variables_name[i],
variables_value[i]);
UF_UI_write_listing_window(msg);
}
}
Here are my problems:
1) The variables_name[] print out only the last variable that read from the file. but the variables_value[] print out correct.
2) I print it agian back in main and I got the same result.
3) Why it only retain 1 name (last one in the file)
Here are ehat in the file:
.
.
.
UNDERCUT_THICKNESS 33
UNDERCUT_ANGLE 34
WINDOW_WIDTH 35
UNDERCUT_WIDTH 36
BLIND_SLOT_HOLE_DIA 0.195
And here are what it print out:
variable BLIND_SLOT_HOLE_DIA = 33.0000
variable BLIND_SLOT_HOLE_DIA = 34.0000
variable BLIND_SLOT_HOLE_DIA = 35.0000
variable BLIND_SLOT_HOLE_DIA = 36.0000
variable BLIND_SLOT_HOLE_DIA = 0.1950
Please help! I'm struggling with this problem all day yesterday.
Many thanks in advance.
Dang
|