mardi 17 mai 2022

it's not reaching end of file while reading the file and loop runs for infinite [duplicate]

while loop in ifstream does not stop even if the the condition is to stop at the end of file also i intended to read the string till it reach either full stop or max 75 character limit for a line then print rest on next line but after printing first line correctly it doesnt read anything

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

int main()
{   
    system("cls");
    
    char para[]="The default behaviour of ifstream type stream (upon opening files) allows user to read contents from the file. if the file mode is ios::in only then reading is performed on a text file and if the file mode also includes ios::binary along with ios::in then, reading is performed in binary mode. No transformation of characters takes place in binary mode whereas specific transformations take place in text mode. ";
   puts(para);
    char line[75];
    int a=strlen(para);
    cout<<endl<<"Size of paragraph: "<<a<<endl;
  
    ofstream fout;
    fout.open("out.txt");
    fout.write(para,a);
        fout.close();

    ifstream fin;
    fin.open("out.txt");
    int b=0;
    //char q;
    cout<<endl;
//this loop has the problem
    while (!fin.eof())
    {    
        b++;
        fin.getline(line,74,'.');
        //  if(fin.eof())
        //     break;
     
        cout<<b<<" : "<<line<<"\n";
       
    }
    fin.close();

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire