while getline problems

Hey folks, im having a problem with the getline function, iv looked through all the other threads about getline and i cant find any that address my problem. So here goes...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void Grid::GenerateGridTiles(fstream &rMap, POINT ptStartPos)
{
  //Initialize the variables required for the loops later
  int iLineNum = 0;
  int iItemNum = 0;
  string sLine, sItem;
  m_ptStartPos = ptStartPos;

  //Gets the length and height of the entire of the map for saving in the map array.
  while(getline(rMap, sLine))
  {
    istringstream linestream(sLine);
    iItemNum = 0;
    while(getline(linestream, sItem, ','))
    {
      iItemNum++;
    }
    iLineNum++;
  }

  //Sets up the map array with the appropriate length/width
  m_pMap = new Tile*[iLineNum];
  for(int i=0; i<iLineNum; i++)
    m_pMap[i] = new Tile[iItemNum];

  //Assign the tile of the map to the Map Array
  iLineNum = iItemNum = 0;
  rMap.seekg(0, ios_base::beg); 
  rMap.clear();
  while(getline(rMap, sLine)) <- this is the loop that is skipped
  {
    istringstream linestream(sLine);
    iItemNum = 0;
    while(getline(linestream, sItem, ','))
    {
      const char* cItem = sItem.c_str();
      int iTileRef = atoi(cItem);
      m_pMap[iLineNum][iItemNum] = *_pTiles[iTileRef];
      iItemNum++;
    }
    iLineNum++;
  }

  //Assign the displayed grid(m_pTileGrid) to a position in the Map
  for(int i=0; i<m_iTilesAcross; i++)
    for(int j=0; j<m_iTilesDown; j++)
      m_pTileGrid[i][j] = m_pMap[(i + m_ptStartPos.x)][(j + m_ptStartPos.y)];
}


All the code compiles fine, however the 2nd group of while loops is skipped (marked with <-----)
this presumably is because the condition of the loop are not met, wich leads me to think that after the first set of loops the getline function has encountered the end of file, and hence when the 2nd lot of loops come it skips them.

My question is, is there any way to bring the indicator of what has been 'got' already back to the beginning of the file (rMap is a csv file).

Someone told me this would work however it dosnt, (inserted just before the troubled while loop.)
1
2
rMap.seekg(0, ios_base::beg); 
rMap.clear();


Any help is appreciated, thanks!
Last edited on
I don't see you attempting to seek or clear the stream in the code that you posted. Post a minimal, but complete example that demonstrates the problem. I can't compile and run what you have posted. Seeking to the beginning is exactly what you need to do and clearing the state of the file stream certainly doesn't hurt.
Topic archived. No new replies allowed.