无法使用ifstream打开文件

时间:2015-08-27 16:54:33

标签: c++ vector fstream ifstream

我正在尝试打开一个文本文件,以便可以扫描向量中的某些单词。我无法打开文件。当我运行程序时,它直接进入我的if语句并输出文件无法打开。

主要

#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[]);

0 个答案:

没有答案