Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Outfile导致内存冲突导致崩溃_C++_Arrays_Fstream_Access Violation - Fatal编程技术网

C++ Outfile导致内存冲突导致崩溃

C++ Outfile导致内存冲突导致崩溃,c++,arrays,fstream,access-violation,C++,Arrays,Fstream,Access Violation,我正在写一个程序把数字从十进制转换成二进制。我已经有了正确的算法,程序在使用cout时运行良好。但是,只要我在循环中使用outfile,程序就会崩溃,并出现错误代码(0xC0000005)。 以下是我的源代码: #include <iostream> #include <iomanip> #include <fstream> #include <cmath> using namespace std; int main() { int n

我正在写一个程序把数字从十进制转换成二进制。我已经有了正确的算法,程序在使用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;
int main()
{
int num,余数_计数;
ifstream infle;//定义新的输入文件流
ofstream outfile;//定义新的输出文件流
infle.open(“C:\\Users\\Arctic Gaming\\CLionProjects\\working\\Source\\Binary Conversion(Loop w File)\\Binary Input.txt”);//将流连接到实际文件
如果(!infle)
{
库特数;

outfile您的程序通过访问超出边界的元素而具有未定义的行为。由于行为未定义,因此问题实际上与使用
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
是数组中的元素数。
剩余部分的有效索引是
0
1
2
,直到
14
数组

如果您已切换到使用而不是常规的
C++
数组,而不是未定义的行为,则使用
at()
访问该元素时,将立即引发
std::out_of_range
异常

std::数组余数={{0};
余数=15;
//...     
如果(num>0&&num 0)
{
余数.at(余数计数)=num%2;//…引发异常


因此,正如您所看到的,您的程序从来没有像您声称的那样“运行良好”,您必须修复您的程序,以使您不会超出数组的界限。

rements[rements\u count]
--如果
余数_count==15怎么办?
看到该行有什么问题吗?欢迎来到堆栈溢出。请花时间阅读并参考您可以在此处询问的内容和方式。解决此类问题的正确工具是调试器。在询问堆栈溢出之前,您应该逐行检查代码。有关更多帮助,请阅读。至少,您应该[编辑]您的问题,以包含一个重现您的问题的示例,以及您在调试器中所做的观察。此外,您的程序永远不会“运行良好”,即使使用
cout
。感谢数组的提示。我不知道我用错了。当我创建数组时,我认为数字是我想要多少个元素,从0开始,以15结束。总共16个。只需将数组更改为这个固定值:
数组余数;
     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