eg:fputs和fputc函数//c库函数
#include<stdio.h>
#include<string.h>
int main()
{
FILE *fp=fopen("f1.txt","w");
if(fp==NULL){
printf("fopen error\n");
return -1;
}
#if 1
char str[32]="welcome to shanghai";
fputs(str,fp);//fputs读取整行字符
fclose(fp);
#else
fputc //fputc读取单个字符
#endif
}
eg: fpintf()和fscanf() //c库
#include<stdio.h>
#include<string.h>
typedef struct student
{
char name[32];
int age;
char sex;
}STU;
int main()
{
#if 1
int age;
char name[32];
char sex;
FILE * fp=fopen("fprintf.txt","w");
STU stu={"小明",21,'M'};
fprintf(fp,"%s\t%d\t%c\t",stu.name,stu.age,stu.sex);
fclose(fp);
#else
FILE *fp=fopen();
#endif
}
eg:fseek() ftell()
#include<stdio.h>
int main()
{
FILE *fp=fopen("f3.txt","r");
fseek(fp,0,SEEK_END);//指针偏移量,0:偏移一个字节,SEEK_END:从末尾开始偏移 -是往前移,+是往后移
int ret=ftell(fp);//以文件首为参照物,指针现在的位置,返回值是从文件首到目前多少个字节
printf("ret=%d\n",ret);
fclose(fp);
网友评论