Untitled

 avatar
unknown
plain_text
2 years ago
2.1 kB
12
Indexable
void escrever_especif_com_reg()
{
	/*
		Get the txt file from the user using the pedir_ficheiro function
	*/
	char ficheiro_limp[50];
	pedir_ficheiro("ruas.txt", ficheiro_limp, 1, 1);
	FILE *fl = fopen(ficheiro_limp, "r");

	/*
		get the dat file from the user using the pedir_ficheiro function
	*/
	char ficheiro_dat[50];
	pedir_ficheiro("registos.dat", ficheiro_dat, 4, 1);
	FILE *fd = fopen(ficheiro_dat, "rb");

	/*
		Create and load the struct with all the data inside the dat file
	*/
	struct Arguments database[MAX_NUM_RECORDS];
	read_database(ficheiro_dat, database);

	/*
		Create the varariables needed to run the rest of the function
			One to store the line read from the txt file: line
			One to store the line number: line_number
			One to store the word inputed by the user: word
	*/
	char line[100];
    int line_number = 1;
	char word [100];

	/*
		Get the word we want to look for from the user
	*/
	printf("\033[1;34mEscreva um nome (ou varios) para procura nas arterias: \033[0m");
	fgets(word, sizeof(word), stdin);
	word[strcspn(word, "\n")] = '\0';

	printf("\n");
	/*
		Loop to every line of the txt file until the end (at the end fgets returns NULL)
	*/
    while (fgets(line, sizeof(line), fl) != NULL) 
	{	
		//replaces the \n at the end of the array with a \0 so we can work with it as a string 
		int length = strlen(line);
        if (length > 0 && line[length - 1] == '\n') 
		{
            line[length - 1] = '\0';
        }

		/*
			If the string contains the word chosen, print the line itself and its arguemnts that are != 0
		*/
        if (strstr(line, word) != NULL) 
            {
                printf("\t%-50s - %d ", line, line_number);

                for (int i = 0; i < 6; i++)
                {
					if (database[line_number-1].registo_int[i] == 0)
					{
						continue;
					}
                    printf("- %d %c ", database[line_number-1].registo_int[i], database[line_number-1].registo_ch[i]);
                }

                printf("\n");
            }
		line_number++;
    }
	fclose(fl);
	fclose(fd);
}
Editor is loading...
Leave a Comment