0

我正在研究 HEVC,即 X265,在这里我正在尝试使用从文件中读取的值输入 QP 数组。我知道 qp 数组的值将从 0 到 100。

我创建了一个测试文件并输入 1 和 0 的组合直到 99。文件如下所示:

10101010110101010000000000000000000000000000000000000000000000000000000000000000000000000000000000

我为此目的编写的代码如下:

ifstream myfile;
    myfile.open("/Users/Ahmedrik/Mine/Uni-Stuff/Test-FYP/roi.txt");
    char input[100];
    int qp_input[100];


        while (!myfile.eof()) {
            myfile >> input;
            cout<< input<<endl;
        }myfile.close();


    for(int i=0;i<100;i++){
        qp_input[i]=(int)input[i];
        if(qp_input[i]==48){
            qp_input[i]=1;
        }
        else
            qp_input[i]=0;

        cout<<i<<" : "<<qp_input[i]<<endl;
    }

但我无法拥有正确的价值观。qp_input 保持为 0。我做错了什么?

4

2 回答 2

2

检查此解决方案

#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sstream>
#include <fstream>
using namespace std;
int main(int argc, char* argv[]) {
    ifstream myfile;
    myfile.open("text.txt");

    int qp_input[100];

    //will read whole contents to a string instead of while(!myfile.eof())
    std::string input( (std::istreambuf_iterator<char>(myfile) ),
                       (std::istreambuf_iterator<char>()    ) );
    for(int i=0;i<input.size();i++){
        stringstream ss;
        ss<<input[i];
        ss>>qp_input[i];
        cout<<i<<" : "<<qp_input[i]<<endl;
    }
}
于 2016-10-14T07:50:28.890 回答
0

在数组中输入,并且您试图读入指针“>> input”而不是该数组中的数组索引,例如“>> input[index]”。您应该在循环中有一个计数器并读入数组。

    int index = 0;
    while (!myfile.eof()) {
        myfile >> input[index];
        cout<< input[index] <<endl;
        index++;
    }
    myfile.close();

另外,文件中的数据是什么类型。目前,您将读取字符,因此假设它们是字节。如果您有纯文本十进制格式的结果,您需要将输入类型更改为 int 或 double。

于 2016-10-14T07:35:36.003 回答