sách gpt4 ăn đã đi

Không thể nhập chính xác số chẵn vào chương trình C

In lại Tác giả: Walker 123 更新时间:2023-11-30 15:32:53 27 4
mua khóa gpt4 giày nike

我有一个这样格式化的txt文件:

MyDepartureTown MyDestinationTown 123.45 Vehicle 12

我正在尝试将数据导入到我的 C 程序中。这是我用来实现这一目标的代码:

void import_city_info(Grafo *G)
{
char city_dep[20];
char city_des[20];
double km;
char vehicle[12];
int time;

FILE *data_file = fopen("data/routes.txt", "r");
if (data_file == NULL)
{
fprintf(stderr, "Errore. Impossibile aprire il file.\n");
exit(EXIT_FAILURE);
}

while (5 == fscanf(data_file, "%s %s %f %s %d", &city_dep, &city_des, &km, &vehicle, &time))
{
printf("%2.3f\n", km);
//more code...

}
}

当我尝试打印双数时,它输出0.00。它实际上正确导入了除 double 据之外的所有数据。我做错了什么?

câu trả lời hay nhất

您使用了错误的说明符来读取double。这将调用未定义的行为。您需要%lf

while (5 == fscanf(data_file, "%s %s %lf %s %d", &city_dep, &city_des, &km, &vehicle, &time))
{

关于无法将双数正确导入到 C 程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23938974/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress