-1

I have got a text file with contents:

Artificial neural networks (ANNs) or connectionist systems are computing systems vaguely inspired by the biological neural networks that constitute animal brains.[1] Such systems "learn" (i.e. progressively improve performance on) tasks by considering examples, generally without task-specific programming. For example, in image recognition, they might learn to identify images that contain cats by analyzing example images that have been manually labeled as "cat" or "no cat" and using the results to identify cats in other images. They do this without any a priori knowledge about cats, e.g., that they have fur, tails, whiskers and cat-like faces. Instead, they evolve their own set of relevant characteristics from the learning material that they process.

and I'm using this code to read the contents.

ifstream file("/Users/sourav/Desktop/stl/stl/stl/testdata.txt");
while (! file.eof()) {
    string word;
    file >> word ;
    cout << word << "\n";
}

This is the first few lines of output:

Artificial

neural

(ANNs)

are

vaguely

and if you notice that the contents are not read properly. I don't see or connectionist systems are computing systems.

I'm missing few string values from the text file while reading it.

Note:I'm using Xcode.

ifstream file("/Users/sourav/Desktop/stl/stl/stl/dictionary.txt");
string line;

if (file.is_open())  // same as: if (myfile.good())
{

    while(getline(file,line,'\r')){
         transform(line.begin(), line.end(), line.begin(), ::tolower);
        Dictionary.insert(line);


    }
    cout<<Dictionary.size()<<" words read from dictionary\n";
    file.close();

Why does the dictionary.size() change in value when I transform it to lowercase

Sourav Sachdeva
  • 353
  • 5
  • 18
  • The concerns belo about about `while (! file.eof())` are valid. [It's a common bug](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong), but I can't see how this results in the problem you are reporting. [Bashing out a quick test case](https://ideone.com/GyMKyD), I'm unable to reproduce. Obviously the linked code cannot run, no file, but it does expose one of the many problems with `while (! file.eof())`: If the file did not open it will never hit EOF. Please add a [mcve] to the question so we can see exactly what is going on. – user4581301 May 03 '18 at 16:37

2 Answers2

1

Try using something along the lines of this:

ifstream file("/Users/sourav/Desktop/stl/stl/stl/testdata.txt");

string word;
while(file >> word) //While there is a word to get... get it and put it in word
{
    cout << word <<"\n";
}

A little bit more of an explanation can be found on the accepted answer in question read word by word from file in C++

I don't see much of a difference in logic though between this and your logic.

JParks
  • 27
  • 4
1

While this may not explain why it does not work, your code may look like:

ifstream file("testdata.txt");
do {
  string word;
  file >> word ;
  if (!file.good()) break;
  cout << word << "\n";
} while (!file.eof());

It is not correct to test for eof condition if you never tried to read something first.

This code (and yours while being logically incorrect) works perfectly. So something else is happening (that is not related to xcode).

Jean-Baptiste Yunès
  • 30,872
  • 2
  • 40
  • 66