Reading from text files in C/C++ 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()&amp;&amp;first_number&gt;=100&amp;&amp;second_number&gt;=100)
    {   in &gt;&gt; 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() &amp;&amp; first_number &gt;= 100 &amp;&amp; second_number &gt;= 100)
        {   in &gt;&gt; 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!
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 &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;

void readFile()
{
    std::ifstream input("file.txt", std::ios::in);
    std::string currentLine;
    std::vector&lt;std::string&gt; allLines;
    std::vector&lt;std::string&gt;::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.