ficheros en C,ayuda por favor!!
hola!! Necesito a alguien que sepa manejar ficheros en C,tengo exam y no tengo ni idea de hacer un ejercicio...me pueden ayudar??
muxas gracias
muxas gracias
No se exactamente q necesita, enviame un correo con + info.Aki tienes un ejemplo de la ayuda de Boland q no es maolo mirala de vez en cuando.
Lo he comentado malamente.
#include <string.h>//para tratar con cadenas
#include <stdio.h>
int main(void)
{
FILE *stream;//puntero al fichero o archivo
char msg[] = "this is a test";
char buf[20];
if ((stream = fopen("DUMMY.FIL", "w+")) == NULL)//apertura para escritura
{
fprintf(stderr, "Cannot open output file.n");
return 1;
}
/* write some data to the file */
fwrite(msg, strlen(msg)+1, 1, stream);//para escrivir un texto en el archivo o bytes directamente
/* seek to the beginning of the file */
fseek(stream, SEEK_SET, 0);//posiciona el puntero al principio del archivo
/* read the data and display it */
fread(buf, strlen(msg)+1, 1, stream);
//leer del archivo
printf("%sn", buf);//imprime lo q acabas de leer
fclose(stream);//cierra el puntero al archivo
//siempre debes cerrarlo
return 0;
}
Lo he comentado malamente.
#include <string.h>//para tratar con cadenas
#include <stdio.h>
int main(void)
{
FILE *stream;//puntero al fichero o archivo
char msg[] = "this is a test";
char buf[20];
if ((stream = fopen("DUMMY.FIL", "w+")) == NULL)//apertura para escritura
{
fprintf(stderr, "Cannot open output file.n");
return 1;
}
/* write some data to the file */
fwrite(msg, strlen(msg)+1, 1, stream);//para escrivir un texto en el archivo o bytes directamente
/* seek to the beginning of the file */
fseek(stream, SEEK_SET, 0);//posiciona el puntero al principio del archivo
/* read the data and display it */
fread(buf, strlen(msg)+1, 1, stream);
//leer del archivo
printf("%sn", buf);//imprime lo q acabas de leer
fclose(stream);//cierra el puntero al archivo
//siempre debes cerrarlo
return 0;
}
