1
I have a file that contains calls to functions present in my code. Some examples of functions are criar(), inserir(elemento, conjunto), listar(conjunto)
where the arguments are integers passed to the function. These functions must be called through the arquivo.txt
that will contain, for example:
criar()
inserir(1,0)
listar(0)
To read the file, I used the following function:
void leArquivo(){
FILE *arq;
if((arq = fopen("arquivo.txt", "r")) == NULL)
printf("Erro ao abrir o arquivo.\n");
char buffer[100];
char *p;
while (fscanf (arq, "%s\n", buffer) != EOF) {
p = buffer;
printf("%s\n", p);
}
fclose (arq);
}
However, this only prints the "name" of the function itself, and does not call it. For example, if in the file the function is criar()
, he printa criar()
and not the return of the create function. I found that, as printf("%d", criar());
returns the function value, printf("%d\n", p);
would return the same, but not.
I thought I’d use the function strcmp()
to be able to compare if the names are equal and hence call the function, and I succeeded in functions without arguments, but I do not know how to do for functions like inserir(elemento, conjunto)
, where it is not possible to "predict" the arguments. Any suggestions on how to call these functions from reading the file?
Show your code in a way that can be analyzed better what you are doing. Leave it in a state that can be compiled and executed as far as you can. But I can already say that you are far from the result. You will have to analyze the string and decide what to do. That is, you will have to do some form of parser. It’s not that simple.
– Maniero
It’s just that I thought what the functions themselves did would make no difference, since they all return only integers that must be displayed. The problem is actually calling them from the file. :(
– Moni
The idea of strcmp is the most common way for these things, if the language of functions is not too complex. Compares the string to an if (or switch) and true case calls the function (doing this one by one). What can help is to change the syntax, leaving the functions of the same size and with parameters in the same order (but there goes the concrete case).
– Bacco