Задание таково:
есть структура из 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
Подскажите пжлст в чем ошибка?что я делаю не так?почему не считывает?