1
I’m doing a project for the college, the code already reads the files and divides the lines taking into account the ";", how can I save these tokens in variables?
struct informacaoFicheiroInput{
int id;
int acompanhantes;
char tipo[11];
int entrada;
int saida;
int servico;
};
void lerFicheiroInput(){
struct informacaoFicheiroInput informacao[20];
FILE* file;
file = fopen("input.txt","r");
if(file == NULL){
printf("Não foi possivel abrir o arquivo.\n");
}
char line[20], *token, dados[11][20];
while(fgets(line, 100, file) != NULL){
int count = 0;
token = strtok(line,";");
while(token != NULL) {
dados[count++] = token;
token = strtok(NULL, ";");
}
}
}
The input file is of the genus:
10 ; Visitante ; 10 ; 19 ; 2
2 ; 1 ; Funcionario ; 8 ; 0
3 ; 2 ; Diretor ; 12 ; 19
4 ; Visitante ; 8 ; 0 ; 3
You can do
int inteiro = atoi(dados[x][y]);
to obtain the integers, andstrcpy(string, dados[x][y]);
to copy strings. In the input file, delimiters were missing at the end of the line,10; Visitante; 10; 19; 2;<quebra>
– Marcelo Shiniti Uchimura
I’ll try, what does the atoi function? I can’t change the input file, how do I?
– Bruno Bacelar
int atoi(const char* s)
takes a string as parameter, tries to convert it to integer, and returns this integer value.– Marcelo Shiniti Uchimura
Thank you, I’m clear!
– Bruno Bacelar
It may be necessary to include line breaking as a delimiter, in the
strok
:token = strtok(line, ";\n");
– Marcelo Shiniti Uchimura
I could not use int integer = atoi(data[x][y]); to get integers, and strcpy(string, data[x][y]), you can enter this into code to see if it works?
– Bruno Bacelar
What each element of a file line means?
– Marcelo Shiniti Uchimura
If the type of employee is employee or director, the line will be of this genus 2 ; 1 ; Employee ; 8 ; 0, ie by order, id, companions, type, hours of entry, hours of exit If the type of employee is visitor, the line will be of type 4 ; Visitor ; 8 ; 0 ; 3, that is, by order, id, type, check-in time, check-in time, and employee id number you will visit
– Bruno Bacelar
Are the spaces between data, shown in your file example, really there? Or were they included as readability?
– Marcelo Shiniti Uchimura
The spaces before and after the ";" such as line breaks at the end of each sentence are there
– Bruno Bacelar