• Register
1 vote
24 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,260 points

Related questions

0 votes
1 answer 19 views
0 votes
1 answer 25 views
0 votes
1 answer 18 views
18 views
Is there any way to check if the file is in use??
asked Sep 8, 2020 Oscar909 460 points
0 votes
1 answer 9 views
9 views
Problem: Why this error happened: Are arrays passed by reference in C++?
asked Mar 17 Wafa Abu Yousef 6.1k points
0 votes
2 answers 36 views
0 votes
1 answer 19 views
19 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 16.2k points
0 votes
1 answer 18 views
18 views
My goal is the following: to enter 2 numbers (A and B) other than 0 by keyboard, and with A greater than B, I must determine how many times B fits into A, by using a recursive function. However, what I do works with A = 6 and B = 2 in which the program returns that "2 fits 3 times in ... A, B); printf ("% d fits% d times in% d \ n", B, div, A); system ("pause"); } What is the mistake I am making?
asked Aug 10, 2020 game 4.6k points
0 votes
1 answer 14 views
14 views
Problem: Hello developers, I need a little help, I want to save some data in a file and then read it out.
asked Oct 13, 2020 anonymous
0 votes
1 answer 11 views
11 views asked Oct 17, 2020 SapperIt 1.2k points
0 votes
1 answer 21 views
21 views
Problem:hello, kodlogs audience I need your help as I am trying to access a file in java and I don't know how I would be able to that. Kindly help me it would great if you could explain with the help of code
asked Oct 12, 2020 anonymous