Aquí, vamos a aprender tellg (), seekg () y () seekp funciones obras en C ++ Programming Language? programa en C ++ para demostrar ejemplo de tellg (), seekg () y seekp () funciones .
tellg () , seekg () y seekp () funciones se utilizan para establecer / obtener la posición de get y punteros puesto en un archivo mientras que la lectura y la escritura.
sintaxis:
// tellg()
streampos tellg();
// seekg()
istream& seekg (streampos pos);
istream& seekg (streamoff off, ios_base::seekdir way);
// seekp()
stream& seekp (streampos pos);
ostream& seekp (streamoff off, ios_base::seekdir way);
Aquí,
- pos – representa la nueva posición absoluta dentro de la corriente (desde el principio).
- de – representa el desplazamiento a buscar.
- pos – representa las siguientes constantes,
- ios_base :: BEG / ios :: beg – Inicio de la corriente.
- ios_base :: cur / ios :: cur – posición actual en la corriente.
- ios_base :: terminar / ios :: end – poner fin a la corriente.
programa C ++ para demostrar el ejemplo de tellg (), seekg () y seekp ()
En el siguiente programa, estamos utilizando un ‘my.txt’ archivo, el archivo contiene la siguiente texto,
del archivo: my.txt
IncludeHelp is specially designed to provide help to students,
working professionals and job seekers
programa:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
fstream F;
// opening a file in input and output mode
F.open("my.txt", ios::in | ios::out);
// getting current location
cout << F.tellg() << endl;
// seeing 8 bytes/characters
F.seekg(8, ios::beg);
// now, getting the current location
cout << F.tellg() << endl;
// extracting one character from current location
char c = F.get();
// printing the character
cout << c << endl;
// after getting the character,
// getting current location
cout << F.tellg() << endl;
// now, seeking 10 more bytes/characters
F.seekg(10, ios::cur);
// now, getting current location
cout << F.tellg() << endl;
// again, extracing the one character from current location
c = F.get();
// printing the character
cout << c << endl;
// after getting the character,
// getting current location
cout << F.tellg() << endl;
// again, seeking 7 bytes/characters from beginning
F.seekp(7, ios::beg);
// writting a character 'Z' at current location
F.put('Z');
// now, seeking back 7 bytes/characters from the end
F.seekg(-7, ios::end);
// now, printing the current location
cout << "End:" << F.tellg() << endl;
// extracting one character from current location
c = F.get();
// printing the character
cout << c << endl;
// closing the file
F.close();
return 0;
}
salida
0
8
e
9
19
i
20
End:93
s
Después de la ejecución del programa es el contenido del archivo,
IncludeZelp is specially designed to provide help to students,
working professionals and job seekers