Charli
Charli

Reputation: 1

Attaching text file to Visual Studio project...?

I'm almost done with a homework assignment but I'm having a hell of a time with one aspect of it. The purpose of the program is to read in a text file, and then do analysis. Now, if I'm on my computer, I can put in the full path of the file and it runs fine.

But it won't run ok if my professor tries to run it. I tried prompting the user to input a full path and that didn't work. I tried attaching the text file to the .exe but I don't think I did it right.

Anyone have any advice?

//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);
            }
        }
    }

Upvotes: 0

Views: 2495

Answers (3)

Ben Voigt
Ben Voigt

Reputation: 283614

This is the problem:

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

Double-backslashes are only meaningful to the C++ compiler. When you prompt the user for a path, the compiler isn't involved and double backslashes should NOT be used. (and string input cannot use \t to indicate a tab, etc., unless you implement special processing afterward)

Upvotes: 2

gideon
gideon

Reputation: 19465

This bit of code I'm assuming is where the file path is being entered/decided.

.......
cin >> whichbook;

if(bookinput == 1){
  bookselect = "1984.txt";
}
else if(bookinput == 2){
  bookselect = "conneticutYankeeInKingArthursCourt.txt";
}
//should'nt this be either one line?
bookread.open(bookselect.c_str());
bookread.open(whichbook.c_str());

So if bookinput is 1 or 2, you're trying to open a file without specifying the path? You could keep the two text files in the same path as the executable and then you have to get the path of the executable and use it with the filename, instead, the easier/crappier route would be, you could hardcode it to a known path

Like:

  bookselect = "C:\\Temp\\1984.txt";

Upvotes: 0

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22064

You can keep the source file/exe file and text file in the same folder. Inform your professor to copy the entire folder and run it from there.

On another thought, the Prof. should have his own copy of the text file and he should be giving you instructions on how to locate the file within your program.

Upvotes: 0

Related Questions