could someone fix this for me? int fileLoadRead(string filename) { ifstream in(f
ID: 3724349 • Letter: C
Question
could someone fix this for me?
int fileLoadRead(string filename)
{
ifstream in(filename.c_str());
if(!in.is_open())
{
return -1;
}
else
{
int count =0;
string line;
while(getline(in,line))
{
count=count+1;
}
in.close();
return count;
}
}
Explanation / Answer
C++ Code:
#include<iostream>
#include<fstream>
using namespace std;
int fileLoadRead(string filename)
{
ifstream in(filename.c_str()); //open file
//if not opened, return -1
if(!in.is_open())
{
return -1;
}
int count = 0; //to count lines
string line; //to read line
//read all lines
while(getline(in,line))
{
count = count + 1; //increment count
}
in.close(); //close file
return count; //return count value
}
int main()
{
string filename = "empty.txt";
int x = fileLoadRead(filename);
cout<<x;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.