数组和十六进制值C++
Array and hex values C++?
我有一个.txt
,其中有一些十六进制的数字,我将它们放入一个带有ifstream
的数组中。然而,它打印不正确,我的意思是,数字不符合要求。这是我的代码:
void arrayNumbers(unsigned int longSize, unsigned int array[]) {
file.open("hex.txt");
int i;
if(file){
longSize=4;
array[longSize];
for(int i=0;i<longSize;i++){
file >> hex >> array[i];
cout << array[i]<< endl;
}
cout << "RIGHT" << endl;
}
else
cout << "WRONG" << endl;
file.close();
}
在main()
中,我有:
void main() {
unsigned int array[longSize];
unsigned int longSize=4;
arrayNumbers(longSize,array);
cout << "OK" << endl;
}
这里发生了什么?T-T
longSize=4;
array[longSize];
这一点完全错了。你在这里尝试做的事情是你做不到的:
- 在运行时增加数组的大小(可能是您使用无意义的
array[longSize]
的意图) - 从运行时值(
longSize
)中选择数组的大小
请考虑std::vector
。
此外,您的main
返回void
,而不是int
(格式错误),并且main
内的数组具有另一个运行时变量的大小,该变量在数组的后面声明为!
最后,我强烈建议使用传统的缩进和大括号格式,因为您的格式很奇怪,而且代码很难阅读。
错误可能是在打印数字之前没有将hex
传递给cout
。
如果稍微清理一下你的程序:
#include <iostream>
#include <fstream>
using namespace std;
void arrayNumbers(unsigned int longSize, unsigned int array[]) {
fstream file("hex.txt");
if (file){
for (int i = 0; i < longSize; i++){
file >> hex >> array[i];
cout << hex << array[i] << endl;
}
cout << "RIGHT" << endl;
} else {
cout << "WRONG" << endl;
}
}
void main() {
const size_t longSize = 4;
unsigned int array[longSize];
arrayNumbers(longSize, array);
cout << "OK" << endl;
}
相关文章:
- 如何在openssl-ecc中获取十六进制格式的私钥
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- 为什么mpfr_printf与十六进制浮点(%a转换说明符)的printf不同
- 在 std::无符号字符的向量处存储 int 的十六进制表示形式
- 指向存储在字符串 c++ 中的十六进制
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C ++如何使用UTF8十六进制代码打印UTF8符号?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何在C++中将十六进制字符串转换为文本数据
- 使用 sprintf 将十六进制0xAABBCC转换为字符串"AA:BB:CC"
- 绝对编码器十六进制输入
- 为什么C++总是显示十六进制内存地址,而不仅仅是整数?
- C++17 十六进制浮点文字单精度后缀冲突?
- 是否可以在C++中获取 CHAR 的有效十六进制地址?
- 打印十六进制字符
- 编译器始终打印十六进制/八进制数字