Read line by line file

Asked

Viewed 58 times

0

Hello, I have a question about file reading. Well I’m starting to mess with files now, I would like to know how to read line by line a file and present these lines, already researched, but I only find explanations that mix C with C++.

  • What is the file format? It is a text file?

  • If it is in C++ use the ifstream class or fstream. Search the documentation.

  • file is format . txt

1 answer

0

#include <string>
#include <fstream>
#include <iostream>

std::ifstream arquivo( "teste.txt" );     # Seleciona o arquivo
std::string linha;                        # String linha


int main()
{
    while(getline(arquivo, linha))        # Enquanto houver linhas, continuará
    {
         std::cout << linha << std::endl; # Imprime valores, linha a linha
    }

    return 0;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.