C++ Files

This is a discussion on C++ Files within the C++ Programming forums, part of the General Programming Boards category; Hey guys, I was trying to make code that would count how many times the word hello appears in a ...

You cannot use == with character arrays. You should use C++ strings instead, or, if you must use C style strings, use strcmp.

The forward slash does not need to be escaped, so your file path should be "c:/test.txt" or "c:\\test.txt".

You shouldn't use eof() to control the while loop. Simply use the call to getline as the while control. You already do this in the inner loop, so the outer eof() loop is redundant and should be removed. If you don't remove it and the input fails for some reason, the code will loop forever.

yes by adding if statements you could test for other words. Uhm, do you actually know how to program in for example C or visual basic? Because those are quite basic questions.

Code:

while (file.getline(ch, 1000))

does (grosso modo):
as long as the file hasn't been fully read, do "file.getline(ch, 1000)". Now, that command means, get a line from the file and store it in the char array. A line means a row of characters untill a line break. The thousand stands for, if the line is longer than 1000 characters, it won't fit in your array "ch", so to avoid errors, we only read untill 1000 chars. In the next pass, the rest of the line would be read.

Hey, thanks again for your reply as you can probably tell I am brand new to programming so I am finding it a bit hard to get my head round is there anyway that a whole file could be read without having to make a huge array? Thanks again.

Oh and when my program asks for a "color number" it means a chart I made with the understood colors for each number. When I find out how to attach files I will give you my program with everything that comes with it.