# Reading from text files in C/C++ [closed]

I'm writing a program that has to check which are the two largest three digit numbers that are missing from a text file. I'm writing this in C++ and I'm trying to solve the mystery in a while loop. How can I "move" the search back to the beggining of the text file? More explicit: if the checking is now around the middle of the text file and I have to check at this point of time again all the numbers from the beggining of the file, how can I do this? I'm attaching only the code part which contains the while.

    while(!in.eof()&&first_number>=100&&second_number>=100)
{   in >> number_to_compare;

if(first_number==number_to_compare)
{   first_number--;
if (first_number == second_number)
first_number--;
}
else if(second_number==number_to_compare)
{   second_number--;
if (second_number == first_number)
second_number--;
}
}


I would need in bouth of the mainif operators cases a line code that makes the search to start again from the beginning of the text file. I'm just starting to learn programming, I don't know any OOP. I tried also something similar with the logic of bubble sort algorithm (loop in loop) but I think that it's not working because the file reading will not start again from the beginning of the file for a new iteration of the main while ... so it's almost the same problem ... how can I make the program to start again to read the text file from the beginning? Here is the second attempt:

int need_to_search = 1;

while (need_to_search)
{   need_to_search = 0;

while (!in.eof() && first_number >= 100 && second_number >= 100)
{   in >> number_to_compare;

if (first_number == number_to_compare)
{   first_number--;
if (first_number == second_number)
first_number--;

need_to_search = 1;
}
else if (second_number == number_to_compare)
{   second_number--;
if (second_number == first_number)
second_number--;

need_to_search = 1;
}
}
}


Example: If the file would contain the folowing numbers:

1292 5422 997 125 7735 1 5474 956 7557 1249 966 624 12 5499 5677 884 964 998 546 455 778 6658 957

The output should be:

999 996

because these are the largest two numbers of three digits that are missing from the text file.

I would really appreciate a hint! Thanks!

• First things first: pick a language! Do you want C or C++? They look similar and some constructs work in both languages, but that doesn't make them identical. They're really, really different languages. – Mast May 12 '16 at 21:59
• I know what you are saying... I'm writing in the C++ that I have learned until now. I heard that until this stage what I'm using (like other beginners) is a C with cout and cin...this is what I know so far. It's how students usually start to learn C++ in highschool ... looks more like C code. – CCBet May 12 '16 at 22:05
• @David Foerster, I added a Input/Output example. Thanks! – CCBet May 12 '16 at 22:40
• @Skyp89, he meant a code :) Just show us what happens inside the main function, which is not touching your algorithm. For example calling the function you wrote, or creating an instance of data structure and illustrating what it can do. Simplified: show us the full code that you used to get output you posted. – Incomputable May 12 '16 at 22:54
• Why is iostream::eof inside a loop condition considered wrong? – Martin York May 12 '16 at 23:24

If you're interested in learning OOP and don't have the ambition to work with embedded electronics (roughly everything smaller than a phone), you're really more interested in C++ than C. This review is written based on the assumption you want to write C++ and ignores C.

Your current method of reading a file is odd. While checking for EOF is a relatively easy method to check whether you've hit the end of a file yet, it's not the only thing you should check for when reading a file. C++ can check whether the input is still 'good'. You could do so like this:

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

{
std::ifstream input("file.txt", std::ios::in);
std::string currentLine;
std::vector<std::string> allLines;
std::vector<std::string>::iterator itLines;
while (input.good())
{
getline(input, currentLine);
allLines.push_back(currentLine);
}
for (itLines = allLines.begin(); itLines != allLines.end(); itLines++)
{
// Iterate over the data you just read
}

}


Note that since we don't return anything, a void will do.

Now you're reading all lines of data (up till every newline) and iterate over them, which appears to be what you want to do. Not only will this stop when the file has reached EOF, it will also check whether the stream hasn't failed or took a bad chunk somewhere along the way.