Atoi with String varaible

Hi, im trying to use these while condtion to read my file.

1
2
3
4
5
6
7
8
9
10
11
12
hile ( getline ( inFile, sLine ) )
       {
            
                if ( sLine.length() == 0 )continue;        // if line is empty get nl
                vLineList.push_back ( sLine );      // store sLine into vectory
                
                    for ( int myNums = 0; myNums < 1024; ++myNums )
                    {              
                                  
                        if ( sLine[myNums] == ' ' )break;   
                        cout << sLine[myNums];  // prints first digit in file to eof
}


the file looks similar to this:

0001 45 555

right, so read the first digit, and stopped until eof
but now i need to convert these binary like digits to real numbers.
Should i give up on the while condition / dynamic allocation and use an array?
i really want this dynam thing down.


hey, i think theres something in C++ where u can use the %d and %f as long as u include a specific function to go with cout. I haven't found it yet tho. And that would solve the conversion to integer.
As I understand it, you want to read in the file line-by-line and store the contents of the file as a list/vector of integers. So in your example above you want the list to contain three elements -- 1, 45, and 555.

So you can read in lines and parse them as you are doing, in which case you can use strtol or strtoul.

There are other ways to do it though.

sLine has the contents of the file.

What i did in C Syntax is used the 'fscanf' function and converted the numbers to %f or %d, that way.

i've already read what i 'wanted' to read. In my file, certain numbers needed to be converted, for example, the 0001, in my preceeding post, needes to be a integer or 'single digit', which atoi does.

Therefore, the loop-- ( myNums < vLineList.size() WONT WORK! it truncates the 1!! )
1
2
3
4
5
for ( int myNums = 0; myNums < 1024; ++myNums )
                    {              
                                  
                        if ( sLine[myNums] == ' ' )break; 
                     }

So this loop nested in my while conditional to read the file, stops reading digits when it hits a space in the file and continues until EOF; as a result it stores those values into sLine.

So i've read what i 'wanted', now, parently, i need to use a character array to convert to ascii to integer. Which means reading the file differently.

I would reallly like to use this vLineList deal with the <vector> library.
Topic archived. No new replies allowed.