I could tell the difference between direct and sequential files in C ++

2

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

    
asked by shadowkira_123 11.05.2016 в 22:47
source

1 answer

3

I do not know what you mean by "direct files"; but regarding sequential files it is not a file type but a way to access files.

  

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, it is the only way to access the data, for example, on a magnetic tape.

     

It can also be the chosen access method, to simply process a sequence of data in order.

Source: Wikipedia .

To read files in C ++ either sequentially or randomly, I think the best option is to use streams of Files ( FILE is from , but 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 reading pointer, so with these functions the reading would be sequential, these functions would be:

You can manipulate the position of the reading pointer before or after reading, if between readings you move the pointer the reading 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);
}
    
answered by 12.05.2016 в 09:14