Reputation: 1407
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:
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;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?
Upvotes: 1
Views: 282
Reputation: 54325
Not what you asked for, but I would rewrite your printSchemaType
function.
Create a new function called SchemaTypeStr
(or some other name that fits your naming scheme) like this:
const char* SchemaTypeStr(enum SchemaType type) {
switch(type) {
case SCHEMA_INT: return "int"; break;
case SCHEMA_ARRAY: return "array"; break;
case SCHEMA_BOOLEAN: return "boolean"; break;
case SCHEMA_CHAR: return "char"; break;
case SCHEMA_REAL: return "real"; break;
case SCHEMA_RECORD: return "record"; break;
case SCHEMA_STRING: return "string"; break;
default: abort();
}
}
void printSchemaType(FILE* f,SchemaType schemaType) {
fputs(SchemaTypeStr(schemaType), f);
}
Upvotes: 1
Reputation: 359
There doesn't seem to be a portable solution. For linux, you can use fmemopen, but for Windows you'll need to use CreateFile, CreateFileMapping and MapViewOfFile.
See similar question here.
Upvotes: 0