0

I am starting the first part of a school assignment and I must prompt the user to enter a filename, check for the existence of the file, and if it exists, open it for processing; otherwise I am to have the user enter another filename.

When I compile and run my program below, I get the error message "No file exists. Please enter another filename." When I type in names of files that don't exist it just runs the first part of my do while loop again. I'm a beginner at C++ but I've done this before and I feel as if it should be running properly. Any help would be appreciated.

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

struct customerData
{ 
  int _customerID;
  string _firstName, _lastName;
  double _payment1, _payment2, _payment3;
};

void processFile();

int main()
{
  processFile();

  system ("pause");
  return 0;
}

void processFile()
{
  string filename;
  ifstream recordFile;

 do
 {
    cout << "Please enter a filename\n";
    cin >> filename;
    recordFile.open(filename);
    if (recordFile.good())
    // {
    //  enter code for if file exists here
    // }
    ;
 }
 while(recordFile.fail());
 {
        cout << "No file by that name. Please enter another filename\n";
        cin >> filename;
        recordFile.open(filename);
 }
}

3 Answers 3

2

To check whether a file was successfully opened you must use the std::fstream::is_open() function, like so:

void processfile ()
{
  string filename;

  cout << "Please enter filename: ";
  if (! (cin >> filename))
    return;

  ifstream file(filename.c_str());
  if (!file.is_open())
  {
    cerr << "Cannot open file: " << filename << endl;
    return;
  }

  // do something with open file
}

The member functions .good() and .fail() check for something else not whether the file was opened successfully.

0
1

I'm not 100% sure what your intent is here, but do you understand that you've only got one loop here? After your do/while loop, you've got some code in braces, but that's not connected to any loop construct... it's simply a new scope (which doesn't serve a purpose here).

So, your program does this:

1) Ask for filename. Try to open it. If file stream can be read, do the "enter code here" part.

2) Check if filestream is "bad". if so, go back to step 1. Otherwise, continue.

3) Print out "no file by that name", prompt for a new file, try to open it

That's almost certainly not what you want.

1
  • Ah I got it, it's been awhile since one of the assignments called for a do/while :p
    – sircrisp
    Oct 4, 2012 at 16:39
1

You can use c code.

FILE *fp = fopen("file" "r");
if(fp){
//do stuff
}
else{
//it doesnt exist
}

on a side note, when using namespace std try to make it not global

you can put it inside of your functions instead when necessary

int main(){
using namespace std;
//other std stuff
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.