Update cv6/a_station.c
This commit is contained in:
		
							parent
							
								
									3e16b996d8
								
							
						
					
					
						commit
						360573e904
					
				@ -112,32 +112,15 @@ int count_capacity(struct station* station) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Функція для завантаження бази даних з текстового файлу
 | 
					// Функція для завантаження бази даних з текстового файлу
 | 
				
			||||||
int load_from_file(struct station* station, const char* filename) {
 | 
					void save_to_file(struct station* moja_stanica) {
 | 
				
			||||||
    FILE* file = fopen(filename, "r");
 | 
					    FILE *file = fopen("data.txt", "w");
 | 
				
			||||||
    if (file == NULL) {
 | 
					    if (file == NULL) {
 | 
				
			||||||
        return -1; // Не вдалося відкрити файл
 | 
					        printf("Nepodarilo sa otvoriť súbor na zápis.\n");
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    char target[TARGET_SIZE];
 | 
					    for (int i = 0; i < moja_stanica->track_count; i++) {
 | 
				
			||||||
    int capacity;
 | 
					        struct car* current = moja_stanica->tracks[i];
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    while (fscanf(file, "%s %d", target, &capacity) == 2) {
 | 
					 | 
				
			||||||
        add_target_capacity(station, target, capacity);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fclose(file);
 | 
					 | 
				
			||||||
    return 0; // Успішне завантаження
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Функція для збереження бази даних у текстовий файл
 | 
					 | 
				
			||||||
int save_to_file(struct station* station, const char* filename) {
 | 
					 | 
				
			||||||
    FILE* file = fopen(filename, "w");
 | 
					 | 
				
			||||||
    if (file == NULL) {
 | 
					 | 
				
			||||||
        return -1; // Не вдалося відкрити файл
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (int i = 0; i < station->track_count; i++) {
 | 
					 | 
				
			||||||
        struct car* current = station->tracks[i];
 | 
					 | 
				
			||||||
        while (current != NULL) {
 | 
					        while (current != NULL) {
 | 
				
			||||||
            fprintf(file, "%s %d\n", current->value, current->capacity);
 | 
					            fprintf(file, "%s %d\n", current->value, current->capacity);
 | 
				
			||||||
            current = current->next;
 | 
					            current = current->next;
 | 
				
			||||||
@ -145,6 +128,24 @@ int save_to_file(struct station* station, const char* filename) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fclose(file);
 | 
					    fclose(file);
 | 
				
			||||||
    return 0; // Успішне збереження
 | 
					    printf("Údaje boli úspešne uložené do data.txt.\n");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Funkcia pre načítanie údajov zo súboru
 | 
				
			||||||
 | 
					void load_from_file(struct station* moja_stanica) {
 | 
				
			||||||
 | 
					    FILE *file = fopen("data.txt", "r");
 | 
				
			||||||
 | 
					    if (file == NULL) {
 | 
				
			||||||
 | 
					        printf("Súbor data.txt nebol nájdený. Začíname s prázdnou stanicou.\n");
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    char target[TARGET_SIZE];
 | 
				
			||||||
 | 
					    int capacity;
 | 
				
			||||||
 | 
					    while (fscanf(file, "%s %d", target, &capacity) != EOF) {
 | 
				
			||||||
 | 
					        add_target_capacity(moja_stanica, target, capacity);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fclose(file);
 | 
				
			||||||
 | 
					    printf("Údaje boli úspešne načítané zo súboru data.txt.\n");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user