View Single Post
Old Nov 13th, 2006, 9:09 PM   #1
Mcoy
Newbie
 
Mcoy's Avatar
 
Join Date: Mar 2006
Posts: 20
Rep Power: 0 Mcoy is on a distinguished road
About atoi function and const chars...

string temp;
    char temp2[3];
    int num_of_values;
    
    getline (myfile,temp);
    
    for(int i(0); i < 3; i++)
    {
            temp2[i] = temp[i];
    }
    
    num_of_values = atoi(temp2);

The goal of the above code was to read a line from a file (with the getline function) and convert it from string type to integer type with atoi(). When supplying the atoi function with the string, the compiler gave me an error saying that atoi did not accept strings, only const char*s. So I had to use the for loop that you see in my code to change each letter from the string to a char array. -Seems very redundant to me considering a string is simply a character array, or am I mistaken?
What is a better/more efficient/intuitive way to read integers into my program from a file?
Mcoy is offline   Reply With Quote