【问题标题】:How do I copy data from a file into an array in C++?如何将文件中的数据复制到 C++ 中的数组中?
【发布时间】:2014-04-24 18:48:07
【问题描述】:

我正在编写一个程序,该程序需要我将一个 128 位的数据块从一个文件读取到一个数组中。我该怎么做?

我一直在使用的代码如下,但我认为它不起作用。

ifstream inFile;
    ofstream outFile;

    inFile.open("datain.txt");
    outFile.open("dataout.txt");

    for (int i=0;i<16;i++)
   {
    inFile >> tempin[i];
    }

    inFile.close();

【问题讨论】:

  • 通常流有一个 read() 函数,您可以在其中准确指定要读入缓冲区的字节数
  • 你不是刚问这个吗?我记得在同一个问题上支持关于以二进制形式阅读的评论。
  • 你能显示示例输入吗?

标签: c++ arrays file-io


【解决方案1】:

您必须将文件读取为字符串,并对其进行解析以将十六进制数字转换为字节。

【讨论】:

    猜你喜欢
    • 2019-05-02
    • 1970-01-01
    • 2013-08-26
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多