Nino
Nino

Reputation: 11

How to read again from the first line when end of file?

I want to read data from my input file:

Dr Lee
Dr Kumar
Dr Masji
Dr Hioh
Dr Yeoh

here is my sample code:

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

int main()
{
    int num = 0;
    string doc[100];
    ifstream inFile;
    ofstream outFile;
    inFile.open("hi.txt");
    outFile.open("output.txt");
    while (num < 100)
    {
        getline(inFile, doc[num]);
        outFile << doc[num] << endl;
        num++;
    }
    inFile.close();
    outFile.close();
    
    return 0;
}

How do I continue to read again from the first line of my input file after the end of file reach ?

Upvotes: 0

Views: 41

Answers (1)

Veda
Veda

Reputation: 2073

You can go to the beginning of the file with:

inFile.seekg (0, inFile.beg);

Upvotes: 1

Related Questions