• Register
1 vote
15 views

Problem:

How can I import a text file and read it line by line in c++? Is it possible? If so, please tell me the way.
Thanks

12 7 7
15,250 points

1 Answer

1 vote

Solution:

Hello Gavin,

Yes, it is possible with some simple codes. You may my little program below.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

    ifstream file("filename.txt");
    string content;

    while(file >> content) {
        cout << content << ' ';
    }
    return 0;
}

This simple program will do what are you looking for.

Thanks

13 9 6
94,240 points

Related questions

0 votes
1 answer 11 views
0 votes
1 answer 16 views
0 votes
1 answer 12 views
12 views
Is there any way to check if the file is in use??
asked Sep 8 Oscar909 460 points
0 votes
1 answer 25 views
25 views
Read a file line by line using ifstream in C ++ The contents of file.txt are as follows: 5 3 6 4 7 1 10 5 11 6 12 3 12 4 5 Where are the pairs of 3 coordinates? How to process this data line by line in C ++?
asked Aug 29 sasha 2.2k points
1 vote
2 answers 477 views
477 views
Problem: Hello Kodlogs, Myself is a beginner level student of c++ and I am trying to solve problems from a website. I was doing good until I got a problem that says, c++ read file line by line into vector. I was looking for a solution over the internet but I failed. Could you please solve the problem?
asked Apr 3 Gavin 15.3k points