简体   繁体   English

读取文件并将其输入为整数数组

[英]Reading file and inputting it as inputting it as integer array

I am working on HEVC ie X265 and over here I am trying input the QP array with the values I read from file.I know that values of qp array would be from 0 to 100. 我正在使用HEVC即X265,在这里我尝试使用从文件中读取的值输入QP数组。我知道qp数组的值将从0到100。

I have created a testing file and input a combination of 1's and 0's till 99. The files looks like this: 我创建了一个测试文件,并输入1和0的组合,直到99。这些文件如下所示:

10101010110101010000000000000000000000000000000000000000000000000000000000000000000000000000000000

the code i have written for this purpose is as follows: 我为此编写的代码如下:

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;
    }

But I am unable to have correct values. 但我无法拥有正确的价值观。 The qp_input stays 0. What is it that I am doing wrong? qp_input保持为0。我做错了什么?

Check this solution 检查此解决方案

#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;
    }
}

Input in an array and you are trying to read into the pointer " >> input" instead of into the array indices within that array eg ">> input[index]". 在数组中输入,您正在尝试读入指针“ >>输入”,而不是读入该数组中的数组索引,例如“ >> input [index]”。 You should have a counter in your loop and read into the array. 您应该在循环中有一个计数器,然后读入数组。

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

Also, what type is the data in the file. 此外,文件中的数据是什么类型。 At the monent you will read in characters so it's assuming they are bytes. 在monent,您将读取字符,因此假设它们是字节。 If you have the results in plain text decimal format you will want to change the type of input to int or double. 如果结果为纯文本十进制格式,则需要将输入类型更改为int或double。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM