1

I have a text file with numbers ranging from 0-255 separated by commas. I want to be able to store each of these numbers into an integer array. An example of what the text file might contain is; "32,51,45,12,5,2,7,2,9,233,132,175,143,33..." etc

I have managed to get my program to store the data from the text file as a string and output them on the screen. What I need to do next is store the values of that string in an integer array, separating the numbers by the commas.

Here is the code I have written so far, which I am having problems getting it working;

int _tmain(int argc, _TCHAR* argv[])
{
   string line;
   ifstream myfile ("example.txt");
   if (myfile.is_open())
   {
       while ( myfile.good() )
       {
           getline (myfile,line);
           cout << line << endl;
       }
       myfile.close();
   }

   else cout << "Unable to open file"; 

//STRING CONVERSION
std::string str = line;
std::vector<int> vect;

std::stringstream ss(str);

int i = 0;

while (ss >> i)
{
    vect.push_back(i);

    if (ss.peek() == ',')
        ss.ignore();
}

system("pause");
return 0;
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Mangkey
  • 11
  • 4
  • 1
    What part of the code is causing problems? In other words, what is your question? – pedrofurla Oct 15 '12 at 06:06
  • @pedrofurla I am getting errors saying "Cannot find or open the PDB file" even though the file is being located and read from. But my main question is am I performing the second part of the operation (STRING CONVERSION) correctly? Is this the correct way to do this? – Mangkey Oct 15 '12 at 06:11
  • Edit the question so it provides the proper info snd actually ask something. Don't expect ppl to just look at your and fix it. – pedrofurla Oct 15 '12 at 06:19
  • To your question, I think it will work as you have it (at least the comma-seperation), though I think if you finish reading an `int` and the peek() doesn't give back a comma you should probably break your loop (though it will likely fail on the next read anyway). – WhozCraig Oct 15 '12 at 06:26
  • 1
    How about some of the following: http://stackoverflow.com/questions/1894886/parsing-a-comma-delimited-stdstring http://stackoverflow.com/questions/2619227/best-way-to-get-ints-from-a-string-with-whitespace http://stackoverflow.com/questions/1321137/convert-string-containing-several-numbers-into-integers – Zamfir Kerlukson Aug 25 '13 at 21:03

1 Answers1

-1

It looks like your code for tokenizing your string is bit off. In particular you need to make sure you call atoi() on the string of your integer to get an integer. I'll focus on the parsing of the string though.

One thing you could use is C's strtok. I recommend this mainly because your case is rather simple, and this is probably the simplest way to go about it.

The code you'd look for is essentially this:

char* numStr = strtok(str.c_str(), ",");
while (numStr)
{
    vect.push_back(atoi(numStr));
    numStr = strtok(NULL, ",");
}

strtok() takes two arguments: a pointer to the C-style string (char*) you're tokenizing, and the string of delimiters (note that each character in the delimiter string is treated as its own delimiter).

I should mention that strtok is not thread-safe, and you also have to ensure that the string you extract from the file ends with a null character \0.

The answers to this question provide many alternatives to my solution. If you'd prefer to use std::stringstream then I suggest you look at the 5th answer on that page.

Regarding your trouble with PDBs, what is the exact error you're getting?

Community
  • 1
  • 1
kevintodisco
  • 5,061
  • 1
  • 22
  • 28
  • You have the string terminator character with wrong slash, it should be a _backslash_ (`\\`) not a forward slash. – Some programmer dude Oct 15 '12 at 06:40
  • 2
    Also, use of `atoi` is discouraged in favor of either [`std::strtol`](http://en.cppreference.com/w/cpp/string/byte/strtol) or [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol). – Some programmer dude Oct 15 '12 at 06:42
  • @JoachimPileborg My mistake for making a simple typo. I don't see how that warrants a vote down. My answer provides a solution with relevant sources and alternative options. Your comments note preferred calls. – kevintodisco Oct 16 '12 at 00:42