Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;
}
  
  
}

Write a function fileLoadRead that reads a file. The function takes one parameter, the name of the file filename . The function returns the number of lines in the file If the file was not opened successfully, return -1 If the file exists but is empty (contains no non-newline characters), return 0 If the file exists and is not empty, return the number of lines. Include all the lines in your count even those that are blank For Example Given a file called myFile.txt with the following content hola ciao hello hallo the function call fileloadRead"myFile.toxr) would return 6.

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;

}