spaghet
spaghet

Reputation: 13

How do I print out words from a .txt word list?

I have a .txt word list file with 3000 english words that I want to be printed out.

Here's what I've done:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

vector<string> wordList(){

    string path = "file:///C:/Users/me/Documents/BnS/words.txt";
    ifstream file(path);
    vector<string> list;
    string oneWord;
    while (file >> oneWord){
        list.push_back(oneWord);
    }
    return list;
}

void testWordList(){

    vector<string> words = wordList();
       
    for (unsigned int i = 0; i < words.size(); i++){
        cout << words[i] << endl;
    }
}

int main(){    
    testWordList();
    return 0;
}

Obviously something is wrong since I dont get anything when i run it. What am I missing? Thanks.

Upvotes: 1

Views: 70

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

std::ifstream usually doesn't support reading from URL.

The line

    string path = "file:///C:/Users/me/Documents/BnS/words.txt";

should be

    string path = "C:/Users/me/Documents/BnS/words.txt";

If this doesn't work, try this:

    string path = "C:\\Users\\me\\Documents\\BnS\\words.txt";

(\ is used as escape sequence, so you have to write \\ to express \)

Upvotes: 1

Related Questions