CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2009
    Posts
    3

    Unhappy getline problems, need help plz

    Hey folks, im having a problem with the getline function, iv looked through all the other threads about getline and the only one that has come close to my problem is: http://www.codeguru.com/forum/showth...hlight=getline

    however even their solution isnt working, so figured perhaps its something different.

    [ code]
    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;
    while(getline(rMap, sLine)) <------ this main loop 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)];
    }
    [/code]

    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).

    i have already tried:
    [ code]
    file.seekg(0, ios_base::beg);
    file.clear();
    [/code]

    Any help is appreciated, thanks!

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: getline problems, need help plz

    So you want to read the file twice, is that it? I'd think the seekg()/clear() would do it. You are calling it on rMap rather than file, right?

    Perhaps if you passed in rMap as an istream or ifstream rather than an fstream?

  3. #3
    Join Date
    Oct 2009
    Posts
    3

    Re: getline problems, need help plz

    yea im calling

    Code:
    rMap.seekg(0, ios_base::beg); 
    rMap.clear();
    sty bout the mistake i cant figure out how to edit ma post.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured