Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.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++ 数组和十六进制值C++;?_C++_Arrays_Hex - Fatal编程技术网

C++ 数组和十六进制值C++;?

C++ 数组和十六进制值C++;?,c++,arrays,hex,C++,Arrays,Hex,我有一个带有一些十六进制数字的.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=

我有一个带有一些十六进制数字的
.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();
}
这里发生了什么事?T-T

longSize=4; 
array[longSize];
这一点是完全错误的。你在这里试图做的事情你做不到:

  • 在运行时增加数组的大小(可能是您打算使用无意义的
    数组[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;
}
#包括
#包括
使用名称空间std;
void arrayNumbers(无符号整数longSize,无符号整数数组[]){
fstream文件(“hex.txt”);
如果(文件){
对于(int i=0;i>十六进制>>数组[i];

为什么你把你的<代码> }/Cube放在这样奇怪的地方?你能张贴一个你现在不应该编译的东西吗?你使用的是哪一本C++书?你意识到<代码>数组[LangSe];< /C>不创建新的数组吗?
#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;
}