Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: cant view string after read from file  (Read 1305 times)

0 Members and 1 Guest are viewing this topic.

danersound

  • Newbie
  • *
  • Posts: 12
  • sound of the silent
    • View Profile
    • Email
cant view string after read from file
« on: December 08, 2017, 06:57:55 pm »
i have a file.txt with some  text i want to read them and view in a render window with this function
 
    void textLoader (){
        bool dummy;
        string menuLine;
        ifstream infile;
        infile.open ("Tools/datiMenu.txt");
            while(!dummy){
                getline(infile,menuLine); // Saves the line in menuLine.
                cout<<menuLine <<endl;
                menu.setString(menuLine);
                dummy=infile.eof();
            }
        infile.close();
    }
 
the main part if I remove the comment from
// textLoader();
the line
 menu.setString("hello!!!");
is not visible any more!
int main(){

    RenderWindow window(VideoMode(1000, 600), "Scheduler Simulator",Style::Titlebar);

    fontLoad();
    menu.setString("hello!!!");
   // textLoader();
    menu.setCharacterSize(20);
    menu.setFillColor(Color::Red);

    while (window.isOpen()){ // main Loop
        Event event;

        while (window.pollEvent(event)){

            if (event.type == Event::Closed)
                window.close();
        }
       window.clear(Color::Blue);
       window.draw(menu);
       window.display();
    }

    return 0;
}

 

anyone can help me?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: cant view string after read from file
« Reply #1 on: December 08, 2017, 07:10:37 pm »
setString replaces the string, it doesn't concatenate. So only the last line will be visible. And since you don't iterate correctly to read from your file, this last line is most likely empty.

while (std::getline(infile, menuLine))
{
    ...
}
Laurent Gomila - SFML developer

danersound

  • Newbie
  • *
  • Posts: 12
  • sound of the silent
    • View Profile
    • Email
Re: cant view string after read from file
« Reply #2 on: December 08, 2017, 07:23:49 pm »
setString replaces the string, it doesn't concatenate. So only the last line will be visible. And since you don't iterate correctly to read from your file, this last line is most likely empty.

how can i iterate the file correctly ?
« Last Edit: December 08, 2017, 09:21:46 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: cant view string after read from file
« Reply #3 on: December 08, 2017, 09:22:10 pm »
Quote
how can i iterate the file correctly ?
The code at the end of my reply, what do you think it is? :P
Laurent Gomila - SFML developer

 

anything