Reputation: 141
I'm reading in a text file with a content like
char "ID", float "5.1", float "0.01", float "5", int "1"
I want to define a structure type variable with the types specified in the text file e.g.
struct myStruct
{
char ID;
double a1;
double a2;
double a3;
int index;
};
I don't know the types of these variable until I read the text file. Is there a way to do something like this in C?
Upvotes: 0
Views: 200
Reputation: 181849
I want to define a structure type variable with the types specified in the text file
[...]
I don't know the types of these variable until I read the text file. Is there a way to do something like this in C?
"like this", maybe, depending on your definition of "like", but not exactly what you describe.
C-level data types are a feature of C source code. They do not exist as such at runtime, so you certainly cannot define new ones then. But you can devise a system that handles text input consisting of data type tags and corresponding text data. You would then have control over the in-memory representation of the data, and if you wanted to do, you could parse and format it into a form similar to the representations of C structures. Any way around, though, you would need to provide your own functions for accessing the attributes of these pseudo-structures. The C member-access operators would not be suitable.
Note also that although I say "you can", that's in a generic sense of "you". This is not an easy or small project, and that you pose the question in the first place leads me to guess that it would be very challenging for you at your present level of experience.
Upvotes: 1