Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 14.11.2011, 18:22   #1
apraxie
 
Регистрация: 05.11.2011
Сообщений: 8
Восклицание Чтение масива структур из текстового файла

Задание таково:
есть структура из 2-х полей:№телефона и комментарий
Создать тел.книгу.
реализовать так же считывание массива структур из текст.файла
Но при считывании с текст.файла считывает непоймешь что!
какието левые числа и строки(
вот код программы:
Код:
#include <stdio.h>
#include <conio.h>
main()
{
      int n,i,j,c,identifier;
      double x,z;
      FILE *file;
      struct book
      {
             double nom;//number phone
             char com[20];//comments
      }
      temp,a[20];
do
{
      system("cls");
      printf("\nCourse engineering:'Phonebook'\n");
      printf("Work has executed:x.x.x.,12-91 group,2011\n\n");
      printf("\nSelect the action:\n");
      printf("Enter   data            ---- '1'\n");
      printf("Output  data            ---- '2'\n");
      printf("Sorting data            ---- '3'\n");
      printf("Write data to the file  ---- '4'\n");
      printf("Read data from file     ---- '5'\n");
      printf("Exit                    ---- '6'\n");
      printf("\n");
      printf("Your choice:");
      scanf("%d",&c);
      if (c==1)
      {
               system("cls");
               printf("Enter Number of records<20:");//number of records
               scanf("%d",&n);
               while (n>20||n<1)
               {
                     printf("Invalid input.Repeat,please:");
                     scanf("%d",&n);
               }
               printf("\n");
               for(i=0;i<n;i++)//input records
               {
                        printf("%d record:\n",i+1);
                        printf("Enter ten-digit phone number:");
                        scanf("%lf",&z);a[i].nom=z;
                        while(a[i].nom<1000000000.0||a[i].nom>9999999999.0)
                        {
                            printf("Invalid input.Repeat,please:");
                            scanf("%lf",&z);
                            a[i].nom=z;
                        }
                        printf("Enter comments (max 20 symbols):");
                        scanf("%s",&a[i].com);
                        printf("\n");
               }
               identifier=1;
      }
      if (c==2)
      {
               system("cls");
               if (identifier!=1)
               {
                   printf("\n<DATA NOT AVIABLE!>\a\n");
                   printf("Please first enter the data - Key '1'.Or read data from file - Key '5'\n");
               }
               else
               {
                   printf("\n'Phonebook':\n");
                   printf("\nNumber phone        |            Comments\n");
                   printf("-----------------------------------------");
                   for(i=0;i<n;i++)
                   {
                        printf("\n");
                        printf("%10.0lf          |%20s",a[i].nom,a[i].com);
                   }
               }
               printf("\nPress 'Enter' to continue...");
               getch();
      }
      if (c==3)
      {
               system("cls");
               if (identifier!=1)
               {
                   printf("\n\n<DATA NOT AVIABLE!>\a\n");
                   printf("Please first enter the data - Key '1'. Or read data from file - Key '5'.\n\n");
                   printf("\n\nPress 'Enter' to continue...");
               }
               else
               {
                   for(i=0;i<n-1;i++)
                      for(j=n-2;j>=i;j--)
                      if (a[j+1].nom<a[j].nom)
                      {
                          temp=a[j];
                          a[j]=a[j+1];
                          a[j+1]=temp;
                      }
               printf("\nSort succeed! \1 \1 \1 \a\n");
               printf("\nPress 'Enter' to continue...");
               }
               getch();
      }
      if (c==4)
      {
               system("cls");
               file=fopen("Rezult.txt","w");
               for(i=0;i<n;i++)
               fprintf(file,"%.0lf \t %s\n",a[i].nom,a[i].com);
               fclose(file);
               printf("\nWrite data to the file was successful!\1 \1 \1 \a\n");
               printf("\n\nPress 'Enter' to continue...");
               getch();
      }
      if (c==5)
      {
               system("cls");
               file=fopen("Rezult.txt","r");
               i=0;
               while(!eof(file))
               {
                  fscanf(file,"%lf%s",&x,&a[i].com);
                  a[i].nom=x;
                  i++;
               }
               fclose(file);
               printf("\n\nReading to a file was successful! \1 \1 \1 \a\n");
               printf("\n\nPress 'Enter' to continue...");
               getch();
               identifier=1;
      }
}
while(c!=6);
}
Пункт чтения массива структур из файла реализуется при с==5
Подскажите пжлст в чем ошибка?что я делаю не так?почему не считывает?
apraxie вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Чтение из текстового файла Form_13 Помощь студентам 0 01.11.2011 03:16
чтение с текстового файла salwator Паскаль, Turbo Pascal, PascalABC.NET 0 11.06.2011 15:49
чтение текстового файла Аглан Microsoft Office Access 2 15.02.2011 22:14
Чтение из текстового файла MooNChilDUA Общие вопросы C/C++ 2 13.01.2011 19:25
Чтение текстового файла Kasper1 Общие вопросы по Java, Java SE, Kotlin 5 17.12.2010 15:57