external file read failure

I'm having trouble with a function that reads in data from an external txt file. inFile.fail() keeps returning true which really baffles me because I have the exact same code and am using the exact same file as an earlier assignment but it still works even now.

Here is the function in the new assignment:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
bool readIn(string file, MovieList *ml)
{
  Movie tempFilm;
  string inName, inType;
  int inYr, inMins;
  double inCost;
  bool inYes = false;	// file read success/ fail
  int nothing = 0;  	// just for skipping over movie count at top of file
  ifstream inFile;
  
  inFile.open(file.c_str());

  if (inFile.fail()){
	cout << "File failed to load. Press enter to quit. ";
	cin.get();
	cout << endl << endl;
  }else{
    inFile >> nothing;
	while (!inFile.eof()){
	  inFile >> inName;
	  tempFilm.setTitle(&inName);
	  inFile >> inMins;
	  tempFilm.setLength(&inMins);
	  inFile >> inType;
	  tempFilm.setGenre(&inType);
	  inFile >> inYr;
	  tempFilm.setYear(&inYr);
	  inFile >> inCost;
	  tempFilm.setPrice(&inCost);
	  ml->insertNode(tempFilm);
	}
	inYes = true;
  }
  inFile.close();
  
  return inYes;
}


And here is the previous assignment function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void readIn(string file, Movie *movies, int *size)
{
  string inName, inType;
  int inYr, inMins;
  double inCost;
  int nothing = 0;  // just for skipping over movie count at top of file
					//  since we've already obtained that data
  ifstream inFile;
  
  inFile.open(file.c_str());

  if (inFile.fail()){
	cout << "File failed to load. Press enter to quit. ";
	cin.get();
	cout << endl << endl;
  }else{
    inFile >> nothing;
	for (int i = 0; i < *size; i++){
	  inFile >> inName;
	  (movies + i)->setTitle(&inName);
	  inFile >> inMins;
	  (movies + i)->setLength(&inMins);
	  inFile >> inType;
	  (movies + i)->setGenre(&inType);
	  inFile >> inYr;
	  (movies + i)->setYear(&inYr);
	  inFile >> inCost;
	  (movies + i)->setPrice(&inCost);
	}
  }
  inFile.close();
}


Again, I actually just copies the function from the old assignment into the new one and modified it. I'm using the same txt file and the old one still works but the new one always fails.

Oh, and I've checked that I have the right directives at the top of the code:
1
2
3
4
5
6
#include "Movie.h"
#include "MovieList.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


Any help would be greatly appreciated (I have to turn this in tonight)!
Last edited on
Topic archived. No new replies allowed.