当我从文件中读取数据时,结果均为0.00 [英] When I read data from file, the results are all 0.00

查看:555
本文介绍了当我从文件中读取数据时,结果均为0.00的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码很简单.但是,我不知道我的代码有什么问题.即使花费了3个小时,我也无法得知此错误.这是我的问题.
随机值保存在文件中.当我从文件中读取这些数据时,结果均为0.00" .我的代码有什么问题?在此先感谢您的帮助和时间.

My code is very simple one. But, I don''t know what''s wrong with my code. I can''t know this error even after spending 3 hours. Here is my problem.
"Random value are saved in file. When I read these data from file, the results are all 0.00". What''s wrong with my code? Thanks in advanced for your help and time.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define DATALENGTH 10

void main()
{
    FILE* fp;
    int err_no = fopen_s(&fp, "C:\\Tests\\test.dat", "wb");
    if (err_no != 0) {
        printf("file open error.");
        _getch();
    }

    double data = 0.00;
    for (int i = 0; i < DATALENGTH; i++) {
        data = rand()%100;
        fprintf(fp, "%lf", data);
    }

    fclose(fp);

    FILE* fp_read;
    err_no = fopen_s(&fp_read, "C:\\Tests\\test.dat", "r");
    if (err_no != 0) {
        printf("file read error.");
        _getch();
    }

    double* buffer = (double*)malloc(sizeof(double) * DATALENGTH);
    int data_len = fread(buffer, sizeof(double), DATALENGTH, fp_read);
    if (data_len != DATALENGTH) {
        printf("File read failed.");
        getch();
    }
    fclose(fp_read);

    for (int j = 0; j < DATALENGTH; j++) {
        printf("buffer[%d] == %.2lf\n ", j, buffer[j]);
    }
    _getch();
}

推荐答案

看看文件中的数据-这与您想的不一样.
您将得到的是数字的字符串表示形式,其精度为6位数字,其长度可能会或可能不会变化,具体取决于您首次生成的随机数中的位数(0-9可能会给您一个较短的值,即10 -99)

然后,您将其读回为二进制数据,并尝试将其解释为double值.
不,它不会给您您所期望的.甚至没有关闭.

相反,请尝试使用数据终止符(用逗号或换行符)编写它,然后将其读回为字符串,然后将其转换为double.
或将原始数字转换为双精度数字,然后将其另存为二进制数据.

您的选择.
Have a look at your data in the file - it is not what you think it is.
What you will have is a string representation of the numbers with 6 digits of precision, that may or may not vary in length depending on the number of digits in the random number you first generated (0-9 may give you a shorter value that 10-99)

You then read this back as binary data and try to interpret it as double values.
No. It''s not going to give you what you expect. Not even close.

Instead, try writing it with a data terminator (comma say, or new line) and reading it back as a string then converting it to a double.
Or convert the original number to a double, and save that as binary data.

Your choice.


此代码没有任何意义:仅查看输出文件中写入的内容.您已经以人类可读的字符串表示形式编写了一些浮点数,而值之间没有任何定界符.由于格式的不便,某些记录"占用9个字节,而另一些占用8个字节.您不应该首先写它-用途是什么?

然后打开文件进行读取,并尝试将其作为双精度浮点数数组(每个8字节)读取.您使用一种没有实际意义的格式进行书写,因为它不允许进行明确的解释(由于记录之间没有固定的边界),并且以完全不相关的格式进行读取.这只是一团糟.

没有解决办法,因为这段代码显然没有功能目的,因此我假设目的是学习如何呈现,读取和写入数据.可以通过不同的方式来完成,您将需要学习它们.首先,您需要以多种不同的方式写入数据并查看其外观.作为初学者,我建议您同时使用文本编辑器和二进制编辑器,并检查数据的布局方式.当您掌握正在发生的事情时,请考虑阅读它.

一些建议:不要使用过时的getch,请使用_getch.永远不要使用硬编码的绝对文件路径,即使在研究/研究代码中也要使用相对路径;在您的情况下,它可能只是不带目录的文件名,因此实际位置将由当前工作目录定义.请记住,在最终产品中,没有可以使用硬编码路径的情况.切勿重复任何数据;您已经编写了两次"C:\\ Tests \\ test.dat",这是完全不支持的.将其定义为一个显式常量,仅一次.请参阅: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [
This code makes no sense: just look what is written in the output file. You have written some floating-point numbers in human-readable string representation, without any delimiters between the values; due to casualty of the format, some "records" take 9 bytes, some other take 8. You should not have written it in first place — what''s the use?

Then you open the file for reading and try to read it as an array of double-precision floating-point numbers, 8 bytes each. You write in one format, which makes no practical sense, because it does not allow for unambiguous interpretation (due to lack of fixed boundaries between records), and read in totally unrelated format. This is nothing but a mess.

There is no a fix, because apparently there is no functional purpose in this code, so I assume the purpose is learning how data is presented, read and written. It can be done in different ways, and you will need to learn them all. First, you need to write data in many different ways and see how it looks. As you are a beginner, I would advise you use both text and binary editors and examine how the data is laid out. When you get a grip on what''s going on, think about reading it.

Some side advice: don''t use obsolete getch, use _getch. Never use hard-coded absolute file path, even in the research/study code, use relative; in your case, it could be just the file name without directory, so the actual location will be defined by the current working directory. Remember, in the final product, there are no situations when a hard-coded path can be used. Never repeat any data; you have written "C:\\Tests\\test.dat" twice, which is totally unsupportable. Define it as an explicit constant, only once. Please see: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^]. Failing to follow this very basic principle is the invitation for all kinds of trouble.

By the way, how about writing at least a bit in C++, not C, once your file is *.cpp? Avoid #define, use const int length = 10; instead. But if you want to lean C, as opposed to C++, create a *.C file.

—SA


编写或读取.dat文件没有错.唯一出错的是如何在控制台中显示它

试试这个

There is nothing wrong in writing or reading the .dat file. The only thing going wrong is how you show it in console

try this out

for (int j = 0; j < sizeof(double)*10; j++)
 {
        //printf("buffer[%d] == %.2lf\n ", j, buffer[j]);
        printf("%c", ((char *)buffer)[j]);
}





我得到的输出是





And the out put I got is

41.00000067.00000034.0000000.00000069.00000024.00000078.00000058.00000062.000000


您可以设置格式以在每行之间留出空间,也可以换行


And you can format this to have space between each or can go for new line


这篇关于当我从文件中读取数据时,结果均为0.00的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆