Quantcast
Channel: Форум программистов и сисадминов Киберфорум
Viewing all articles
Browse latest Browse all 517273

Чтение структуры из файла - C для начинающих

$
0
0
Добрый вечер!
Курсач горит - все готово, но при чтении структуры из файла - fread выдает ноль
Уже что только не делал - что только не предпринимал - никак не помогает
Если поможете - буду очень вам благодарен!

ЗАГОЛОВОК
:

#ifndef STUDLIST_H
#define STUDLIST_H

#include <stdbool.h>  // Библиотека boolean

typedef struct {
        char name[255];          // Формат: Иванов И.И.
    bool geschlecht;  // Пол (0 - мужской, 1 - женский)
    unsigned int schule;  // Школа (Диаползон номеров: [1-999999999], 0 - без школы (например дом. обучекние)
    bool medaille;    // Есть ли медаль (1 - есть, 0 - отсутствует)
    char note;            // Оценка (Диапозон [0-250], 251 - нет оценки
    bool arechnung;      // Зачет по сочинению (1 - да, 0 - нет)

} studlist;

#endif // STUDLIST_H

Writer
:

#pragma hdrstop
#pragma argsused

#include <stdio.h>
#include <stdlib.h>
#include "studlist.h" //Структура

#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif

bool writeperson(studlist *stud_TStringList) {
        // Добавляем имя
        bool good;
        unsigned int count = 0;
        char temp = '1';

        good = false;
        while (good == false) {
                count = 0;
                temp = '1';

                printf("Vvedite name - format: Slim.R.G.\n");
                stud_TStringList->name[count]= getchar();
                while ((stud_TStringList->name[count] != '\n') && (count < 255)){
                count++;
                stud_TStringList->name[count]= getchar();
                }
                stud_TStringList->name[count]= NULL;
        count=0;

                // Добавляем пол
                printf("VVedite pol (0 - man, 1 - woman)\n");
                scanf("%u", &count);
                if (count == 1) {
                        stud_TStringList->geschlecht = true;
                }
                else {
                        stud_TStringList->geschlecht = false;
                }

                // Вводим номер школы
                printf("Vvedite nomer schule, esli net - 0\n");
                scanf("%u", &count);
                stud_TStringList->schule = count;

                // Проверка на медаль
                printf("Est li medal? (1- yes, 0 - no)\n");
                scanf("%u", &count);
                if (count == 1) {
                        stud_TStringList->medaille = true;
                }
                else {
                        stud_TStringList->medaille = false;
                }

                // Вводим оценку
                printf("Vvedite note \n");
                scanf("%u", &count);
                stud_TStringList->note = count;

                // Проверка на зачет
                printf("Est li zachet? (1 - yes, 0 - no)\n");
                scanf("%u", &count);
                if (count == 1) {
                        stud_TStringList->arechnung = true;
                }
                else {
                        stud_TStringList->arechnung = false;
                }

                // Запрос на правильность

                system("cls"); // FOR WINDOWS ONLY
                // system("clear"); //FOR LINUX ONLY

                printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
                printf("Proverte pravilnost'\n");
                printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
                printf("Name: ");
                printf(stud_TStringList->name);
                printf("\nPol: ");
                if (stud_TStringList->geschlecht == true) {
                        printf("woman\n");
                }
                else {
                        printf("man\n");
                }
                printf("School: ");
                printf("%u", stud_TStringList->schule);
                printf("\nMedal: ");
                if (stud_TStringList->medaille == true) {
                        printf("est'");
                }
                else {
                        printf("net");
                }
                printf("\nOchenka: ");
                printf("%u", stud_TStringList->note);
                printf("\nZachet: ");
                if (stud_TStringList->arechnung == true) {
                        printf("est'\n");
                }
                else {
                        printf("net\n");
                }

                printf("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
                printf("Vse verno? (1 - yes, 0 - no)\n");
                scanf("%u", &count);
                if (count == 1) {
                        good = true;
                }
                temp = '1';
                while ((temp != EOF) && (temp != '\n')) {
                        scanf("%c", &temp);
                };
        }

        // Добавить еще пользователя
        printf("dobavit echo studentov? (1 - yes, 0 - no)\n");
        scanf("%u", &count);
        temp = '1';
        while ((temp != EOF) && (temp != '\n')) {
                scanf("%c", &temp);
        };

        if (count == 1) {
                return true;
        }
        else {
                return false;
        }

}

int _tmain(int argc, _TCHAR* argv[]) {
        char *adress;
        unsigned long long int count;
        char  temp;
        FILE *output;
        studlist stud_TStringList;
        //Ввод адресса файла сохранения
        printf("Enter the file address\n");
        adress = NULL;
        count = 0;
        temp = '1';

        while ((temp != EOF)&&(temp != '\n')){
          adress = (char*)realloc(adress, ((count+1)*sizeof(char)));
          temp = getchar();
          if ((temp != EOF)&&(temp != '\n')){
                  adress[count]=temp;
                  count++;
                  if (count >= 184467440737095){
                          printf("Previsinaa max dlina");
                          return 60;
                  }

          }
        }
  adress[count]=NULL;


  // Открываем файл
  output = fopen(adress, "w");
  if (!output) {
          printf("Can't open file");
          return 404;
        }

  //Заливаем данные

  while (writeperson(&stud_TStringList) == true){
        fwrite(&stud_TStringList, sizeof(studlist), 1, output);
  }
  fwrite(&stud_TStringList, sizeof(studlist), 1, output);
  fclose(output);
        system("pause");
        return 0;
}

Reader
:

#pragma hdrstop
#pragma argsused

#include <stdio.h>
#include <stdlib.h>
#include "studlist.h"

#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif

int _tmain(int argc, _TCHAR* argv[]) {
        char oc;
        char *adress;
        unsigned long long int count;
        char temp;
        FILE *input;
        studlist stud_TStringList;
        // Ввод адресса файла сохранения
        printf("Enter the file address\n");
        adress = NULL;
        count = 0;
        temp = '1';

        while ((temp != EOF) && (temp != '\n')) {
                adress = (char*)realloc(adress, ((count + 1)*sizeof(char)));
                temp = getchar();
                if ((temp != EOF) && (temp != '\n')) {
                        adress[count] = temp;
                        count++;
                        if (count >= 1844674) {
                                printf("Previsinaa max dlina");
                                return 60;
                        }

                }
        }
        adress[count] = NULL;
        count = 0;
        // Открываем файл
        input = fopen(adress, "r");
        if (!input) {
                printf("Can't open file");
                return 404;
        }

        printf("Vvedite minimalnuu udovletv. ochenku\n");
        scanf("%d", &oc);
        printf("%d", (fread(&stud_TStringList, sizeof(studlist), 1, input)));
        while (fread(&stud_TStringList, sizeof(studlist), 1, input) == 1) {
                // if (stud_TStringList.note < oc) {
                // printf("=====================\n");
                //printf("%s", stud_TStringList.name);
                // printf("%s,", '\n');
                system("pause");
                // }
        }

        system("pause");
        return 0;
}

Добавлено через 12 минут
Судя по отладке - записывает верно, а вот с чтением бяда

Viewing all articles
Browse latest Browse all 517273

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>