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

Write a function named fcheck() that checks whether a file exists. The function

ID: 3691322 • Letter: W

Question

Write a function named fcheck() that checks whether a file exists. The function should accept an ifstream object as a formal reference parameter. If the file exists, the function should return a value of 1; otherwise, the function should return a value of 0.

Can you please check if im doing this right? I having a very hard time with chp 8 from c++ for engineers and scientists by Gary J. Bronson. This question BTW is 8.4 # 2

This is what i have:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
int fcheck();
int main()
{
   int result;
   result = fcheck();
   if (result == 1)
   {
       cout << "file exist ";
   }
   else
   {
       cout << "file does not exist";

   }
   system("PAUSE");
   return 0;
}
int fcheck()
{
   int result = 0;
   ifstream myfilecheck;
   myfilecheck.open("values.dat");
   if (myfilecheck)
   {
       result = 1;
   }
   else
   {
       result = 0;
       return result;
   }
}

Explanation / Answer

Answer for Question:

Step 1: I updated the fcheck function with open function call.

myfilecheck.open("values.dat",ios::in);

Step 2: Removed the return statement inside else condition.

Step 3: Added at the end the function call.

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;
int fcheck();

int main()
{
int result;
result = fcheck();
if (result == 1)
{
cout << "file exist ";
}
else
{
cout << "file does not exist";
}

return 0;
}
int fcheck()
{
int result = 0;
ifstream myfilecheck;
myfilecheck.open("values.dat",ios::in);
if (myfilecheck)
{
result = 1;

}
else
{
result = 0;

}
return result;
}

Step 4:

Output:

File Exit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote