I am working on a text processor that takes in text from a file and inserts it into a Graph data structure. I made the Graph, but I am having trouble with the text processor. Whenever I execute the code, it says I am unable to open the file. I made sure that the text file was in the same directory when I executed the code. Here is the code for the GraphTextProcessor class:
#include <fstream>
#include <cstring>
#include <string>
#include "Graph.h"
class GraphTextProcessor {
private:
Graph* m_data;
public:
GraphTextProcessor();
Graph* process(std::string filename);
};
GraphTextProcessor::GraphTextProcessor() {
}
Graph* GraphTextProcessor::process(std::string filename) {
//process text file and insert into graph here
std::string word;
//opens file in read mode
std::ifstream readFile;
readFile.open(filename.c_str(), std::ios::in);
if (readFile.is_open()) { //Not opening
while (readFile >> word) {
std::cout << word << std::endl;
}
// Closes open text file
readFile.close();
}
else {
std::cout << "Unable to open text file." << std::endl;
}
return NULL;
}
I am just trying to read from a file first before I actually try writing to the Graph. Here is the code that I am running in Main:
#include <iostream>
#include <string>
#include "GraphTextProcessor.h"
int main() {
GraphTextProcessor *gp = new GraphTextProcessor();
gp->process("hello.txt");
}
It prints "Unable to open text file"
. Any suggestions?