Karol Ciok
Karol Ciok

Reputation: 1

How to make a funcion in C++ which read from a file to an array of hex?

First of all I must to say that i'm total noob in C++ programing. I have to make this funcion:

void License(bool& exists, unsigned int longLicense, unsigned int license[])

It has to read a txt file which contains 6 hex numbers separated by "-", for example:

462E3784-11F24312-13B57611-27A3197F-3B30158A-AB7EF8E0

and then save this numbers (in decimal) in the "license" array. Also it has to return true or false in "exists".

I've tried doing it like this but it doesn't work. It reads correctly only the first number. The rest of them aren't the same as I have in the txt file.

#include <iostream>
#include <fstream>
#include <string>

#define N_INT_LICENSE 6

using namespace std;

bool existe;
unsigned int licencia[N_INT_LICENSE];

ifstream stream;
stream.open("license.txt");
if (stream) {
    for (int i = 0; i < N_INT_LICENSE; i++) {
        stream >> hex >> licencia[i];
    }

I didn't find other topic that could help me but if there is one I'm sorry for posting that. Please someone help me. Thank you.

Upvotes: 0

Views: 99

Answers (1)

Christophe
Christophe

Reputation: 73376

You should skip the dash:

for (int i = 0; i < N_INT_LICENSE; i++) {
    stream >> hex >> licencia[i];
    if (i!= N_INT_LICENSE-1) {
        char c; 
        stream >> c; 
        if (c!='-') 
           cout<<"Error: invalid format";
    }
}

By the way, what shall happen if the license key is too short ?

Upvotes: 2

Related Questions