【问题标题】:Writing multiple array pointers to file with ofstream?使用ofstream将多个数组指针写入文件?
【发布时间】:2011-05-20 02:22:50
【问题描述】:

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

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

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

#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;
}

【问题讨论】:

  • 这不是回答你的问题,但我只是想指出,如果你用new[] 初始化一个对象,你应该用delete[] 销毁它。另外,只是一个简单的问题:您是否查看过 test.dat 以查看它是否正确地写出了所有数组?

标签: c++ iostream ifstream ofstream


【解决方案1】:

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

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

【讨论】:

  • 您的意思是它可以在您的计算机上完美执行?你得到的数据和它应该出来的一样吗?
  • 这很奇怪。我刚刚重新启动了我的电脑,并且代码运行良好,正如它应该的那样。抱歉打扰了 :x 不安全的代码确实不安全。
  • 是的,完全正确。我已经在 OS X 和 Linux 上使用 g++ 运行它,它返回的值与写出的值完全相同。所以:检查打开输出文件的错误;检查文件上的修改日期以验证您是否真的在写入;转储文件的内容(在 Linux 上使用 od -f 之类的工具)以查看写入的内容。
  • 由于某种原因,我再次遇到了这个问题,尽管多次重新启动并测试了有效的文件读/写。这个问题还没有答案... :(
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    相关资源
    最近更新 更多