- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
- #define MYFILE "missing.txt"
- int main( )
- {
- FILE* fin;
- fin=fopen( MYFILE,"r" );
- if( fin==(FILE*)NULL )
- {
- printf( "%s: %s\n",MYFILE,strerror( errno ) );
- exit( -1 );
-
- }
- fclose( fin );
- return 0;
-
-
- }
-
- #include <stdio.h>
- #include <errno.h>
- int main( )
- {
- int i=0;
- FILE* fout;
- const char string[ ]={
- "This\r\nis a test\r\nfile.\r\n\0"
- };
- fout=fopen("inpfile.txt","w");
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n","inpfile.txt",strerror( errno ) );
- exit( -1 );
- }
- while( string[ i ]!=NULL )
- {
- fputc( ( int )string[ i ],fout );
- ++i;
- }
- fclose( fout );
- return 0;
- }
- #include <stdio.h>
- #include <errno.h>
- int main( )
- {
- FILE* fin;
- fin=fopen( "inpfile.txt","r" );
- if( fin==( FILE* )NULL )
- {
- printf( "%s: %s\n","inpfile.txt",strerror( errno ) );
- exit( -1 );
- }
- int i;
- while( (i=fgetc( fin ))&&i!=EOF )
- {
- printf( "%c",(char)i );
-
- }
- return 0;
-
- }
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #define LEN 80
- int main( )
- {
- char line[ LEN+1 ];
- FILE* fout;
- FILE* fin;
-
- fout=fopen( "testfile.txt","w" );
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n","testfile.txt",strerror( errno ) );
- exit( -1 );
-
- }
-
- fin=fdopen( 0,"r" );
- printf( "Please input some characters,end with CTRL+D:\n" );
-
- while( ( fgets(line,LEN,fin) )!=NULL )
- {
- fputs( line,fout );
-
- }
- fclose( fout );
- fclose( fin );
-
- printf( "\nthe characters read from file is:\n" );
- char getLine[ LEN+1 ];
- fin=fopen( "testfile.txt","r" );
- if( fin==(FILE*)NULL )
- {
- printf( "%s: %s\n","testfile.txt",strerror( errno ) );
- exit( -1 );
- }
- fgets( getLine,LEN,fin );
- printf( "%s\n",getLine );
- fclose( fin );
-
- return 0;
- }
- #include <stdio.h>
- #include <errno.h>
- #define MAX_LINE 40
- #define FILENAME "myfile.txt"
- typedef struct
- {
- int id;
- float x_coord;
- float y_coord;
- char name[ MAX_LINE+1 ];
-
- }MY_TYPE_T;
- #define MAX_OBJECTS 3
- MY_TYPE_T objects[ MAX_OBJECTS ]={
- {0,1.5,8.3,"Frist-object"},
- {1,4.5,6.3,"Second-object"},
- {2,3.5,7.5,"Thrid-object"},
- };
- int main( )
- {
- int i;
- FILE* fout;
- fout=fopen(FILENAME,"w" );
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
- printf( "Writing...\n" );
- for( i=0;i<MAX_OBJECTS;++i )
- {
- fprintf( fout,"%d %f %f %s\n",objects[ i ].id,objects[ i ].x_coord,
- objects[ i ].y_coord,objects[ i ].name);
-
- }
- fclose( fout );
- printf("Write over\n");
- printf("Read from file\n");
- FILE* fin=fopen( FILENAME,"r" );
- if( fin==(FILE*)NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
-
- }
- MY_TYPE_T objects;
- while( !feof(fin) )
- {
- fscanf( fin,"%d %f %f %s\n",&objects.id,&objects.x_coord,
- &objects.y_coord,&objects.name);
- printf("%d %f %f %s\n",objects.id,objects.x_coord,
- objects.y_coord,objects.name);
-
- }
- fclose( fin );
- return 0;
- }
- #include <stdio.h>
- #include <errno.h>
- #define MAX_LINE 40
- #define FILENAME "myfile.bin"
- typedef struct
- {
- int id;
- float x_coord;
- float y_coord;
- char name[ MAX_LINE+1 ];
-
- }MY_TYPE_T;
- #define MAX_OBJECTS 3
- MY_TYPE_T objects[ MAX_OBJECTS ]={
- {0,1.5,8.3,"Frist-object"},
- {1,4.5,6.3,"Second-object"},
- {2,3.5,7.5,"Thrid-object"},
- };
- int main( )
- {
- int i;
- FILE* fout;
- fout=fopen(FILENAME,"w" );
- if( fout==( FILE* )NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
- printf( "Writing...\n" );
- fwrite( ( void* )objects,sizeof( MY_TYPE_T ),3,fout );
- fclose( fout );
-
- printf( "Read from file\n" );
- MY_TYPE_T object;
- FILE* fin=fopen( FILENAME,"r" );
- if( fin==( FILE* )NULL )
- {
- printf( "%s: %s\n",FILENAME,strerror( errno ) );
- exit( -1 );
- }
-
- fseek( fin,( 2*sizeof( MY_TYPE_T ) ),SEEK_SET );
- fread( &object,sizeof( MY_TYPE_T ),1,fin );
- printf("%d %f %f %s\n",object.id,object.x_coord,
- object.y_coord,object.name);
-
- fseek( fin,( 1*sizeof( MY_TYPE_T ) ),SEEK_SET );
- fread( &object,sizeof( MY_TYPE_T ),1,fin );
- printf("%d %f %f %s\n",object.id,object.x_coord,
- object.y_coord,object.name);
-
- rewind( fin );
- fread( &object,sizeof( MY_TYPE_T ),1,fin );
- printf("%d %f %f %s\n",object.id,object.x_coord,
- object.y_coord,object.name);
- fclose( fin );
- return 0;
-
- }