• Register
1 vote
21 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

Please log in or register to answer this question.

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 18 views
0 votes
1 answer 19 views
0 votes
1 answer 16 views
16 views
Is there any way to check if the file is in use??
asked Sep 8, 2020 Oscar909 460 points
0 votes
2 answers 28 views
0 votes
1 answer 13 views
13 views
Why doesn't ifstream.eof() return TRUE after reading the last line of the file? When a beginner starts reading an ifstream, the instinct is usually to read the file using a loop like this: while (!ifstream.eof() { ... } However, when I used this code I noticed that ... file is better off using a loop like this: while (ifstream >> someVar) { ... } Why is the first code not always working properly?
asked Aug 29, 2020 sasha 8.4k points
0 votes
1 answer 4 views
0 votes
1 answer 13 views
13 views
Problem: I want to read csv file by using c++ so here is my code int main(){ ifstream classFile("class.csv"); vector<string> classData; while (getline(classFile, line,',')) // there is input overload classfile { classData.push_back(line); } } here is my question ... +/ Library / John is there anyway that I can separate my last column from first of next row? Thank you and sorry that it is confusing
asked Dec 26, 2020 Mashhoodch 9.1k points
0 votes
1 answer 7 views
7 views
Problem: I have a text file with on every line one or more integers, seperated by a space. How can I in an elegant way read this with C++? If I would not care about the lines I could use cin >>, but it matters on which line integers are. I have a problem tomunderstand each line of numbers.
asked Dec 24, 2020 Mashhoodch 9.1k points
0 votes
1 answer 36 views
36 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, 2020 sasha 8.4k points
1 vote
2 answers 780 views
780 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, 2020 Gavin 15.3k points