RE:problemas con ficheros
#include< stdio.h >
#include< string.h >
int main()
{
char * path = "c:\dat.txt";
FILE *f;
char text[15];
// abrir fichero ( r )....
f = fopen( path,"r" );
if( f == NULL ) // si el fichero no existe retorna NULL(0) y mostramos un mensaje.
{
printf( "no se encontrxA2 el archivo...
" ); // xA2 = ó.
printf( "se crearxA0 uno...
" ); // xA0 = á.
// crear fichero nuevo ( w )....
f = fopen( path,"w" );
strcpy( text, "100001 3000003" ); // escribimos dos valores en una cadena(texto).
fwrite( &text, 1, strlen(text), f );
fclose( f );
// abrir fichero nuevo ( r )....
f = fopen( path,"r" );
}
int a, b, total;
// fscan:
// Com fscan el contenido de fichero debe ser solo texto o sino obtendremos 'basura'.
// El valor de retorno es el total de campos leidos de otra forma EOF = -1.
// total = fscan( file, "%%%...", campo1, campo2, campo3, etc... );
total = fscanf( f, "%d %d", &a, &b ); // leemos dos valores.
if( total == -1 ) //EOF
{
printf( "el fichero %s no contiene nada...
", path );
return 0;
}
if( total > 0 )
printf( "valor_a = %d
", a );
if( total > 1 )
printf( "valor_b = %d
", b );
fclose( f );
return 0;
}