I'm having trouble converting a char buffer in a FILE* (if this is even possible, of course!). I have an enumeration defines as such:
typedef enum SchemaType{
SCHEMA_INT,
SCHEMA_REAL,
SCHEMA_STRING,
SCHEMA_BOOLEAN,
SCHEMA_CHAR,
SCHEMA_SIMPLE,
SCHEMA_RECORD,
SCHEMA_ARRAY
} SchemaType;
I have a perfectly functioning function that prints out the enumeration itself (for debugging purposes):
void printSchemaType(FILE* f,SchemaType schemaType){
switch (schemaType){
case SCHEMA_INT: fprintf(f,"int"); break;
case SCHEMA_ARRAY: fprintf(f,"array"); break;
case SCHEMA_BOOLEAN: fprintf(f,"boolean"); break;
case SCHEMA_CHAR: fprintf(f,"char"); break;
case SCHEMA_REAL: fprintf(f,"real"); break;
case SCHEMA_RECORD: fprintf(f,"record"); break;
case SCHEMA_STRING: fprintf(f,"string"); break;
}
}
Now the problem: I need to use the string printed out by fprintf (i.e. "int", "char", "real", ex cetera) as key in an hashtable; for this purpose i would like to store the printed string of printSchemaType() inside a variable, maybe by using sprintf:
char buffer[25];
sprintf(buffer,"%s",printSchemaType_output);
The problem is that I need to get the function output inside a variable! I know I could:
- create a new function
storeSchemaTypeInsideBuffer(const char* buffer,SchemaType type);
with a switch ... case but i would prefer not to because I don't want redundant code for a little thing like this; - change the prototype of "printSchemaType" into
void printSchemaType(const char* buffer,SchemaType schemaType)
but I would prefer not to because there are other functions like printSchemaType with a similar prototype (printTYPE(FILE* f, TYPE typevariable)
) and changing its prototype would create a differentiation of prototypes between this only function and the other ones;
The solution I was thinking is to transform a char buffer into a FILE* variable: in this way I would be able to do something like this:
SchemaType=SCHEMA_INT;
char buffer[25];
FILE* bufferActingAsAFile=make_buffer_acts_as_a_File(buffer);
fprintf(bufferActingAsAFile,type); //now in the buffer there is stored "int"
Is something like this even possible in C? If yes, how can I do that?