[英]C++ file writing/reading
I'm trying to create an array, write array to the file and than display it. 我试图创建一个数组,将数组写入文件,然后显示它。 It seems to be working but i get just part of the output (first 3 elements) or i get values over boundaries.
它似乎正在工作,但我只是输出的一部分(前三个元素),或者我得到了边界上的值。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int arr[20];
int i;
for (i = 0; i < 5; i++)
{
cout << "Enter the value to the array: " << endl;
cin >> arr[i];
}
ofstream fl("numbers.txt");
if (!fl)
{
cout << "file could not be open for writing ! " <<endl;
}
for (i = 0; i < arr[i]; i++)
{
fl<<arr[i]<<endl;
}
fl.close();
ifstream file("numbers.txt");
if(!file)
{
cout << "Error reading from file ! " << endl;
}
while (!file.eof())
{
std::string inp;
getline(file,inp);
cout << inp << endl;
}
file.close();
return 0;
}
The terminating condition in the for
loop is incorrect: for
循环中的终止条件不正确:
for(i=0;i<arr[i];i++)
If the user enters the following 5 int
s: 如果用户输入以下5个
int
:
1 0 4 5 6
1 0 4 5 6
the for
loop will terminate at the second int
, the 0
, as 1 < 0
(which is what i<arr[i]
would equate to) is false
. for
循环将在第二个int
0
处终止,因为1 < 0
( i<arr[i]
等于)等于false
。 The code has the potential to access beyond the bounds of the array, for input: 该代码具有访问数组范围之外的潜力,可以进行输入:
10 11 12 13 14
10 11 12 13 14
the for
loop will iterate beyond the first 5 elements and start processing unitialised values in the array arr
as it has not been initialised: for
循环将遍历前5个元素,并开始处理数组arr
中的统一值,因为它尚未初始化:
int arr[20];
which could result in out of bounds access on the array if the elements in arr
happen to always be greater than i
. 如果
arr
的元素总是大于i
,则可能导致对数组的访问超出范围。
A simple fix: 一个简单的解决方法:
for(i=0;i<5;i++)
Other points: 其他要点:
always check the result of I/O operations to ensure variables contain valid values: 始终检查I / O操作的结果,以确保变量包含有效值:
if (!(cin >> arr[i])) { // Failed to read an int. break; }
the for
loop must store the number of int
s read into the arr
, so the remainder of the code only processes values provided by the user. for
循环必须存储读取到arr
的int
的数量,因此其余代码仅处理用户提供的值。 An alternative to using an array, with a fixed size, and a variable to indicate the number of populated elements is to use a std::vector<int>
that would contain only valid int
s (and can be queried for its size()
or iterated using iterators). 除了使用固定大小的数组和指示已填充元素的数量的变量外,另一种方法是使用
std::vector<int>
,该std::vector<int>
仅包含有效的int
(并且可以查询其size()
或使用迭代器进行迭代)。
while (!file.eof())
is not correct as the end of file flag will set only once a read attempted to read beyond the end of the file. while (!file.eof())
不正确,因为只有尝试读取超出文件末尾的读取时,文件末尾标志才会设置。 Check the result of I/O operations immediately: 立即检查I / O操作的结果:
while (std::getline(file, inp)) { }
Try this: 尝试这个:
//for(i=0;i<arr[i];i++)
for(i=0;i<5;i++)
[EDITED] I would initialize the array with 0 like this: int arr[20] = {0}; [编辑]我将使用0初始化数组,如下所示:int arr [20] = {0}; In this case you can use for example:
在这种情况下,您可以使用例如:
while ((arr[i] != 0 || i < sizeof(arr))
its like hmjd said 就像hmjd说
for(i=0;i<arr[i];i++)
looks wrong 看起来错了
it should look like this 它应该看起来像这样
int size;
size=sizeof(your array);
for(i=0;i<size;i++)
i<array[i]
这是错误的,因为它与数组的内容相对应,它不检查数组的大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.