-3

I'm trying to read characters from a file and I get stuck in an infinite loop, this is the code :

#include "stdafx.h"
#include <iostream>
#include <fstream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    ifstream ifs("ifile.txt", ios_base::in);
    int b;
    while ((b = ifs.get()) != EOF)
    {
        cout << b << endl;
    }
}

Note that I've made a file called ifile.txt inside the project Debug folder where the .exe file is being created (I'm using Microsoft visual studio 2013)

when I run it, it get stuck in an infinite loop. Not sure why it happens but I think it might have to be with the place I've created the ifile.txt maybe ?

Tugal.44
  • 153
  • 4
  • 13
  • 1
    What do you think comparing `b` to `ifs.eof()` does? – David Schwartz Oct 02 '14 at 00:09
  • @DavidSchwartz meant to write EOF instead my bad, but for some reason b equals EOF and never gets printed yet the file is filled with characters and not empty – Tugal.44 Oct 02 '14 at 00:15
  • Can you show us the code you're actually having problems with? – David Schwartz Oct 02 '14 at 00:17
  • @DavidSchwartz I've updated the thread, pretty much what was ther before just with the includes & main declaration. I've created ifile.txt inside the Debug folder. – Tugal.44 Oct 02 '14 at 00:22
  • You don't check if the file exists. – David Schwartz Oct 02 '14 at 00:26
  • The updated code is fine , there is no infinite loop. If you have solved your problem, don't edit the solution into the answer. Instead, accept a posted answer. – M.M Oct 02 '14 at 00:28
  • @DavidSchwartz I've created it why wouldn't it be ? I understand that's it's good practice but this is a small application and it won't really solve my problem – Tugal.44 Oct 02 '14 at 00:28
  • @MattMcNabb I'm still having problems with functionality – Tugal.44 Oct 02 '14 at 00:29
  • @Tugal44 So? Currently your question is asking why the code goes into an infinite loop, however the code has no infinite loop in it. If you have a new question **and you cannot solve it by doing your own research first** then post a new question, and leave this question as it was. – M.M Oct 02 '14 at 00:30
  • @Tugal.44 Lots of reasons -- permissions, wrong directory, and so on. The first step in debugging code is to make sure the critical parts of the code test for all possible error conditions and doesn't just blindly continue past a fatal error. – David Schwartz Oct 02 '14 at 00:32

1 Answers1

6

Please never loop on eof() it is almost always wrong.

Try this:

ifstream ifs("ifile.txt", ios_base::in);

char b;
int letters[26] = {};
int numbers[10] = {};

while(ifs.get(b))) // much better !!
{
    cout << b << endl;
}
ifs.close();

In explanation:

There are several problems with using eof(). EOF is only triggered after you try to read your character. I know this is not exactly how you have used it. Another problem is that EOF is not checked after an error happens on the stream (maybe your infimite loop?) That's why it is better to check if the read succeeded rather than if the end of file was reached.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • why eof() is almost always wrong ? what if I use != EOF ? – Tugal.44 Oct 02 '14 at 00:23
  • it doesn't even let me use get with parameters – Tugal.44 Oct 02 '14 at 00:27
  • @Tugal44 **read the documentation** instead of guessing. You will not get far in C++ by a combination of guessing and posting on the internet. – M.M Oct 02 '14 at 00:29
  • @Tugal.44 There are several problems with using `eof()` EOF is only triggered **after** you try to read your character. I know this is not exactly how you have used it. Another problem is that `EOF` is not checked after an error happens on the stream (maybe your infimite loop?) That's why it is better to check if the read succeeded rather than if the end of file was reached. – Galik Oct 02 '14 at 00:29
  • @MattMcNabb thnx for the correction – Galik Oct 02 '14 at 00:33