Outfile 因内存冲突导致崩溃
Outfile causes crashes with a memory violation
我正在编写一个程序,将数字从十进制转换为二进制。我已经有了正确的算法,并且程序在使用 cout 时运行良好。但是,一旦我在循环中使用 outfile,程序就会崩溃并显示错误代码 (0xC0000005)。
这是我的源代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
int num, remainder_count;
ifstream infile; //define new input file stream
ofstream outfile; //define new output file stream
infile.open("C:\Users\Arctic-Gaming\CLionProjects\working\Source\Binary Conversion (Loop w File)\Binary Input.txt"); //connect the stream to an actual file
if (!infile)
{
cout << "Cannot open input file! Program aborted" << endl;
return 1;
}
outfile.open("C:\Users\Arctic-Gaming\CLionProjects\working\Source\Binary Conversion (Loop w File)\Decimal Output.txt"); //connect the stream to an actual file
do
{
int remainder [15] = {0};
remainder_count = 15;
infile >> num;
outfile << "\n" << num << endl;
if (num > 0 && num <= 65535)
{
while (num > 0)
{
remainder[remainder_count] = num % 2;
num /= 2;
remainder_count--;
}
remainder_count = 0;
while (remainder_count < 16)
{
if (remainder_count % 4 == 0)
{
outfile << " ";
}
outfile << remainder[remainder_count];
remainder_count++;
}
}
else if (num == 0)
outfile << "0000 0000 0000 0000" << endl;
else
cout << "Error! Invalid Input." << endl;
}
while (!infile.eof());
}
您的程序越界访问元素具有未定义的行为。由于行为是未定义的,这个问题实际上与使用 std::cout
而不是使用文件流无关。
int remainder [15] = {0};
//...
remainder_count = 15;
//...
remainder[remainder_count] = num % 2; // out-of-bounds access (remainder[15] is out-of-bounds)
一旦执行了上面的那行代码,关于您的程序将如何运行的所有赌注都将落空。数组的有效索引范围从 0
到 n-1
,其中 n
是数组中元素的数量。因此,对于 remainder
数组,有效索引为 0
、1
、2
,直至 14
。
如果您切换到使用 std::array 而不是常规 C++
数组,而不是未定义的行为,您将在访问该元素时立即抛出 std::out_of_range
异常at()
.
std::array<int, 15> remainder= {{0}};
remainder_count = 15;
//...
if (num > 0 && num <= 65535)
{
while (num > 0)
{
remainder.at(remainder_count) = num % 2; //... exception thrown
因此,如您所见,您的程序从未像您声称的那样 "ran fine",您将不得不修复您的程序,以免超出数组范围。
我正在编写一个程序,将数字从十进制转换为二进制。我已经有了正确的算法,并且程序在使用 cout 时运行良好。但是,一旦我在循环中使用 outfile,程序就会崩溃并显示错误代码 (0xC0000005)。 这是我的源代码:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
int main()
{
int num, remainder_count;
ifstream infile; //define new input file stream
ofstream outfile; //define new output file stream
infile.open("C:\Users\Arctic-Gaming\CLionProjects\working\Source\Binary Conversion (Loop w File)\Binary Input.txt"); //connect the stream to an actual file
if (!infile)
{
cout << "Cannot open input file! Program aborted" << endl;
return 1;
}
outfile.open("C:\Users\Arctic-Gaming\CLionProjects\working\Source\Binary Conversion (Loop w File)\Decimal Output.txt"); //connect the stream to an actual file
do
{
int remainder [15] = {0};
remainder_count = 15;
infile >> num;
outfile << "\n" << num << endl;
if (num > 0 && num <= 65535)
{
while (num > 0)
{
remainder[remainder_count] = num % 2;
num /= 2;
remainder_count--;
}
remainder_count = 0;
while (remainder_count < 16)
{
if (remainder_count % 4 == 0)
{
outfile << " ";
}
outfile << remainder[remainder_count];
remainder_count++;
}
}
else if (num == 0)
outfile << "0000 0000 0000 0000" << endl;
else
cout << "Error! Invalid Input." << endl;
}
while (!infile.eof());
}
您的程序越界访问元素具有未定义的行为。由于行为是未定义的,这个问题实际上与使用 std::cout
而不是使用文件流无关。
int remainder [15] = {0};
//...
remainder_count = 15;
//...
remainder[remainder_count] = num % 2; // out-of-bounds access (remainder[15] is out-of-bounds)
一旦执行了上面的那行代码,关于您的程序将如何运行的所有赌注都将落空。数组的有效索引范围从 0
到 n-1
,其中 n
是数组中元素的数量。因此,对于 remainder
数组,有效索引为 0
、1
、2
,直至 14
。
如果您切换到使用 std::array 而不是常规 C++
数组,而不是未定义的行为,您将在访问该元素时立即抛出 std::out_of_range
异常at()
.
std::array<int, 15> remainder= {{0}};
remainder_count = 15;
//...
if (num > 0 && num <= 65535)
{
while (num > 0)
{
remainder.at(remainder_count) = num % 2; //... exception thrown
因此,如您所见,您的程序从未像您声称的那样 "ran fine",您将不得不修复您的程序,以免超出数组范围。