Could you tell me the difference between direct and sequential files in C++

What is the difference between direct and sequential files? If you can give me an example of each with the syntaxes they carry in C++ please.

 2
Author: shadowkira_123, 2016-05-11

1 answers

I don't know what you mean by "direct files"; but with respect to sequential files it is not a file type but a file access mode.

In Computer Science, sequential access means that a group of elements is accessed in a predetermined sequential order (one record at a time).

Sequentially, sometimes, is the only way to access data, for example, on a magnetic tape.

Can also be the method to simply process a sequence of data in order.

Source: Wikipedia .

Source: [Wikipedia](https://es.wikipedia.org/wiki/Acceso_secuencial)


To be able to read files in C++ either sequentially or randomly, I think the best option is to use file streams (FILE it is c , although also usable in C++).

C++ file streams have several functions that allow you to read data from the file. Most of these functions automatically move forward the data read pointer, so with these functions the Read would be sequential, these functions would be:

You can manipulate the position of the read pointer before or after performing a read, If between reads you move that Pointer The Read will be random instead of sequential; the functions that manipulate this pointer are:

You can know the current position of the reading pointer with tellg .


I add a simple example of use, of reading files, is using the library <fstream>:

// Crea un stream de archivo de lectura.
// "i" corresponde a lectura (input)
// "f" corresponde a archivo (file)
// si el stream fuese "o" lo abriria como escritura (ofstream)
std::ifstream archivo{"archivo.txt"};

// Comprueba si esta abierto, de ser asi: sigue.
if (archivo.is_open())
{
    int valor{};
    std::string texto{};

    // Lee secuencialmente un int y un string
    archivo >> valor;
    archivo >> texto;

    // Rebobina a la posicion donde se acaba el dato "valor"
    archivo.seekg(sizeof(valor));
    char letra{};
    // Lee un caracter.
    arthivo.read(&letra, 1);
}
 3
Author: PaperBirdMaster, 2016-05-12 15:32:15