работа с файлами в Си (проблемы с double)

Модератор: Модераторы разделов

Ответить
Аватара пользователя
Dudraug
Сообщения: 313
ОС: Debian lenny/sid

работа с файлами в Си

Сообщение Dudraug »

вот пример тестовой программы

Код: Выделить всё

#include <stdio.h>


int main()
{
    FILE* file;
    int a;
    double b,
           c;
    file = fopen("file", "rt");

    fscanf(file, "%d%f%f", &a, &b , &c);
    printf("%d  %f %f", a, b, c);
    return 0;
}


содержимое файла file

4 3.3 23.3

Код: Выделить всё

[dudraug@localhost iii]$ ./a.out
4  0.000000 0.000000


не понимаю!

меняем double на float и все работает
почему не работает с double
P4-3.0, ASUS P5GD1, 1024MB OЗУ, GeForce 6600GT
Спасибо сказали:
Аватара пользователя
drBatty
Сообщения: 8735
Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
ОС: Slackware-current
Контактная информация:

Re: работа с файлами в Си

Сообщение drBatty »

Dudraug писал(а):
28.12.2007 21:13
меняем double на float и все работает
почему не работает с double

man fscanf
f Matches an optionally signed floating-point number; the next
pointer must be a pointer to float.

e Equivalent to f.

g Equivalent to f.

E Equivalent to f.

a (C99) Equivalent to f.

выходит сканф double вводить не умеет :(
http://emulek.blogspot.ru/ Windows Must Die
Учебник по sed зеркало в github

Скоро придёт
Осень
Спасибо сказали:
Аватара пользователя
dey
Сообщения: 335
ОС: OpenSuse 11.1

Re: работа с файлами в Си

Сообщение dey »

Dudraug писал(а):
28.12.2007 21:13

Код: Выделить всё

[dudraug@localhost iii]$ ./a.out
4  0.000000 0.000000


не понимаю!

меняем double на float и все работает
почему не работает с double


В спецификаторе формата для double надо добавить префикс l, то есть

Код: Выделить всё

  fscanf(file, "%d%lf%lf", &a, &b , &c);
    printf("%d  %f %f", a, b, c);
В сознательных действиях должен присутствовать существенный неалгоритмический компонент.
Roger Penrose,The Emperor's New Mind
Спасибо сказали:
Аватара пользователя
Dudraug
Сообщения: 313
ОС: Debian lenny/sid

Re: работа с файлами в Си

Сообщение Dudraug »

спасибо помогло
P4-3.0, ASUS P5GD1, 1024MB OЗУ, GeForce 6600GT
Спасибо сказали:
Аватара пользователя
drBatty
Сообщения: 8735
Статус: GPG ID: 4DFBD1D6 дом горит, козёл не видит...
ОС: Slackware-current
Контактная информация:

Re: работа с файлами в Си

Сообщение drBatty »

Dudraug писал(а):
28.12.2007 21:50
спасибо помогло
:) А man прочитали?
CONVERSIONS
The following type modifier characters can appear in a conversion spec‐
ification:
l Indicates either that the conversion will be one of diouxX or n
and the next pointer is a pointer to a long int or unsigned long
int (rather than int), or that the conversion will be one of efg
and the next pointer is a pointer to double (rather than float).
Specifying two l characters is equivalent to L. If used with %c
or %s the corresponding parameter is considered as a pointer to
a wide character or wide character string respectively.

L Indicates that the conversion will be either efg and the next
pointer is a pointer to long double or the conversion will be
dioux and the next pointer is a pointer to long long.
http://emulek.blogspot.ru/ Windows Must Die
Учебник по sed зеркало в github

Скоро придёт
Осень
Спасибо сказали:
Ответить