fstreams

= **__ fstreams __ ** = =﻿=

Historia del c++ **C++ ** es un [|lenguaje de programación] diseñado a mediados de los años 1980 por [|Bjarne Stroustrup]. La intención de su creación fue el extender al exitoso [|lenguaje de programación] [|C] con mecanismos que permitan la manipulación de [|objetos]. En ese sentido, desde el punto de vista de los [|lenguajes orientados a objetos], el C++ es un lenguaje híbrido. Posteriormente se añadieron facilidades de [|programación genérica], que se sumó a los otros dos paradigmas que ya estaban admitidos ([|programación estructurada] y la [|programación orientada a objetos] ). Por esto se suele decir que el C++ es un [|lenguaje de programación multiparadigma].

Biblioteca ifstream <span style="background-color: #ffffff; color: #404040; display: block; font-family: 'Arial','sans-serif'; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"> <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: #ffffff; color: #404040; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;">En los siguientes apartados conoceremos los pasos a seguir para poder abrir los ficheros: <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: #ffffff; color: #404040; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;">Como abrir un fichero: (nombre_variable_fichero).open("nombre_fichero.dat/txt",ios::in); para abrirlo en modo lectura. (nombrevariablefichero).open("nombre_fichero.dat/txt",ios::out); para abrirlo en modo escritura. <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="font-family: Arial,Helvetica,sans-serif;">**<span style="background-color: transparent; color: #000000; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Ejemplo: ** <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">f.open("datos.txt",ios::in); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Como cerrar el fichero: nombre_variable_fichero.close; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Ejemplo: f.close;

<span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="font-family: Arial,Helvetica,sans-serif;">**<span style="background-color: #ffffff; color: #000000; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;">EJEMPLO : ** <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Leer un fichero: <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">1-Si es fichero de texto plano: <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">#include <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">#include <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">#include <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">using namespace std; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">int main { <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">ifstream entrada; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">entrada.open("textoPlano.txt"); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">string unString; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">while(entrada >> unString) <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">cout << "Lei: " << unString << endl; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">return 0; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">} <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">2-Si es un fichero binario(.dat); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">nombre_variable_fichero.read((char*)&nombre_variable,sizeof(tipo_variable)); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="font-family: Arial,Helvetica,sans-serif;">**<span style="background-color: transparent; color: #000000; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Ejemplo : ** <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">f.read((char*)&e,sizeof(int)); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Escribir un fichero: <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">1-Si es fichero de texto(.txt): <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">nombrevariable<<"texto"; donde "texto" puede ser también una variable de cualquier tipo primitivo, o un string.

<span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="font-family: Arial,Helvetica,sans-serif;">**<span style="background-color: transparent; color: #000000; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Ejemplo : ** <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"> <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">f<<HOLA; <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">2-Si es un fichero binario(.dat); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">nombre_variable_fichero.write((char*)&nombre_variable,sizeof(tipo_variable)); <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">Ejemplo: <span style="background-color: transparent; color: #000000; display: block; font-family: Times New Roman; font-size: 16px; line-height: normal; margin-bottom: 0pt; text-align: justify; text-decoration: none; vertical-align: auto;"><span style="background-color: transparent; color: #000000; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: justify; text-decoration: none; vertical-align: auto;">f.write((char*)&e,sizeof(int));