File类文件读写函数案例


#include<stdio.h>
intmain( void )
{
FILE *in,*out;
in= fopen ( "\\AUTOEXEC.BAT" , "rt" );
if (in==NULL)
{
fprintf (stderr, "Cannotopeninputfile.\n" );
return 1;
}
out= fopen ( "\\AUTOEXEC.BAT" , "wt" );
if (out==NULL)
{
fprintf (stderr, "Cannotopenoutputfile.\n" );
return 1;
}
while (! feof (in))
fputc ( fgetc (in),out);
fclose (in);
fclose (out);
return0;
}



#include<stdio.h>
#include<stdlib.h>
#include<process.h>
FILE *stream;
int main( void )
{
int i=10;
double fp=1.5;
chars[]= "thisisastring" ;
charc= '\n' ;
stream= fopen ( "fprintf.out" , "w" );
fprintf (stream, "%s%c" ,s,c);
fprintf (stream, "%d\n" ,i);
fprintf (stream, "%f\n" ,fp);
fclose (stream);
system ( "typefprintf.out" );
return0;
}



屏幕输出:
1
2
3
thisisastring
10
1.500000

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
intmain()
{
FILE *fp;
inti=617;
char *s= "thatisagoodnew" ;
fp= fopen ( "text.dat" , "w" );
fputs ( "total" ,fp);
fputs ( ":" ,fp);
fprintf (fp, "%d\n" ,i);
fprintf (fp, "%s" ,s);
fclose (fp);
return0;
}

1
2
total:617
thatisagoodnew

你可能感兴趣的:(C++,c,应用程序,Visual,Studio,2010)