Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Provide a C++ code segment that reads data from a text file using a while loop.

+1 vote
asked Dec 18, 2019 by Ednah (130 points)

1 Answer

0 votes
answered Mar 5, 2020 by James Hunter (220 points)
Here is a simple program ... hope it helps.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream myFile;
 
    myReadFile.open("text.txt");
 
    char outputBuffer[100];
 
    if (myFile.is_open())
    {
        while (!myReadFile.eof())
        {
            myFile >> output;
            cout << output;
        }
    }
    
    myFile.close();

    return 0;
}
commented Mar 6, 2020 by Jean-Michel Wolff (100 points)
//better with same names

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ifstream myReadFile;
 
    myReadFile.open("text.txt");
 
    char outputBuffer[100];
 
    if (myReadFile.is_open())
    {
        while (!myReadFile.eof())
        {
            myReadFile >> outputBuffer;
            cout << outputBuffer;
        }
    }
    
    myReadFile.close();

    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...