<stdio.h>
1)fopen,fclose
//01fopen.c
#include <stdio.h>
int main(void){
FILE *fp=NULL;
fp=fopen("file.c","w");//打开只写文件,不存在则新建
if(fp==NULL){
return -1;
}
fclose(fp);
fp=NULL;
fp=fopen("file.c","a");//以追加方式打开文件,不存在则新建
if(fp==NULL){
return -1;
}
fclose(fp);
fp=NULL;
fp=fopen("file.c","r");//只读方式打开,文件必须存在,否则失败
if(fp==NULL){
return -1;
}
fclose(fp);
fp=NULL;
return 0;
}
2)
int fprintf(FILE *stream, const char*format, ...);int fscanf(FILE *stream, const char *format, ...);
fprintf是从标准输入设备往文件里写入数据
fscanf是从文件里往标准输出设备里写入数据
//02fio.c
#include <stdio.h>
int main(void){
FILE *fp=NULL;
fp=fopen("file.c","w");
if(fp==NULL){
return -1;
}
int a=10;
float f=12.34;
double d=12345.6789;
char c='a';
fprintf(fp,"%d%f%lf%c",a,f,d,c);//用格式化的方法将4个变量写入文件
fclose(fp);
fp=NULL;
fp=fopen("file.c","r");
if(fp==NULL){
return -1;
}
int a1;
float f1;
double d1;
char c1;
//用格式化方法从文件file.c中读入4个数据到这4个新的变量中。
fscanf(fp,"%d%f%lf%c",&a1,&f1,&d1,&c1);//
printf("%d%f%lf%c",a1,f1,d1,c1);
fclose(fp);
fp=NULL;
return 0;
}
输出:
10120.3400000.678900a
此时file.c里的内容为:
1012.34000012345.678900a
3)
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
[注:size_t为long unsigned int]
//03frw.c
#include <stdio.h>
#include <string.h>
struct student{
int id;
char name[20];
char address[100];
};
int main(void){
FILE *fp=NULL;
fp=fopen("file.c","w");
if(fp==NULL)
return -1;
int a=10;
double d=12345.6789;
char str[1024]="This is a string.";
//将变量写入文件
fwrite(&a,sizeof(int),1,fp);
fwrite(&d,sizeof(double),1,fp);
int len=strlen(str)+1;
fwrite(&len,sizeof(int),1,fp);
fwrite(str,sizeof(char),len,fp);
//将结构体写入
struct student stu={1000,"zhangfei","南京"};
fwrite(&stu,sizeof(struct student),1,fp);
fclose(fp);
fp=NULL;
fp=fopen("file.c","r");
if(fp==NULL)
return -1;
int a1;
double d1;
char str1[1024];
//从文件中读取数据放在变量中
fread(&a1,sizeof(int),1,fp);
fread(&d1,sizeof(double),1,fp);
int len1;
fread(&len1,sizeof(int),1,fp);
fread(str1,sizeof(char),len,fp);
printf("%d%f%d%s\n",a1,d1,len1,str1);
//读取结构体
struct student stu1;
fread(&stu1,sizeof(struct student),1,fp);
printf("%d %s %s\n",stu1.id,stu1.name,stu1.address);
fclose(fp);
fp=NULL;
return 0;
}
输出:
1012345.67890018This is a string.
1000 zhangfei 南京
此时file.c里的内容为:
[空行]
^@^@^@¡ø1æÖ^\È@^R^@^@^@This is a string.^@è^C^@^@zhangfei^@^@^@^@^@^@^@^@^@^ @^@^@å<8d><97>京^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ @^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^ @^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@
fprintf函数读取数字时,文件里是正确格式; 而fread函数读取数字时,文件里是乱码,不知何故。
二者输出的文件内容都是正确格式。4)
int fseek(FILE *stream, longoffset, intwhence);
long ftell(FILE *stream);
//05fseek.c
#include <stdio.h>
#include <string.h>
int main(){
FILE *fp=NULL;
fp=fopen("file.c","w+");
if(fp==NULL)
return -1;
char buf[]="abcdefghijklmnopqrstuvwxyz";
fwrite(buf,sizeof(char),26,fp);
//用ftell()获取文件大小
fseek(fp,0,SEEK_END);
long len=ftell(fp);
printf("文件长度为:%ld个字节\n",len);
fseek(fp,0,SEEK_SET);//指针定位到文件头开始的的一个字节
memset(buf,0,26);//将buf数组中放入26个字符'\0',即清空
fread(buf,sizeof(char),10,fp);
printf("%s\n",buf);
fseek(fp,5,SEEK_CUR);//文件指针定位到当前位置向后偏移5个字节的位置
memset(buf,0,26);
fread(buf,sizeof(char),10,fp);
printf("%s\n",buf);
fseek(fp,-10,SEEK_END);//文件指针定位到文件尾向前偏移10个字节的位置
memset(buf,0,26);
fread(buf,sizeof(char),10,fp);
printf("%s\n",buf);
fclose(fp);
fp=NULL;
return 0;
}
while((ch=getc(fp))!=EOF)
putc(ch,fp1);
6)可变长参数的使用
<stdarg.h>
void va_start(va_list ap, last);
type va_arg(va_list ap, type);
void va_end(va_list ap);
voidva_copy(va_list dest, va_list src)#include <stdio.h>
#include <stdarg.h>
int max(int cnt,...)
{
va_list v;//定义可变长参数表V
va_start(v,cnt);//用函数va_start将参数cnt之后的那些参数保存到v中
//使用函数va_arg从参数表中取一个int 类型参数放入变量maxvalue
//中,并设maxvalue保存的是所有参数中的最大值
int maxvalue=va_arg(v,int);
//循环遍历cnt后面的参数
for(int i=1;i<cnt;i++){
int data=va_arg(v,int);
if(data>maxvalue)
maxvalue=data;
}
va_end(v);//释放可变长参数表v
return maxvalue;
}
int main(){
int maxNum=max(10,12,23,1,2,3,4,5,6,7,45);
printf("最大数为:%d\n",maxNum);
return 0;
}