I need to create a file containing information about my personal book collection. The structure of the record: the book's cipher, author, title, year of publication, location (rack number, etc.).
Moreover, writing (input ()) and reading (output ()) structures are made to / from a file with the extension * .dat. With each new record, we, in fact, add a new structure. So, it does not work out for me to read normally. Displays only the last recorded. It also fails to introduce a new book cipher for the next structure, it just gets(B.key); misses.
#include <stdio.h> #include <conio.h> #include <locale.h> #include <stdlib.h> void input(void); void output(void); struct cd { char key[12]; char author[30]; char title[20]; int year; int location; } B; FILE *f; void main(void) { int n = 0; system("cls"); setlocale(LC_CTYPE, "rus"); while(n != 3) { puts("1. Ввод данных в базу"); puts("2. Вывод всех авторов"); puts("Для выхода из программы нажмите любую другую клавишу..."); puts("\nВаш выбор: "); scanf("%d",&n); fflush(stdin); switch(n) { case 1: input(); break; case 2: system("cls"); output(); break; default: exit(1); } } } void input(void) { setlocale(LC_CTYPE, "rus"); int k = 0; if(( f = fopen("knigolub.dat","w")) == NULL) { puts("Невозможно открыть файл"); exit(1); } while (k != 10) { system("cls"); puts("Введите сведения о книге...\n\n"); printf("Введите шифр книги: "); gets(B.key); printf("Введите автора: "); gets(B.author); printf("Введите название: "); gets(B.title); printf("Введите год издания: "); scanf("%i", &B.year); printf("В какой стеллаж поместить? Введите его номер: "); scanf("%i", &B.location); fwrite(&B, sizeof(B), 1, f); puts("Продолжить работу?[y/n]"); char s; scanf("%s", &s); switch ((int) s) { case (int) 'y': return input(); case (int) 'n': return main(); } } } void output (void) { char letter; setlocale(LC_CTYPE, "rus"); if((f=fopen("knigolub.dat","r")) == NULL) { puts("Невозможно открыть файл"); exit(1); } printf("Шифр книги\tАвтор\tНазвание\tГод издания\tНомер стеллажа\n\n"); while ((letter == fgetc(f)) != EOF) { printf("%s\t%s\t%s\t%i\t%i\t\n", B.key, B.author, B.title, B.year, B.location); } getch(); system("cls"); fclose(f); }
while ((letter == fgetc(f)) != EOF) { printf("%s\t%s\t%s\t%i\t%i\t\n", B.key, B.author, B.title, B.year, B.location);- this is, sorry, what is it? - PinkTuxfgetcinchar- it is already so hard on the teeth that even to mention laziness ...) - PinkTux