I am trying to open a text file so that it can be scanned for certain words that are in a vector. I cannot get the file to open. When I run the program it goes straight to my if statement and outputs File could not be opened.
Main
#include "scanTxt.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <string>
int main()
{
std::ifstream myFile;
myFile.open("emailtest.txt");
std::string word;
int counter[65] = {};
if (!myFile)
{
std::cout << "File could not be opened." << "\n";
}
while (myFile >> word)
{
scanTxt(word, counter);
}
countList(counter);
myFile.close();
system("pause");
return 0;
}
int scanTxt(const std::string &word, int counter[])
{
std::vector<std::string> alert =
{
"agent", "task force", "attack", "assassination", "cops", "response", "dirty bomb",
"domestic nuclear detection", "shots fired", "deaths", "hostage", "breach", "standoff",
"SWAT", "lockdown", "bomb", "facility", "nuclear", "cloud", "plume", "radiation", "leak",
"evacuation", "ebola", "subway", "terrorists", "target", "pirates", "plot", "forest fire"
};
for (int i = 0; i < alert.size(); i++)
{
if (word == alert[i])
{
counter[i]++;
return counter[i];
}
}
}
void countList(int counter[])
{
std::vector<std::string> alert =
{
"agent", "task force", "attack", "assassination", "cops", "response", "dirty bomb",
"domestic nuclear detection", "shots fired", "deaths", "hostage", "breach", "standoff",
"SWAT", "lockdown", "bomb", "facility", "nuclear", "cloud", "plume", "radiation", "leak",
"evacuation", "ebola", "subway", "terrorists", "target", "pirates", "plot", "forest fire"
};
for (int i = 0; i < alert.size(); i++)
{
if (counter[i] > 0)
{
std::cout << "ALERT! : " << alert[i] << "\n\n\tDETECTED" << counter[i] << "TIMES" << "\n";
}
}
}
scanTxt.h
#include <iostream>
int scanTxt(const std::string &, int[]);
void countList(int[]);