UNIVERSIDAD AUTONOMA METROPOLITANA Programacin Estructurada Tema ARCHIVOS Archivo

  • Slides: 9
Download presentation
UNIVERSIDAD AUTONOMA METROPOLITANA Programación Estructurada Tema: ARCHIVOS

UNIVERSIDAD AUTONOMA METROPOLITANA Programación Estructurada Tema: ARCHIVOS

Archivo: Conjunto de Bytes almacenados en memoria secundaria.

Archivo: Conjunto de Bytes almacenados en memoria secundaria.

 • 1. Archivos son objetos quedan vivos después de la muerte de un

• 1. Archivos son objetos quedan vivos después de la muerte de un programa. • 2. Trabajar con archivos involucra diferentes operaciones de entrada/salida (E/S) – lectura/escritura (R/W)

Manejo de Archivos #include <stdio. h> #include <stdlib. h> int main(){ FILE *f 1;

Manejo de Archivos #include <stdio. h> #include <stdlib. h> int main(){ FILE *f 1; 0111 1001 //Apuntador o descriptor de un archivo 1111 1001 0010 1100 0001 EOF Fin de Archivo

f 1=fopen(“nombre del archivo”, “mode”); mode Descripción Abre un archivo para lectura. El archivo

f 1=fopen(“nombre del archivo”, “mode”); mode Descripción Abre un archivo para lectura. El archivo debe existir. “r” Crea (y abre) un archivo vacío para sólo escritura. Si ya existe un “w” archivo con el mismo nombre, se borra su contenido y es considerado como un nuevo archivo vacío. “a” Agregar contenido al final de un archivo. Si el archivo no existe lo crea. “r+” Abre un archivo para lectura y escritura, se posiciona al comienzo del archivo. El archivo debe existir. “w+” “a+” Crea (y abre) un nuevo archivo para lectura y escritura. Abre un archivo para agregar contenido y para lectura, si no existe lo crea.

#include <stdio. h> #include <stdlib. h> Crear archivo nuevo (mode: "w" ó "w+") y

#include <stdio. h> #include <stdlib. h> Crear archivo nuevo (mode: "w" ó "w+") y escribir texto en éste con fputs() int main(){ FILE *f 1; //Apuntador o descriptor de un archivo f 1= fopen("miarchivo. txt", "w+"); fputs("texto de prueba con fputs“, f 1); fclose(f 1); return 0; }

#include <stdio. h> #include <stdlib. h> Crear archivo nuevo (mode: "w" ó "w+") y

#include <stdio. h> #include <stdlib. h> Crear archivo nuevo (mode: "w" ó "w+") y escribir texto en éste con fprintf() int main(){ FILE *f 1; //Apuntador o descriptor de un archivo f 1= fopen("miarchivo 2. txt", "w+"); fprintf(f 1, "texto de prueba con fprintf"); fclose(f 1); return 0; }

#include <stdio. h> #include <stdlib. h> Leer texto desde un archivo (mode: "r" )

#include <stdio. h> #include <stdlib. h> Leer texto desde un archivo (mode: "r" ) leyendo 20 caracteres con fgets() int main(){ FILE *f 1; char buffer[50]; f 1= fopen("archivo. txt", "r"); fgets(buffer, 20, f 1); archivo. txt texto de prueba para fgets puts(buffer); fclose(f 1); return 0; } (el archivo debe existir en la carpeta del programa)

#include <stdio. h> #include <stdlib. h> Leer 3 palabras desde un archivo (mode: "r"

#include <stdio. h> #include <stdlib. h> Leer 3 palabras desde un archivo (mode: "r" ) con fscanf() int main(){ FILE *f 1; char buffer[50]; int i; f 1= fopen("archivo 2. txt", "r"); for(i=0; i<3; i++){ fscanf(f 1, "%s", buffer); puts(buffer); } fclose(f 1); return 0; } archivo 2. txt Eduardo Soto Quintana (el archivo debe existir en la carpeta del programa)