从文件到 8 位字符到文件的 C++ 32 位整数-程序在某个整数处崩溃

标签 c++ visual-c++

<分区>

长期倾听者,第一次来访者。

我的任务是从文件中读取一系列 32 位整数并将 8 位字符输出到一个新文件,从而生成 ASCII 图稿(史莱克笑)

以下工作绝对正常并输出我所期望的:

'538976288 538976288 538976288 538976288 538976288 1600085855
537534559 538976288 538976288 538976288 538976288 539438380
538976288 543108704 538976266 538976288 538976288 538976288
538978092 538976288 538976288 539910176 538976288 740302880
169881133 538976288 538976288 538976288 539438112 538976288
538976288 538976288 542908448 538976288 740892704 169892910
740302880 538976302 538976288 538976303 538976288 538976288
538976288 1545609248 538976288 539504416 540761120 790634506
542923815 538976288 539914028 538976288 740302880 1869573999
538979951 538991648 538978092 690433376 538970656 689973545
1679830624 1495420984 538979896 942415904 572662352 945365538
1612718126 773857319 656551213 673188384 539438432 1499471904
1612718119 538982489 1348739104 538976288 1612718112 538976295
539959328 1612718090 757935405 538978350 543121184 538976352
539438112 757935404 538976302 538976288 537534504 538976288
538976288 757935145 539910190 539500576 757873440 539897133
538976288 689971232 538970656 538976288 539959328 744443740
539566119 542908448 1599036448 538978092 538976288 545005600
538976266 538976288 1612718139 740306221 538976295 538976288
757935456 538976295 538976288 169901088 538976288 2082480160
538976288 538978093 538976288 1612718112 538976302 538976288
538976288 537534588 538976288 540761888 740302880 538976288
538976288 538976288 538976297 538976288 975183904 538970656
657350432 538976380 778051616 543108666 774643744 539114042
539897440 538976288 545005600 657272074 2082480160 773857312
538976295 572661792 538976290 538976288 543170592 538976288
778075168 538970656 538976288 975183996 538976315 975183904
975183904 538976288 539909920 538976288 1618747424 758013998
774712615 538970656 538976288 656416892 538979872 975183904
975183904 741236575 539982887 538976288 545005600 169892896
538976288 543170592 538976288 774712668 758013791 2086632487
791510111 538976288 538976288 538976303 537534505 538976288
542908448 538976288 538992476 757948448 1600070238 538978092
538976288 539762720 2082480160 538970656 538976288 538983200
538976352 757948448 757935454 538976295 538976288 740302880
538976295 545005600 538976266 538976288 538991648 538976352
538976288 538976288 538976288 538976288 538980128 538976288
537534511 538976288 538976288 538976348 538976352 1595957024
538976288 538976288 539959328 538976266 538976288 1545609248
538976288 538976288 1612718112 538976288 790634528 538970656
538976288 538976288 538991648 538976288 538976288 538978080
657203232 538970656 538976288 538976288 778051616 538976288
740302880 1595940896 169879340 538976288 ' 

但是下面没有,程序崩溃的唯一区别是添加了 '538976288 538976288 761274400 1600085806 757935406 538976295 '

完整的txt文件内容如下:

'538976288 538976288 538976288 538976288 538976288 1600085855
537534559 538976288 538976288 538976288 538976288 539438380
538976288 543108704 538976266 538976288 538976288 538976288
538978092 538976288 538976288 539910176 538976288 740302880
169881133 538976288 538976288 538976288 539438112 538976288
538976288 538976288 542908448 538976288 740892704 169892910
740302880 538976302 538976288 538976303 538976288 538976288
538976288 1545609248 538976288 539504416 540761120 790634506
542923815 538976288 539914028 538976288 740302880 1869573999
538979951 538991648 538978092 690433376 538970656 689973545
1679830624 1495420984 538979896 942415904 572662352 945365538
1612718126 773857319 656551213 673188384 539438432 1499471904
1612718119 538982489 1348739104 538976288 1612718112 538976295
539959328 1612718090 757935405 538978350 543121184 538976352
539438112 757935404 538976302 538976288 537534504 538976288
538976288 757935145 539910190 539500576 757873440 539897133
538976288 689971232 538970656 538976288 539959328 744443740
539566119 542908448 1599036448 538978092 538976288 545005600
538976266 538976288 1612718139 740306221 538976295 538976288
757935456 538976295 538976288 169901088 538976288 2082480160
538976288 538978093 538976288 1612718112 538976302 538976288
538976288 537534588 538976288 540761888 740302880 538976288
538976288 538976288 538976297 538976288 975183904 538970656
657350432 538976380 778051616 543108666 774643744 539114042
539897440 538976288 545005600 657272074 2082480160 773857312
538976295 572661792 538976290 538976288 543170592 538976288
778075168 538970656 538976288 975183996 538976315 975183904
975183904 538976288 539909920 538976288 1618747424 758013998
774712615 538970656 538976288 656416892 538979872 975183904
975183904 741236575 539982887 538976288 545005600 169892896
538976288 543170592 538976288 774712668 758013791 2086632487
791510111 538976288 538976288 538976303 537534505 538976288
542908448 538976288 538992476 757948448 1600070238 538978092
538976288 539762720 2082480160 538970656 538976288 538983200
538976352 757948448 757935454 538976295 538976288 740302880
538976295 545005600 538976266 538976288 538991648 538976352
538976288 538976288 538976288 538976288 538980128 538976288
537534511 538976288 538976288 538976348 538976352 1595957024
538976288 538976288 539959328 538976266 538976288 1545609248
538976288 538976288 1612718112 538976288 790634528 538970656
538976288 538976288 538991648 538976288 538976288 538978080
657203232 538970656 538976288 538976288 778051616 538976288
740302880 1595940896 169879340 538976288 538976288 538976288
761274400 1600085806 757935406 538976295  '

将整数读入 intArray 似乎没有任何问题,似乎程序在将整数转换为字符时崩溃了。

对于最后几个整数可能出现的问题,我们将不胜感激。

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{
    if ( argc != 2 ) // check arguments
    {
        cerr << "Arguments missing" << endl;
        return 1;
    }

    // set up input file

    ifstream inFile; // declare input file variable

    inFile.open(argv[1], ifstream::in); // open input file

    if (!inFile.good())
    {
        cerr << "Unable to open file" << endl;
        return 2;
    }

    ofstream outFile; // declare output file variable

    outFile.open("outputsample.txt"); // open output file

    // initialize integer array

    int intArray[256];
    int i = 0;
    int k = 0;

    while (inFile.good())
    { 
        inFile >> intArray[i];
        cout << intArray[i] << endl;
        i++;
        k++;
    }

    // convert 32 bit integers to 8 bit characters and print

    for (i = 0; i < k-1; i++)
    {
        int j = 1;
        int numDiv = intArray[i];
        char decChar = numDiv % 256;
        if (decChar == 10)
        {
            outFile << '\n';
        }
        else
        {
            outFile << decChar;
        }

        while (j != 4)
        {
            numDiv = numDiv / 256;
            decChar = numDiv % 256;
            if (decChar == 10)
            {
                outFile << '\n';
            }
            else
            {
            outFile << decChar;
            }
            j++;
        }
    }

    outFile << endl;
    outFile << "Prepared by xxxxxx (xxxxxx)";

    // Close I/O files

    inFile.close();
    outFile.close();

    return 0;
}

最佳答案

您的代码有 2 个问题:

  1. 输入流在无效读取之后无效。因此,在将值添加到数组之前,您应该检查最后一次读取是否成功:
while (inFile.good())
{ 
    int tmp;
    infile >> tmp;
    if (infile.good()) {
        intArray[i] = tmp;
        cout << intArray[i] << endl;
        i++;
        k++;
    }
}
  1. 由于您的数组大小为 256,如果您读取更多数字(即超出其边界),这将导致未定义的行为(实际上,如果您超出边界太多,它通常会崩溃)。增加数组的大小(或使用更好的替代方法,例如可以自行调整大小的 std::vector)。

关于从文件到 8 位字符到文件的 C++ 32 位整数-程序在某个整数处崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49624176/

相关文章:

c# - 从 C++ 库方法返回的 "char*"获取 C# 字符串?

c++ - 如何使用 string::replace 为 char*

c++ - 如何在 Visual C++ 2010 或 2008 中使用 OpenCV 2.1 访问网络摄像头(compro IP50W)

C++ 与部分模板特化语法混淆

c++ - 从数组构造 vector

c# - 在 C# 中使用 C++ 方法

c++ - 在双端队列 C++ 中将所有元素从一个列表传输到另一个列表

c++ - 我可以检索包含非拉丁字符的路径吗?

c++ - 成员函数 SFINAE 错误 C2938

c++ - 如何使用模板参数包实现仅限几种类型的SFINAE