0

I am working on HEVC i.e. 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.

I have created a testing file and input a combination of 1's and 0's till 99. The files looks like this:

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?

Ahmed Rik
  • 58
  • 8
  • [why is while(!myfile.eof()) wrong](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – BeyelerStudios Oct 14 '16 at 07:38
  • Despite your weird way of reading from the file (relevant: http://stackoverflow.com/questions/18398167/how-to-copy-a-txt-file-to-a-char-array-in-c), your code works fine for me. – SingerOfTheFall Oct 14 '16 at 07:47
  • 2
    You have incorrect values because ASCII value of '1' is 49, not 48. Never use magic numbers, please. – Bob__ Oct 14 '16 at 08:07

2 Answers2

2

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;
    }
}
Ajish Kb
  • 330
  • 1
  • 3
  • 11
  • 1
    You could even skip the unnecessary allocations by doing: `std::for_each(std::istream_iterator(myfile), std::istream_iterator(), [](char ch) { ... });` or use `std::transform` to fill the `qp_input` array... – BeyelerStudios Oct 14 '16 at 09:20
  • @BeyelerStudios wow.... thatz awesome. But we need a 11 compiler for that rite..? – Ajish Kb Oct 14 '16 at 09:26
  • 1
    If you want to use a lambda yes, otherwise just define a functor type (type with an `operator()`) and use an instance of that instead. – BeyelerStudios Oct 14 '16 at 09:28
0

Input in an array and you are trying to read into the pointer " >> input" instead of into the array indices within that array e.g. ">> 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. If you have the results in plain text decimal format you will want to change the type of input to int or double.

Eamonn Boyle
  • 351
  • 2
  • 6
  • you're performing unchecked reads, bad example - also there's nothing wrong with reading a string – BeyelerStudios Oct 14 '16 at 07:40
  • @BeyelerStudios But the input array is 100 and then they are looping through the array in the for loop. Clearly the input array is not a char array string but meant to store 100 entries. The example I've given simply fixes the indexing problem which is the question. – Eamonn Boyle Oct 14 '16 at 07:47
  • No, your code fixes nothing in the OP's code. Instead it introduces new bugs, like leaving the 100st entry undefined for 99 characters in the input file. You don't check your inputs, the 100st entry will still be counted by `index` and it'll still write outside the array if there are any more than 99 input characters. No improvement whatsoever. – BeyelerStudios Oct 14 '16 at 08:37