.eof() confusion

Hi,

I am having a bit of confusion understanding the full extent to which I can use the .eof() function from the iostream library. Can this function be used on any input type? In other words, can this unction be used outside of a ifstream object? Thank you in advance for the assistance.
It tells you if the stream has detected an End Of File condition. The file can be at the end of stream before it's detected. And it makes no sense to consider EOF without a stream.
I guess what I am getting confused with is how does this work if I'm not reading from a file but instead reading from an alternative source input like a keyboard. is the eof() uneffected by the type of istream object? In other words, does .eof() only work with ifstream objects or can it work with cin as well ? Thanks again for the clarification.
hint: eof() stands for end-of-file
i understand but I didn't know if it had a broader range of use beyond reading from a file. I get its the end-of-file function but can this be used to read the end of a stream from user input from a keyboard?

This is why I'm asking

https://www.hackerrank.com/challenges/30-dictionaries-and-maps

After the lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input.
Last edited on
In other words, can this function be used outside of a ifstream object?

Look up the member function in your favorite reference:
http://en.cppreference.com/w/cpp/io/basic_ios/eof
Following any appropriate link brings you to a page titled std::basic_ios::eof
The implication is that you can call eof() on any subtype of std::basic_ios, including std::istream (the type of std::cin).

To reach the end of file on a stream means that there is no more input available on the stream. This happens naturally when using a command line program noninteractively (e.g., as part of a filter) as designed. You can also type EOF at a terminal with Ctrl+D (or maybe something else on Windows.)
Last edited on
A stream is a generalization of a file. The idea is a file is logical entity held on a physical device. It may be support sequential or random access, depending on what media it's held on (tape or disk for example).

A stream is generic sequence of bytes with a source and a sink. A sequential file is just a kind of stream (from the C++'s iostream library's point of view).

When the stream ends, that sets the EOF condition. How that works depends on the stream. If the stream is a TCP stream, then when the server shuts down it's write side of the connection, the TCP client will detect that (as read returning zero) and sets the state.

Similarly, when the stream is a file, the underlying filesystem calls goes into EOF state and the stream on top of it detects that the next time it tries to read from it.
I don't know if this is what you are asking or not but unix and dos console both support typing files from the console, like the old dos copy con trick or in unix I think you can do it with 'cat'. The user presses a special key combo (control M, maybe? control z? I forget, its been like 100 years since I did this) .

Which comes back to the point ... I suppose if the user were able to type the special eof signal at the keyboard, the c program would see it as such. I am not sure if this was something the cat/copy/etc programs did inside (they changed a user typed obscure symbol to real eof??) or if the OS did something (os changes the signal to EOF and sends the signal to program??) or if the keyboard combo actually represents the real EOF character (seems unlikely). You could try it, if you wanted to dig around to see the user supplied combo sends an EOF to your program. This seems like a weird, 1982ish thing to be doing, though.



This is why I'm askingĀ 

https://www.hackerrank.com/challenges/30-dictionaries-and-maps

After the lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a to look up, and you must continue reading lines until there is no more input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>
# include <sstream>

struct PhoneBook
{
    std::string m_name;
    double m_number;

    PhoneBook(const std::string& name, const double number)
    : m_name(name), m_number(number){}
};
std::ostream& operator << (std::ostream& os, const PhoneBook& p)
{
    os << p.m_name << "=" << p.m_number << "\n";
    return os;
}

int main()
{
    std::vector<PhoneBook> directory{};
    std::cout << "how many entries \n";
    std::size_t num{};
    std::cin >> num;
    std::cin.ignore();
    size_t i{};
    while (i < num)
    {
        std::cout << "enter name and number for entry " << i + 1 << " of " << num << "\n";
        std::string entry{};
        getline(std::cin, entry);
        std::istringstream stream{entry};
        std::string name{};
        double number;
        stream >> name >> number;
        if(stream)
        {
            directory.emplace_back(name, number);
        }
        ++i;
    }
    std::vector<std::string> queries{};
    do
    {
        std::string line{};
        getline(std::cin, line);
        if (line == "")break;
        queries.push_back(line);
    }while (true);
    //https://stackoverflow.com/questions/28269611/understanding-whiletrue-loop
    //to avoid an infinite loop in theory ( if user keeps entering one non-empty string after another)       
//an additional size restriction on the vector queries can be imposed but, also in theory,
    //a very large number of std::strings in a std::vector can be handled 

    for (const auto& elem : queries)
    {
        auto itr = std::find_if(directory.cbegin(), directory.cend(), [&elem](const PhoneBook& p)
                                { return p.m_name == elem;});
        if (itr != directory.cend())
        {
            std::cout << *itr;
        }
        else
        {
            std::cout << "Not found \n";
        }
    }
}

also data validation throughout is recommended
Last edited on
Topic archived. No new replies allowed.