0
Hello, I am developing a database queue in C. How can I read entries in a file and thus assign values to some specific variable of my program?
Example: I have a variable int qtd_boxes in main. I need to assign to this variable a value read in a text file that will be informed by the user. (File formatting does not change.)
Input example:
qtde de caixas = 9
delta t = 3
disciplina de escalonamento = {8,6,4,3,2}
//Aqui eu preciso armazenar um char "tipo da conta", um int "numero da
conta" e outro int "numero de operações"
Comum - conta 997002 - 6 operacao(oes)
Prata - conta 520699 - 11 operacao(oes)
Prata - conta 932923 - 2 operacao(oes)
Current Code:
int qtd_caixas, delta_t;
int e1, e2, e3, e4, e5;
char tipo_cliente[10];
int num_conta;
int operacoes;
FILE *arq = fopen("entrada-X.txt", "r+");
if(arq == NULL){
exit(1);
}
Already have several answers talking about the subject here on Sopt. You need to do a file-formatted scan (
fscanf
the function it does). For example, to read from the standard input the format you want, would be something likescanf("%s - conta %d - %d operacao(oes)", tipo_ciente, &num_conta, &operacoes)
; to read from the file pointed byarq
, would befscanf(arq, "%s - conta %d - %d operacao(oes)", tipo_ciente, &num_conta, &operacoes)
– Jefferson Quesado
Thanks friend, I had already succeeded and forgot to delete the question. However, taking advantage of his comment, another question: are 9999 possible input cases, so it is possible to receive an argument on the command line (ex: 0289) and when opening the fopen file ("input-X"), replace the 'X' with this argument? Something like fopen("input-%d", X).
– Pedro Felippe
This Isac response shows you how to do what you want: https://answall.com/a/344246/64969
– Jefferson Quesado