Visual C 32位整数从文件到8位字符到文件 - 程序在某些整数上崩溃
visual C++ 32 bit integer from file to 8 bit character to file - program crashing at certain integer
长时间的听众,首次呼叫者。
我的任务是在一系列32位整数中读取从文件和输出8个字符到新文件,从而产生ASCII艺术品(Shrek LOL(
以下功能绝对良好,并输出我期望的内容:
'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 538976288761274400 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个问题:
- 在读取无效后,输入流无效。因此,您应该在将值添加到阵列之前是否成功:
while (inFile.good()) { int tmp; infile >> tmp; if (infile.good()) { intArray[i] = tmp; cout << intArray[i] << endl; i++; k++; } }
- 由于您的数组的尺寸为256,如果您读取更多的数字(即超过其边界(,这将导致不确定的行为(实际上,如果您超过边界过多,通常会崩溃(。增加数组的大小(或使用更好的替代方案,例如
std::vector
,可以调整自己的大小(。
相关文章:
- 如何在c++中从文本文件中逐行读取整数
- 从二进制文件中读取整数数组
- 整数键映射到头文件中的成员函数指针
- 在 c++ 中连接字符串和整数,以便在 C++ 11 不支持计算机的情况下读取多个文件
- 如何在从文件中读取整数时使用 file.eof()?
- 读取二进制文件并解释为整数
- C++:从文件中读取字符串和整数,并获得最大数字
- 尝试将文本文件中的整数替换为用户输入的整数 [C++]
- 我需要编写一个程序来读取一个文件,该文件将输出所有唯一的整数,如果已经看到整数,它将被关闭
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- (C/C++)fscanf_s从txt文件以字符形式读取数组时缺少整数参数错误
- 在控制台上输出我从文件中读取的整数,将程序插入无限循环
- 从文件中读取后,将分号分隔的整数存储到数组中
- 如何将ASCII数字转换为从文件中提取的整数
- 如何在一个巨大的文本文件中对整数进行排序
- 向文件读取和写入整数的程序
- 在 c++ 中将整数写入.txt文件
- 将文本文件读入整数数组
- 对包含整数的文本文件进行排序时,必须逐行进行排序
- 累积从 C++11 中的文件中读取数据的整数