4

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

4 Answers 4

5

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"));
Sign up to request clarification or add additional context in comments.

1 Comment

You can replace your to_int functor with int to_int(char c) { return c - '0'; }. Either way, have a +1.
2

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.

Comments

1

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

3 Comments

@ J-16: I thought int(vectorPatron[i] - '0') was the same as (int)(vectorPatron[i] - '0') in c++, or is there some other syntax error here?
There is nothing wrong with that statement. As shuttle87 said, it has the same meaning as (int)(vectorPatron[i] - '0').
Yes, actually I`ve forgotten to say that the file is like this: 11111100011000111111
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.