使用 Ifstream 将文本文件中的数字添加到 C 样式数组
我对上面的标题有疑问。我已尝试以下操作,但它没有以正确的行为起作用。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你正在读/写一个整数数组。看来您可能错误地读取了 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.
您的代码存在几个问题。首先,你
不向我们展示 arrayStorage 的声明,所以我们必须
猜猜看:我猜
int arrayStorage[N]
,(其中N
是一些持续的)。接下来,阅读:
函数只有在输入失败后才有意义;前
也就是说,当没有其他事情可做时,它可以返回“false”
read 和“true”,即使最后一次输入成功。这
您感兴趣的函数是“fail”,这就是
每当使用“istream”时都会间接调用
需要“bool”的上下文。
int 的。
如果文件包含的所有内容都是这些 int,则标准习惯用法
阅读它们的方法是:
在实践中,您可能需要更多:如果
该文件没有包含足够的 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 toguess it: I'll guess
int arrayStorage[N]
, (whereN
is someconstant). Following that, for reading:
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`.
int's.
If all the file contains is these int's, the standard idiom
for reading them would be:
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 notsizeof 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.