I can never get this to work correctly for me, and I'm looking to you guys for help now. This program is going to eventually calculate the betweenness centrality for nodes on a graph, but I'm trying to get the file I/O to work properly. Any ideas? Code in tags and attached. (The fileIn.cpp file is normally named fileIn.cxx, and not specifically included in the build, as that is taken care of within the headers)

Also: I have tried to choose the file path manually, and that didn't work either. I'm coding on Vista x64, though I don't see why that should be an issue.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "fileIn.h"


using namespace std;


int main()
{
	int vertices;
	vector<int> matrix;
	string fileName=getFileName();
	getData(vertices,matrix,fileName);
	
	cout << vertices;
	for(int i=0;i<(vertices*vertices);i++)
		cout << matrix[i] << endl;
	return 0;
}	//main()
#ifndef fileIn
#define fileIn

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "fileIn.cxx"

using namespace std;

string getFileName();
void getData(int &vertices, vector<int> &matrix, const string& fileName);

#endif //fileIn
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "fileIn.h"

using namespace std;

string getFileName()
{
	string fileName;
	string directory;
	size_t found;
	string currentFile=__FILE__;

	cout << "What is the name of the file to be analyzed? " << endl;
	getline(cin, fileName);

	found=currentFile.find_last_of("\\");
	directory=currentFile.substr(0,found);
	directory+="\\data\\";
	directory+=fileName;
	
	return directory;
}

void getData(int &vertices, vector<int> &matrix, const string& fileName)
{
	int temp;
	ifstream openFile;
	openFile.open(fileName.c_str());
	if(openFile.is_open())
	{
		openFile>>vertices;
		while(!openFile.eof())
		{
			openFile>>temp;
			matrix.push_back(temp);
		}
		openFile.close();
	}
	else
		cout << "Unable to read file." << endl;
}

Recommended Answers

All 5 Replies

Be more descriptive of the problem and you are more likely to get an answer. Not everyone is going to download, compile, run, analyze and then post a response.

After quickly reading through posted code in getData() i would change this:

while(!openFile.eof())		
{			
     openFile >> temp;

to this:

while(openFile>>temp)
{

to avoid off by one error that will surely pop up sooner or later because eof() won't equate to false until you try to read past EOF.

Okay, I made the change that you suggested above.

The issue is that no matter how I seem to input the string for the fileName, the openFile.isOpen() returns false. I have attempted to declare the full path manually, I have tried the method above, and I've tried leaving the file in the default directory, and inputting that file name. Again, no success there. I feel like I'm doing something fundamentally wrong with the ifstream syntax, but I have no idea what it is.

What compiler/IDE are you using? That would help us to tell you where the input file should be located. Are you running this from the IDE or directly in a command window?

Hmph. I apologize for wasting everyones' time here. Turns out that the file somehow got misnamed to "test.txt.txt" and that explains all the issues that went with it. I appreciate the help though, and if a mod wants to lock this/ect, that would be great.

Hmm, bet you made the file in notepad and named it "test.txt", and Vista added the other .txt. Or VS might have done the same thing.

That's why I like to set my systems to display the file extensions.

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.