Найти ошибку в функции fprintf. не могу разобраться с форматом double - C (СИ)
Формулировка задачи:
есть структура:
есть функция сейв которая не работает корректно с остатком (дабл) все значения записывает а дабл записывает в файл в виде 0,00000
вот что получаю в файле:
вот вся программа (мб там что-то с чем-то связано) :
Листинг программы
- struct Abonent
- {
- char nomer[11];
- double ostatok;
- char tarif[15];
- }arr [5];
Листинг программы
- int save (struct Abonent *arr)
- {
- int i=0;
- FILE *f=NULL;
- f=fopen ("D:\Result.txt", "wt");
- if (f==NULL)
- {
- printf ("No file!!!\n");
- exit (0);
- }
- fprintf (f, "Spisok abonentov\n");
- for(i=0;i<5;i++)
- {
- fprintf(f,"%s\t", &arr[i].nomer);
- fprintf(f,"%lf\t", &arr[i].ostatok);
- fprintf(f,"%s\t", &arr[i].tarif);
- fprintf(f,"\n");
- }
- fclose (f);
- return 0;
- }
Spisok abonentov
09342598623 0.000000 life
05085458748 0.000000 mtc
09765874598 0.000000 kyivstar
09345874122 0.000000 life
09645874114 0.000000 mtc
Листинг программы
- #include <conio.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Abonent
- {
- char nomer[11];
- double ostatok;
- char tarif[15];
- }arr [5];
- int load (struct Abonent *arr)
- {
- int i=0;
- FILE *f=NULL;
- f=fopen("D:\Input.txt", "rt");
- if (f==NULL)
- {
- printf ("No file!!!\n");
- exit (0);
- }
- for(i=0;i<5;i++)
- {
- fscanf(f,"%s", &arr[i].nomer);
- fscanf(f,"%lf", &arr[i].ostatok);
- fscanf(f,"%s", &arr[i].tarif);
- }
- fclose (f);
- return 0;
- }
- int sortost (struct Abonent *t)
- {
- struct Abonent x;
- int i=0, j=0;
- for(i=0;i<5;i++)
- {
- for(j=0; j<4; j++)
- {
- if( t[j].ostatok > t[j+1].ostatok )
- {
- x=t[j];
- t[j]=t[j+1];
- t[j+1]=x;
- }
- }
- }
- return 0;
- }
- int sorttar (struct Abonent *arr)
- {
- int i=0, j=0;
- struct Abonent temp;
- for (i=0; i<5; i++)
- {
- for (j=0; j<4; j++)
- {
- if (strcmp(arr[j].tarif, arr [j+1].tarif) >= 0)
- {
- temp=arr[j];
- arr[j]=arr[j+1];
- arr[j+1]=temp;
- }
- }
- }
- return 0;
- }
- int save (struct Abonent *arr)
- {
- int i=0;
- FILE *f=NULL;
- f=fopen ("D:\Result.txt", "wt");
- if (f==NULL)
- {
- printf ("No file!!!\n");
- exit (0);
- }
- fprintf (f, "Spisok abonentov\n");
- for(i=0;i<5;i++)
- {
- fprintf(f,"%s\t", &arr[i].nomer);
- fprintf(f,"%lf\t", &arr[i].ostatok);
- fprintf(f,"%s\t", &arr[i].tarif);
- fprintf(f,"\n");
- }
- fclose (f);
- return 0;
- }
- void main (void)
- {
- int n=0, i=0;
- L: printf("1.load\n2.sortorovka po osratky\n3.sortirovka po tarify\n4.save\n5.exit\n");
- printf ("Input number : ");
- scanf ("%d", &n);
- printf ("\n");
- switch (n)
- {
- case 1 : load(arr);
- break;
- case 2 : sortost (arr);
- break;
- case 3 : sorttar (arr);
- break;
- case 4 : save (arr);
- break;
- case 5 : exit (0);
- break;
- }
- for (i=0;i<5;i++)
- printf("%s %2.2lf %s\n",arr[i].nomer,arr[i].ostatok,arr[i].tarif);
- printf("\n");
- goto L;
- }
Решение задачи: «Найти ошибку в функции fprintf. не могу разобраться с форматом double»
textual
Листинг программы
- fprintf(f, "%f\t", arr[i].ostatok);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д