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.

im not exactly sure but i dont think you can pass an oppened ifstream object. try opening the ifstream object in your build function

im not exactly sure but i dont think you can pass an oppened ifstream object. try opening the ifstream object in your build function

Yeah I've tried doing that. Just to see if that was the problem, I created a local ifstream in my method, opened the file, etc. and it still wouldn't assign the variables.

Modifying the method to this doesn't seem to help.

// Build a CQF by parsing the input file
CQF* Parser::BuildCQF()
{
	ifstream testfReader;
	testfReader.open("input001.txt", ios::in);

	if (!testfReader.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);
	}

	int numOfVars = NULL;
	int numOfCNFs = NULL;

	testfReader >> numOfVars;
	testfReader >> numOfCNFs;

	fReader.close();

	cqf = new CQF(numOfVars, numOfCNFs);
	return cqf;
}

>_< Frustrating.

if you place a cout statement on line 22 to display the values what do you get? also on lines 20 and 21 instead of = NULL; use = 0;

if you place a cout statement on line 22 to display the values what do you get? also on lines 20 and 21 instead of = NULL; use = 0;

Changed NULL to 0, and added at line 22:

//...
cout << "numOfVars = " << numOfVars << endl;
cout << "numOfCNFs = " << numOfCNFs << endl;
//...

My output:

numOfVars = 0
numOfCNFs = 0

would you mind uploading your text file?

Sure. See attachment

alright in this case i believe that you will need to get the data from the file using character strings and then convert it to an integer using atoi();

Is there a particular reason for that?

As you can see in the text file, most of my data is single char values, however, some of my numbers are negative. So I was really hoping to be able to read each number as a string token (delimited by a space) and convert the string to an int when necessary.

From my understanding, the >> operator is supposed to return the next string, int, float, etc. according to the parameter you use it on. And automatically removing any leading/trailing whitespace.. Is that incorrect?

yes because when you have more then one element on a line in the file such as numbers and spaces the indirection operator does not know what to do with the numbers. in cases like this you need to get the information the the form of char variables and convert them. i hope you can understand what I'm saying i might not be explaining it exactly right.

The key to this is the format of your text file. It's a unicode text file which needs special treatment on input.
So the next question is - have you been provided with this specific file which you must be able to read or did you create it yourself?
Hopefully you made it yourself. So create the file again, this time use a different editor or, when saving, make sure you save to a non-unicode format.

If you do that you can return to your original code:
fReader >> numOfVars >> numOfCNFs;
and you'll be just fine.

If you've been given a unicode file because you are expected to read it, do a bit of research on unicode text file format and come back for more help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.