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

Author Topic: getline skips the first line of txt file, if i use myFile >> str before it. Why?  (Read 1365 times)

0 Members and 1 Guest are viewing this topic.

gwyn0431

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hello everyone. Does anyone knows, why getline() skips the first line of txt file, if i use myFile >> str before it? I mean if i output some amount string of file before using getline, then it will skip that amount of lines and would output only those one, that are left.
Does someone know why?

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main() {
    std::string output;
    std::ifstream openfile("map1.txt");

    if (openfile.is_open()) {
        std::string tileLocation;
        openfile >> tileLocation;

        while (!openfile.eof()) {
            std::string str;
            std::getline(openfile, str);
            cout << "string: " << str << endl;
        }
    }

    return 0;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Keep in mind, this is the SFML forum.
For general C++ questions you're better off with web forums like StackOverflow and similar. ;)

Also when posting code please use the [code=cpp][/code] tags.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/