Yadira Suazo
Yadira Suazo

Reputation: 55

Convert char array to int array c++

I`m having problems converting a char array read from file to an int array. Maybe someone can help me. This is my code:

char vectorPatron[67];
int iPatrones[67];
archivo = fopen("1_0.txt", "r");
for(i=0;i<67;i++){
    fscanf(archivo, "%c", &vectorPatron[i]);
    printf("%c",vectorPatron[i]);
}
fclose(archivo);
for(i=0;i<67;i++){
    iPatrones[i] = atoi(&vectorPatron[i]);
    printf("%d",iPatrones[i]);
}

Upvotes: 4

Views: 16644

Answers (4)

Svetlana Gulotta
Svetlana Gulotta

Reputation: 11

Here is a very simple and reusable approach. This function converts a char into an int and returns the int. BAD_RETURN = 10 because every single character is from '0' to '9'. The source of this function is cplusplus.com.

int CharToInt(char ch)
{
    return((ch >= '0' && ch <= '9') ? ch - '0' : BAD_RETURN);
}

This is my own function that works in VS2015 compiler. Define an int array and initialize it to 0s. Pass this int array along with the its char prototype array into following function. This function converts a char array into an int array using CharToInt function:

void copyCharArrToIntArr(char from[MAX], int to[MAX]) 
{
    for (int i = 0; i < MAX; i++)
        to[i] = CharToInt(from[i]);
}

This approach will work with vectors. Instead of MAX, use i < yourVector.size() for the for-loop condition.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490623

Despite using some C++ features, most of your code looks more like C. Might I recommend something more like:

struct to_int { 
    int operator()(char c) { return c - '0'; }
};

const int num = 67;
std::vector<char> patrons(num);
std::vector<int> patron_values(num);

std::ifstream archivo("1_0.txt");  
archivo.read(&patrons[0], num);

std::cout.write(&patrons[0], num);

std::transform(patrons.begin(), patrons.end(), patron_values.begin(), to_int());
std::copy(patron_values.begin(), patron_values.end(), 
          std::ostream_iterator<int>(std::cout, "\n"));

Upvotes: 5

PeterK
PeterK

Reputation: 6317

I believe you cannot use the atoi function since it needs an string (array of chars terminated by the \0 character). Why dont you simply do:

for(i=0;i<67;i++){
    iPatrones[i] = int(vectorPatron[i] - '0');
    printf("%d",iPatrones[i]);
}

Note: i do not know how the source file looks like, so maybe the error is there. How exactly are those numbers stored in the file? Maybe you could use (if they are stored as number separated by space):

for(i=0;i<67;i++){
    fscanf(archivo, "%d", &iPatron[i]);        
}

Upvotes: 1

abyx
abyx

Reputation: 72978

That's because atoi receives a null-delimited string, while you are passing it a pointer to a single char (both are essentially char *, but the intent and use are different).

Replace the call to atoi with iPatrons[i] = vectorPatron[i] - '0';

Also, you can remove the vectorPatrons array, simply read into a single char in the first loop and then assign to the appropriate place in the iPatrons array.

Upvotes: 2

Related Questions