1

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[]);
Brian Roper
  • 528
  • 7
  • 13
  • 1
    Get a error message. http://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails – VP. Aug 27 '15 at 16:55
  • try to use flags when declaring ifstream. – MaxDevelop Aug 27 '15 at 16:56
  • remove all the empty lines between your code, please. this is ugly. – Marcus Müller Aug 27 '15 at 16:58
  • At least specify whether or not you're trying to open the file as binary as opposed to text? Also, verify that your executable is in the same directory as the textfile you mentioned. –  Aug 27 '15 at 16:59
  • Use the *full path* for the filename. – Thomas Matthews Aug 27 '15 at 16:59
  • 2
    Fascinated by the choice of words in the example. – SergeyA Aug 27 '15 at 17:03
  • @SergeyA its a school project to replicate the government scanning your emails haha – Brian Roper Aug 27 '15 at 17:40
  • @BrianRoper a little advice about scanning: make it case insensitive – Kael53 Aug 27 '15 at 17:42
  • When just looking for a word in a list, consider [std::set](http://en.cppreference.com/w/cpp/container/set) as a replacement for std::vector. And also think about making the list a static const global (`static const std::vector alert = {...}`) so you A) don't have to rebuild the list on every call to the functions B) don't have to worry about people changing the list, and C) both functions can use the same list. – user4581301 Aug 27 '15 at 17:49
  • Another note, if you declare your functions and then declare main you won't need the header file. – user4581301 Aug 27 '15 at 17:50

0 Answers0