This question is probably gonna get one of those marks, but I still want to ask and hope for an answer, which is likely very simple to more knowledgeable programmers.
So in C++, there's getline() function that just reads line after line coupled with a while loop. In PHP, the database query results can be fetched by mysql_fetch_row, etc... and there are probably many more examples (and if you know, listing them in your answer would definitely get my up vote). So I'd like to know how these functions just know to go to the next line or result? There's some sort of built-in iterator and the functions keep track and update?
Descriptive answers are much appreciated.
For file reads there is an EOF
(End Of File) marker or is_eof(pointer)
function to detect file end (they are commonly linked to OS core internals). File lines are marked by system EOL
delimiter ,
or
.
For database resultset reads there is a common API, provided by database developers, that allowing to use almost same functionality, i. e. "Is End Of Resultset" or "Go To Line". In php (+MySQLi) for example, there is an mysqli_data_seek()
function or method.
So, the common loop of reading, might looks like the following pseudocode:
handle = getResourceHandle(args); // create a pointer to resource
while(not isEOF(handle)){ // while end of resource is not reached
row = getRow(handle); // read current row
// some processing
nextRow(handle); // move system row pointer to the next row
}
closeResourceHandle(handle); // free pointer to resource
Previous example shows functional-approach, however for OOP a lot of details are encapsulated:
Resource r = new Resource(args); // getResourceHandle() + other operations
while(not r->isEnd()){ // while not isEOF(handle)
row = r->getRowAndMoveToNext(); // getRow(handle) + nextRow(handle);
// some processing
}
destroy r; // closeResourceHandle(handle) + other operations
So in the end, for functional approach on a "low level programming", you "have to" keep track of rows / lines and check the resource to be non-empty by yourself. In OOP most common operations are already done by providers for you.