Biblioteca estándar (Standard Library)
La biblioteca estándar C, también conocida como ISO C Library es una colección de macros, tipos y funciones para tareas como de entrada y salida, manejo de cadena de caracteres, manejo de memoria, operaciones matemática y operaciones relacionada al sistema operativo. Base a esta librería existe la librería estándar C++ esta incluye conjuntos de clases de plantilla el cual nos provee generar y manejar datos estructurados comunes como listas, arreglo, pilas, algoritmos, interacciones entre otras cosas.
Las biblioteca que necesitamos incluir en nuestra cabecera para utilizar elemento de la biblioteca estandar son las siguientes #include <cstdio>
, #include <cstdlib>
, #include <cstring>
e #include <cerrno>
.
#include <cstdio>
using namespace std;
// Read buffer size
const int maxString = 1024;
int main()
{
// File name
const char *fn = "test.txt";
// String to add inside the file
const char *str = "This is a literal C-string.\n";
/*
* Function to opens a file - fopen(@param1, @param2)
* @param1 Specifies the filename
* @param2 Specifies how to open the file
* FILE is a object type define on STD library
*/
FILE *fw = fopen(fn, "w");
printf("Writing file\n");
for(int i = 0; i < 5; i++) {
/*
* Funtion to write a string - fputs(@param1, @param2)
* @param1 Pointer to an character array that store the string
* @param2 Output file stream to write the characters
*/
fputs(str,fw);
}
// Function closes the file that is being pointed
fclose(fw);
printf("Done writing file.\n");
// Read the file
printf("Reading file\n");
char buf[maxString];
FILE *fr = fopen(fn, "r");
while(fgets(buf,maxString, fr)) {
fputs(buf, stdout);
}
fclose(fr);
// Delete file
remove(fn);
printf("done.\n");
return 0;
}