I'm attempting to read in various numbers and chars from a text file using ifstream. I've read through a lot of C++ File IO and can't see why my two int variables, numOfVars and numOfCNFs, aren't getting assigned in the Parser::BuildCQF() method.
The code below shows my Parser.h, Parser.cpp, the instantiation of a new Parser object, and a portion of the text input file.
//****************** Parser.h ******************
#ifndef PARSER_H
#define PARSER_H
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "stdafx.h"
#include "CQF.h"
using namespace std;
class Parser
{
ifstream fReader;
CQF* cqf;
//CNF GetCNF();
public:
Parser(string);
CQF* BuildCQF();
};
#endif
//****************** Parser.cpp ******************
#include "stdafx.h"
#include "Parser.h"
// Parser constructor
// Open the ifstream, fReader, with the string filename passed in as the argument
Parser::Parser(string fileName)
{
fReader.open(fileName.c_str(), ios::in);
if (!fReader.good())
{
cerr << "*** Error opening the file ***" << endl;
cerr << "\nBad input file.\nThe program will now exit" << endl;
cout << "\nPress enter to exit";
cin.get();
exit(1);
}
}
// Build a CQF by parsing the input file
CQF* Parser::BuildCQF()
{
int numOfVars, numOfCNFs;
fReader >> numOfVars >> numOfCNFs;
fReader.close();
cqf = new CQF(numOfVars, numOfCNFs);
return cqf;
}
//****************** Tester ******************
void CQFTester::SetupCQF()
{
cout << "\n*** Setting up and building the CQF from the source input file, input001.txt ***\n" << endl;
parser = new Parser("input001.txt");
cout << "\tECHO: parser = new Parser(\"input001.txt\");" << endl;
cqf = parser->BuildCQF();
cout << "\tECHO: cqf = parser->BuildCQF();" << endl;
}
//****************** input001.txt ******************
/*
8 3
A 1 0
N 2 0
1
1 2 0
E 3 4 0
N 0
2
-1 3 0
-1 4 0
*/
Arghh.. Still very new to C++.
Help is appreciated.