2

i have a graph file as

    3200
    12 16
    114 61
    37 465
    .
    .
    .

and it goes like this.

the first number is vertexNumber other integers represents vertices and we have an edge between them. for example myMatrix[12][16] = 1 and myMatrix[16][12] = 1

i used ifstream to read the graph.txt (ifstream theGraphFile("graph.txt", ios::in);) and created a bool matrix with a constant size like 10000

my pattern for my purpose is this:

while( getline( theGraphFile, line ) )
{
        if (line[1])
            continue;
        else
        {
            //parse
            //matrix
        }

}

so my question: how can i seperate those numbers as "before the space and until the end of the line" format. if my string in nth line is 12 51 i want to use them as matrix indexes like myMatrix[12][51] = 1

Thank you.. :)

sbaker
  • 408
  • 5
  • 13
  • Check this: http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – MDman Dec 25 '11 at 08:30
  • i checked that link before but i read the whole topic more carefully when you mentioned it. helped me in some points thank you – sbaker Dec 25 '11 at 18:59

2 Answers2

0

Try this code

int a = 0, b = -1; 
bool done = false;
for (int i = 0; !done; ++i)
{
  if (line[i] >= '0' && line[i] <= '9')
  {
     if (b == -1)
     {
       a *= 10;
       a += line[i] - '0';
     }
     else
     {
       b *= 10;
       b += line[i] - '0';
     }

  } 
  else if (b == -1)
    b = 0;
  else
    done = true;
}

myMatrix[a][b] = myMatrix[b][a] = 1;
Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
0

2 ways come to mind: you can use the formatted extraction operator >> from ifstream directly or use getline to put the entire line into a string first and then use istringstream to extract it into indices.

If your graph file will only contain indices one after another, like your example, the first method will probably be easier:

while(theGraphFile >> x >> y)
{
    if(x < myWidth && y < myHeight)
        myMatrix[x][y] = true;
}

The second method with getline will allow for some flexibility if each line has other info too:

while(getline(theGraphFile, line))
{
    istringstream linestr(line);
    // lines that don't start with x, y indices get skipped.
    // feel free to handle this differently
    if(!(linestr >> x >> y)) continue;  

    if(x < myWidth && y < myHeight) { myMatrix[x][y] = true; }
    // parse other data
}
greatwolf
  • 20,287
  • 13
  • 71
  • 105
  • yes my graph contains only values like x y and 1st method worked nicely. i tried that one with a little graph file (12 as vertex number and there were 6 vertices picked randomly) and i needed to give myWidth and myHeight as myVertexNumber+1 (because of vertexNumber holds the first line and then i need to allocate space for next 12 lines and that makes 13) but that's ok. thank you! – sbaker Dec 25 '11 at 18:56
  • How does it stop working? You'll have to post more code to give it some context. It might be worth posting it as a separate question though. – greatwolf Dec 25 '11 at 22:20