Fileクラスファイル読み書き関数の例
#include<stdio.h>
intmain(
void
)
{
FILE
*in,*out;
in=
fopen
(
"\\AUTOEXEC.BAT"
,
"rt"
);
if
(in==NULL)
{
fprintf
(stderr,
"Cannotopeninputfile.
"
);
return 1;
}
out=
fopen
(
"\\AUTOEXEC.BAT"
,
"wt"
);
if
(out==NULL)
{
fprintf
(stderr,
"Cannotopenoutputfile.
"
);
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=
'
'
;
stream=
fopen
(
"fprintf.out"
,
"w"
);
fprintf
(stream,
"%s%c"
,s,c);
fprintf
(stream,
"%d
"
,i);
fprintf
(stream,
"%f
"
,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
"
,i);
fprintf
(fp,
"%s"
,s);
fclose
(fp);
return0;
}
1
2 total:617
thatisagoodnew