我几乎完成了一项家庭作业,但从一个方面来说,我却度过了一段时光。该程序的目的是读取一个文本文件,然后进行分析。现在,如果我在计算机上,则可以放入文件的完整路径,并且可以正常运行。

但是,如果我的教授尝试运行它,那就行不通了。我试图提示用户输入完整路径,但是没有用。我尝试将文本文件附加到.exe,但我认为我做的不正确。

有人有什么建议吗?

//int bookinput = 0;
    //string whichbook;
    //ifstream bookread;
    //ifstream bookread(whichbook.c_str());
    //cout << "Welcome to the book analysis program.\n";
    //cout << "Please type in the full path of the book, remembering to double backslashes: ";
    //cin >> whichbook;
    //
    //if(bookinput == 1){
    //  bookselect = "1984.txt";
    //}
    //else if(bookinput == 2){
    //  bookselect = "conneticutYankeeInKingArthursCourt.txt";
    //}
    //

    //bookread.open(bookselect.c_str());
    //bookread.open(whichbook.c_str());

    bookread.open(whichbook.c_str());

    if(bookread.is_open()){
        std::cout << "opening book\n\n";
        if(bookread.good()){
            cout << "opening of book successful :D";
        }
        while(bookread.good()){ //reads to end of file
            string input;
            //getline(bookread, input);
            bookread >> input;

            //only add alphanumerical strings to the word list
            if (isAlphaNumerical(input))
            {
                words.push_back(input);
            }
        }
    }

最佳答案

这就是问题:

cout << "Please type in the full path of the book, remembering to double backslashes: ";


双反斜杠仅对C ++编译器有意义。当您提示用户输入路径时,不会涉及编译器,并且不应使用双反斜杠。 (并且字符串输入不能使用\t表示制表符等,除非您随后执行特殊处理)

关于c++ - 将文本文件附加到Visual Studio项目中吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5163841/

10-09 06:01
查看更多