Hi there; this question may be trivial to some, since I am new to C++, although I come from C.
I have a struct that defines a type (typeInput), and a class with a funtion that uses some of the members of that struct (see the code below).
What I have not been able to do is the following:
once I use the class function mesh.ReadInput to assign two strings from an input file, I cannot pass them into an other function of the same class.
Specifically, if you look at the code reported hereafter (I divided it in [1],[2], and [3] for the sake of clarity), after assigning correctly two strings to what are called Input.msh_file, when I print its value in main after the call to ReadInput, I get an empty output.
Can anyone help?
Thank you very much in advance,
S.
1] Struct and Class definitions (Mesh.H):
Code:
typedef struct InputTag{
char msh_file[32];
int nsd;
} typeInput;
class Mesh{
private:
int nsd;
int nelem;
int nnodes; // number of nodes (of the mesh).
public:
typeInput Input;
Mesh(void); // constructor
//Functions:
void Mesh::ReadInput(char *input_file, char *msh_generator, char *mesh_file, int *nsd);
};
//END Class and Struct definitions
2] Mesh.cpp
Code:
void Mesh::void Mesh::ReadInput(char *input_file, char *msh_generator, char *mesh_file, int *nsd)
{
FILE *input_file_ID;
/*********************************************************/
//Start reading:
input_file_ID = fopen(input_file, "r");
//Read Entries of input file start now:
fscanf(input_file_ID, "%s \n", &(Input.msh_file));
}; //END Mesh.cpp where Mesh::ReadInput is defined.
3] main.cpp
Code:
int main(int argc, char *argv[])
{
/********************************/
Mesh mesh = Mesh();
int nsd, nnodes, nelem, nbdy_nodes, max_elnodes;
char *msh_file, *msh_generator;
/*********************************/
msh_file = (char*) malloc(32 * sizeof(char));
msh_generator = (char*) malloc(32 * sizeof(char));
//Read input file:
mesh.ReadInput( argv[1], msh_file, msh_generator, &nsd);
//!!! HERE IS THE PROBLEM!!! When I print out the strings msh_file and msh_generator that
should have been assigned in mesh.ReadInput, I get an empy output:
printf("msh_file :: %s \n", msh_file);
//Free dyn. memory
free(msh_file);
free(msh_generator);
return 0;
}