使用 Ifstream 将文本文件中的数字添加到 C 样式数组

发布于 2024-11-02 17:06:27 字数 486 浏览 4 评论 0原文

我对上面的标题有疑问。我已尝试以下操作,但它没有以正确的行为起作用。

void ArrayIntStorage::read(ifstream& in)
{
    if(in.is_open())
    {
        while(!in.eof())
        {
            in.getline(arrayStorage, sizeof(in));
        }
    }
}

ofstream& ArrayIntStorage::write(ofstream &out) const
{
    for (int i = 0; i < sizeof arrayStorage; i++)
    {
        out << arrayStorage[i] << endl;
    }
    return out;
}

我是 C++ 编程的新手,所以我可能做了一些愚蠢的事情。提前致谢。

I am having trouble with the title above. I have tried the following but it does not act in the correct behaviour.

void ArrayIntStorage::read(ifstream& in)
{
    if(in.is_open())
    {
        while(!in.eof())
        {
            in.getline(arrayStorage, sizeof(in));
        }
    }
}

ofstream& ArrayIntStorage::write(ofstream &out) const
{
    for (int i = 0; i < sizeof arrayStorage; i++)
    {
        out << arrayStorage[i] << endl;
    }
    return out;
}

I am a novice at C++ programming, so I am probably doing something stupid. Thanks in advance.

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

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

发布评论

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

评论(2

思慕 2024-11-09 17:06:27

我猜你正在读/写一个整数数组。看来您可能错误地读取了 read 方法中的 sizeof(in),而不是 sizeof(arrayStorage)。

I am going to guess that you are reading/writing an array of integers. It appears that you might be incorrectly reading the sizeof(in), rather than the sizeof(arrayStorage) in your read method.

一笔一画续写前缘 2024-11-09 17:06:27

您的代码存在几个问题。首先,你
不向我们展示 arrayStorage 的声明,所以我们必须
猜猜看:我猜 int arrayStorage[N],(其中 N 是一些
持续的)。接下来,阅读:

  • 在循环中使用“istream::eof”永远是不正确的。这
    函数只有在输入失败后才有意义;前
    也就是说,当没有其他事情可做时,它可以返回“false”
    read 和“true”,即使最后一次输入成功。这
    您感兴趣的函数是“fail”,这就是
    每当使用“istream”时都会间接调用
    需要“bool”的上下文。
  • `getline` 需要一个 `char[]`,并且读取字符,而不是
    int 的。

如果文件包含的所有内容都是这些 int,则标准习惯用法
阅读它们的方法是:

for (int i = 0; in && i < N; ++ i) {
    in >> arrayStorage[i];
}

在实践中,您可能需要更多:如果
该文件没有包含足够的 int?如果它包含太多?
并且您需要在失败之后验证失败
是由于文件结尾,而不是文件中的一些随机乱码
文件。或者也许您想要(或需要)更多地验证格式,
确保一行中不超过一个 int。

你的输出函数看起来几乎没问题;最后一个索引应该是N,而不是sizeof arrayStorage(它返回数组中的字节数,而不是元素数)。后来,下游,
您需要检查之后是否有错误
关闭文件;没有什么比这更令人沮丧的了
一个程序说它已经成功,但实际上只是其中的一部分
数据已存入磁盘。

There are several problems with your code. For starters, you
don't show us the declaration of arrayStorage, so we have to
guess it: I'll guess int arrayStorage[N], (where N is some
constant). Following that, for reading:

  • It's never correct to use `istream::eof` in a loop. This
    function only has meaning once input has failed; before
    that, it can return `false` when there is nothing else to
    read, and `true` even though the last input succeeded. The
    function you're interested in is `fail`, which is what is
    called indirectly whenever the `istream` is used in
    a context which requires a `bool`.
  • `getline` requires a `char[]`, and reads characters, not
    int's.

If all the file contains is these int's, the standard idiom
for reading them would be:

for (int i = 0; in && i < N; ++ i) {
    in >> arrayStorage[i];
}

In practice, you'll probably want a bit more: is it an error if
the file doesn't contain enough int's? if it contains too many?
And you'll want to verify after the failure that the failure
was due to end of file, and not some random gibberish in the
file. Or maybe you want (or need) to validate the format more,
ensuring that there is never more than one int in a line.

Your output function looks almost OK; the last index should be N, and not sizeof arrayStorage (which returns the number of bytes in the array, not the number of elements). Later, downstream,
you'll want to check that there has been no errors after
closing the file; there's nothing more frustrating than
a program which says it's succeeded, when in fact only part of
the data made it to disk.

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