Я пытаюсь создать простую (не совсем) программу на C, которая использует файлы для генерации случайного списка ингредиентов, которые в будущем станут рецептами.Я использую текстовые и двоичные файлы с помощью всех связанных функций: getc (), fwrite (), fread (), ...
Все, что я пробовал, показано в кодедве функции:
- int convert_txt_to_bin ()
- int print_ingredient_file (путь к file_path)
Обещаю, они не такие сложные.Также извините за то, что закрыто с перерывом:)
ингредиент
typedef struct {
char name[32]; // The name of the ingredient: "Mela", "Fusilli", "Passata di pomodoro";
int type;
} ingredient;
int convert_txt_to_bin()
{
FILE *txt, *bin;
ingredient tmp;
int i = 0, records_written = 0;
char c;
tmp.type = 0;
txt = fopen(file_list_fruits_txt, "r");
bin = fopen(file_list_fruits_bin, "wb");
if (txt && bin)
{
while (1)
{
tmp.name[i] = getc(txt);
c = tmp.name[i];
if (tmp.name[i] == '\n' || tmp.name[i] == EOF)
{
tmp.name[i] = '\0';
fwrite(&tmp, sizeof(ingredient), 1, bin);
while (i >= 0) tmp.name[i--] = '#';
records_written++;
i++;
printf("Record written successfully\n");
if (c == EOF) break;
}
else i++;
}
fclose(txt);
fclose(bin);
printf(".bin file written correctly\n%d records saved\n", records_written);
return records_written;
}
else
{
printf("ERROR: can't open file. Quitting...");
system("PAUSE");
fclose(txt);
fclose(bin);
exit(1);
}
}
int print_ingredient_file(path file_path)
{
FILE* bin;
ingredient tmp;
int i = 0;
bin = fopen(file_path, "rb");
while (fread(&tmp, sizeof(ingredient), 1, bin))
printf("Ingredient number #%d\n\tName = %s\n\tType = %d\n", i++, tmp.name, tmp.type);
fclose(bin);
}
fruits.txt (it's in italian)
Mela
Banana
Pera
Kiwi
Pesca
Albicocca
Mandarino
Uva
Ciliegia
Lampone
Fragola
Mora
Mirtillo
Fico
Nespola
Caco
Anguria
Melone
Melograno
Arancia
Mango
fruits.bin
19 из 21 плодов успешно записаны.Я не понимаю, почему Pera (груша) и Banana (угадайте, что) не сохраняются правильно и дают проблемы с выводом.
Вывод на стандартный вывод:
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
Record written successfully
.bin file written correctly
19 records saved
Ingredient number #0 <------------------------------ Perana ???
Perana Name = Mela
Type = 0
Ingredient number #1
Name = Kiwi
Type = 0
Ingredient number #2
Name = Pesca
Type = 0
Ingredient number #3
Name = Albicocca
Type = 0
Ingredient number #4
Name = Mandarino
Type = 0
Ingredient number #5
Name = Uva
Type = 0
Ingredient number #6
Name = Ciliegia
Type = 0
Ingredient number #7
Name = Lampone
Type = 0
Ingredient number #8
Name = Fragola
Type = 0
Ingredient number #9
Name = Mora
Type = 0
Ingredient number #10
Name = Mirtillo
Type = 0
Ingredient number #11
Name = Fico
Type = 0
Ingredient number #12
Name = Nespola
Type = 0
Ingredient number #13
Name = Caco
Type = 0
Ingredient number #14
Name = Anguria
Type = 0
Ingredient number #15
Name = Melone
Type = 0
Ingredient number #16
Name = Melograno
Type = 0
Ingredient number #17
Name = Arancia
Type = 0
Ingredient number #18
Name = Mango
Type = 0
Premere un tasto per continuare . . .