使用 ofstream 将多个数组指针写入文件?

发布于 2024-10-05 14:06:58 字数 2170 浏览 3 评论 0原文

我在将多个数据数组写入文件时遇到了一些严重奇怪的问题。基本上,我想将所有数组大小存储在文件顶部,然后存储后面的数组数据。这样我就可以读取大小并使用它来构造数组来保存导入时的数据,并且我将确切地知道每个数组的开始和结束位置。

问题是:我写入了数据,但导入时有所不同。请看一下我的小测试代码。底部有关于值的注释。

非常感谢各位程序员! :)

#include <iostream>
#include <fstream>

int main()
{
    int     jcount = 100, // First item in file
            kcount = 200, 
            in_jcount,    // Third item in file. jcount is used to find where this ends.
            in_kcount;

    float   *j = new float[jcount],
            *k = new float[kcount],
            *in_j,
            *in_k;

    for(int i = 0; i < jcount; ++i) // Write bologna data...
        j[i] = (float)i;
    for(int i = 0; i < kcount; ++i)
        k[i] = (float)i;

    std::ofstream outfile("test.dat");

    outfile.write((char*)&jcount, sizeof(int)); // Good
    outfile.tellp();

    outfile.write((char*)&kcount, sizeof(int)); // Good
    outfile.tellp();

    outfile.write((char*)j, sizeof(float) * jcount); // I don't know if this works!
    outfile.tellp();

    outfile.write((char*)k, sizeof(float) * kcount); // I don't know if this works!
    outfile.tellp();

    outfile.close();


    std::ifstream in("test.dat");

    in.read((char*)&in_jcount, sizeof(int));    // == jcount == 100, good.
    in.read((char*)&in_kcount, sizeof(int));    // == kcount == 200, good.

    in_j = new float[in_jcount],
    in_k = new float[in_kcount];    // Allocate arrays the exact size of what it should be

    in.read((char*)in_j, sizeof(float) * in_jcount);    // This is where it goes bad!
    in.read((char*)in_k, sizeof(float) * in_kcount);

    float   jtest_min = j[0],   // 0.0
            jtest_max = j[jcount - 1],  // this is 99.

            ktest_min = k[0],   // 0.0
            ktest_max = k[kcount - 1],  // this is 200. Why? It should be 199!

            in_jtest_min = in_j[0], // 0.0
            in_jtest_max = in_j[in_jcount - 1], // 99

            in_ktest_min = in_k[0], // 0.0
            in_ktest_max = in_k[in_kcount - 1]; // MIN_FLOAT, should be 199. What is going on here?

    in.close();

    delete k;
    delete j;
    delete in_j;
    delete in_k;
}

I'm having some seriously strange trouble writing multiple arrays of data to a file. Basically, I'm wanting to store all the array sizes at the top of the file, and then the array data following. This way I can just read the sizes and use that to construct arrays to hold the data on import, and I'll know exactly where each array begins and ends.

Here's the problem: I write the data, but it's different on import. Please take a look at my little test code. At the bottom there are comments about the values.

Thank you very much, fellow programmers! :)

#include <iostream>
#include <fstream>

int main()
{
    int     jcount = 100, // First item in file
            kcount = 200, 
            in_jcount,    // Third item in file. jcount is used to find where this ends.
            in_kcount;

    float   *j = new float[jcount],
            *k = new float[kcount],
            *in_j,
            *in_k;

    for(int i = 0; i < jcount; ++i) // Write bologna data...
        j[i] = (float)i;
    for(int i = 0; i < kcount; ++i)
        k[i] = (float)i;

    std::ofstream outfile("test.dat");

    outfile.write((char*)&jcount, sizeof(int)); // Good
    outfile.tellp();

    outfile.write((char*)&kcount, sizeof(int)); // Good
    outfile.tellp();

    outfile.write((char*)j, sizeof(float) * jcount); // I don't know if this works!
    outfile.tellp();

    outfile.write((char*)k, sizeof(float) * kcount); // I don't know if this works!
    outfile.tellp();

    outfile.close();


    std::ifstream in("test.dat");

    in.read((char*)&in_jcount, sizeof(int));    // == jcount == 100, good.
    in.read((char*)&in_kcount, sizeof(int));    // == kcount == 200, good.

    in_j = new float[in_jcount],
    in_k = new float[in_kcount];    // Allocate arrays the exact size of what it should be

    in.read((char*)in_j, sizeof(float) * in_jcount);    // This is where it goes bad!
    in.read((char*)in_k, sizeof(float) * in_kcount);

    float   jtest_min = j[0],   // 0.0
            jtest_max = j[jcount - 1],  // this is 99.

            ktest_min = k[0],   // 0.0
            ktest_max = k[kcount - 1],  // this is 200. Why? It should be 199!

            in_jtest_min = in_j[0], // 0.0
            in_jtest_max = in_j[in_jcount - 1], // 99

            in_ktest_min = in_k[0], // 0.0
            in_ktest_max = in_k[in_kcount - 1]; // MIN_FLOAT, should be 199. What is going on here?

    in.close();

    delete k;
    delete j;
    delete in_j;
    delete in_k;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-10-12 14:06:58

这段代码没有任何明显的错误(事实上,我没有看到当我尝试运行它时遇到的错误),除了您没有检查打开输入/输出文件的错误这一事实。

例如,如果您没有写入“test.dat”的权限,则打开将默默失败,并且您将读回文件中之前发生的任何内容。

There's nothing obviously wrong with this code (indeed, I don't see the errors you're encountering when I try running it), except for the fact that you are not checking for errors opening the input/output files.

For example, if you don't have permission to write to "test.dat", the open will silently fail, and you'll read back in whatever happened to be in the file before.

我要还你自由 2024-10-12 14:06:58

我有同样的错误,我通过使用二进制文件修复它:

ofstream outfile;
outfile.open ("test.dat", ios::out | ios::binary);

ifstream in;
in.open ("test.dat", ios::in | ios::binary);

I've got the same bug, I fix it by using binary file:

ofstream outfile;
outfile.open ("test.dat", ios::out | ios::binary);

and

ifstream in;
in.open ("test.dat", ios::in | ios::binary);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文