0

I have this .txt file that has a lot of words ( one each line ). I tried

ifstream myReadFile;
myReadFile.open("restrict_words.txt");
char output[100];
if (myReadFile.is_open()) {
     while (!myReadFile.eof()) {
          printf("mamao");
          myReadFile >> output;
          cout<<output;
     }
}

But i dont know how to make it work like... where should i pass it path and stuff

I would like to do

while(reading){
     stringArray.add(file.line);
}

How can i do that?

user3120770
  • 199
  • 1
  • 7
  • 19

2 Answers2

4

First, this: (!myReadFile.eof()) is wrong. See the link for why. Second. If all you want is to load a file of strings into an array, this will do it:

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

int main()
{
    std::ifstream inp("restrict_words.txt");
    std::istream_iterator<std::string> inp_it(inp), inp_eof;
    std::vector<std::string> words(inp_it, inp_eof);

    // words now has ever whitespace separated string 
    //  from the input file as a vector entry
    for (auto s : words)
        std::cout << s << '\n';
}

Suggested reading:

Community
  • 1
  • 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • And this class of mine is at a folder named model and inside this model i have the folder chat txts and theres where this txt file is... The path in the ifstream stays the same? – user3120770 Mar 26 '14 at 19:51
  • @user3120770 If I understand your comment, all file paths for opened files must be either absolute (from the root directory) or relative to the *current working directory* of the running process. If you process is running with `"/home/username/foo"` as the working directory and you wanted to open file `"/home/username/foo/subdir/filename.txt"`, you must pass either `"subdir/filename.txt"` or `"/home/username/foo/subdir/filename.txt"` as the file name so the standard library can find it correctly. Hope that made sense. – WhozCraig Mar 26 '14 at 19:54
  • And how do i use this vector, like in c# would be like vector[1] here doesnt work – user3120770 Mar 26 '14 at 19:58
  • `words[i]` or `words.at(i)` for any `i` in 0..(`words.size()-1`) is one option, though I recommend either the range-based for loop as shown in the example or an iterator. You can read about iterators in almost any online text on C++ including my favorite site [here](http://en.cppreference.com/w/cpp/iterator). The `std::vector<>` template exposes such iterators though its member functions. Read about those in the `std::vector<>` link provided in the answer. – WhozCraig Mar 26 '14 at 20:01
  • Please guys, i read a lot this night and i need to get this done. I am using visual studio and marmelade so i have this .cpp file and a folder named chat. Inside this chat folder i have my .txt file with a lot of words ( one per line ). All i need to do is read it and check if my chat text is equal to one of them. And im getting and error in that for missing "," somewhere – user3120770 Mar 27 '14 at 12:45
2

Do you mean this?

//untested
#include <vector>
#include <fstream>
#include <string>
#include <iostream> //edited

int main()
{
    std::ifstream ist("restrict_words.txt");
    std::string word;
    std::vector<std::string> readWords;
    while(ist >> word)
        readWords.push_back(word);
    //test
    for(unsigned i = 0; i != readWords.size(); ++i)
        std::cout << readWords.at(i) << '\n';  // or readWords[i] (not range checked)
}

EDIT:

For each individual line you would do:

std::string line;
std::vector<std::string> readLines;
while(std::getline(ist, line))
{
    readLines.push_back(line);
}
smoothware
  • 898
  • 7
  • 19
  • +1 for both the per-line addition and the notation of `operator []` not being range checked. nice answer. – WhozCraig Mar 26 '14 at 19:50
  • hmmmmm im getting an error when using this code of yours Unhandled exception at 0x5235f49b (KingMobile.s86) in s3e_simulator_debug.exe: 0xC0000005: Access violation reading location 0x00000010. – user3120770 Mar 26 '14 at 20:26
  • When reporting errors, please report at least the line it occurs in and the line before that (best is minimal example). Just tested the top snippet of my answer, worked without problems. Your error sounds like a nullptr access or maybe out-of-range access. Please show some code, it will enable others to help you. Wild guess: Some uninitialized variable maybe? Optionally, try solving it yourself with a debugger. Edit: Do you have a function returing a reference? Does outputting readWords.size() give a number greater than 0? – smoothware Mar 26 '14 at 20:37
  • Sorry here it is: std::ifstream ist("restrict_words.txt"); std::string word; std::vector readWords; while(ist >> word) readWords.push_back(word); return readwords[0]; – user3120770 Mar 26 '14 at 20:43
  • Are you executing that code in main() or another function? Please check if the size of the vector is not 0. Do not use the return type string&, but rather string (since the object you would be referring to goes "out of scope". If you want to output the content of a vector, use either `for(unsigned i = 0; i != vec.size(); ++i) { std::cout << vec[i] << '\n'; //or vec.at(i) }` or `for(const auto& i : vec) { std::cout << i << '\n'; } //need compiler support for C++11` – smoothware Mar 26 '14 at 20:50