13文件操作

文件操作

1.打开,创建与关闭

1
2
3
FILE *fp;
fp = fopen("words.txt", "w")//以只写方式打开
fclose(fp);//关闭

2.读写方式

(1)字符方式读写

1‘ 字符方式读入

1
2
3
4
5
6
7
8
9
10
11
FILE *fp2;
while ((fp2 = fopen("words.txt", "r") )== NULL)
{
printf("error!!!\n");
exit(0);
}
while ((ch = getc(fp2)) != EOF)
{
putchar(ch);
}
fclose(fp2);

也可以使用判断是否到达结尾的函数 feof

1
2
3
4
5
6
7
8
9
10
11
12
13
FILE *fp2;
while ((fp2 = fopen("words.txt", "r") )== NULL)
{
printf("error!!!\n");
exit(0);
}
ch = fgetc(fp2);//fp指向ch所在位置,获取当前的ch
while (!feof(fp2))//若fp指向EOF,则函数返回0
{
putchar(ch);
ch = fgetc(fp2);// fp指向ch所在位置,获取当前的ch
}
fclose(fp2);

2’ 字符方式写入

1
2
3
4
5
6
7
8
9
10
11
12
13
    FILE *fp;
char ch;
while ((fp = fopen("words.txt", "w")) == NULL)
{
printf("error!!!\n");
exit(0);
}
printf("Now,you can input your words:\n");
while ((ch = getchar()) != '\n')
{
fputc(ch, fp);
}
fclose(fp);
(2)字符串方式读写

1‘ 字符串读入

1
2
3
4
5
6
7
8
9
10
11
    FILE *fp2;
char ch2[N];
while((fp2=fopen("words.txt","r"))==NULL)
{
printf("error\n");
exit(0);
}
printf("display:\n");
fgets(ch2,100,fp2);
puts(ch2);
fclose(fp2);

2’ 字符串写入

1
2
3
4
5
6
7
8
9
10
11
    FILE *fp;
char ch[N];
while((fp=fopen("words.txt","w"))==NULL)
{
printf("error!!!\n");
exit(0);
}
printf("now,put your words:\n");
gets(ch);
fputs(ch,fp);
fclose(fp);

细节问题gets和scanf里的%s有什么区别,前者能读换行符后者不能。

(3)格式化读写

怎么读就怎么写,怎么写就怎么读:格式化读写

1‘ 格式化写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    FILE *fp;
int a[N][N];
while((fp=fopen("words.txt","w"))==NULL)
{
printf("error!!!\n");
exit(0);
}
printf("now,put your words:\n");
for(int i=0;i<4;i++)
{
cout<<"请输入语文 数学 信息技术 通用技术成绩:"<<endl;
cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3];
}
for(int i=0;i<4;i++)
{
fprintf(fp,"%2d\t%2d\t%2d\t%2d\n",a[i][0],a[i][1],a[i][2],a[i][3]);
}
fclose(fp);

2’ 格式化读入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    FILE *fp2;
int a2[N][N];
while((fp2=fopen("words.txt","r"))==NULL)
{
printf("error\n");
exit(0);
}
printf("display:\n");
for(int i=0;i<4;i++)
{
fscanf(fp2, "%2d\t%2d\t%2d\t%2d\n", &a2[i][0], &a2[i][1], &a2[i][2], &a2[i][3]);
cout << a2[i][0] << " " << a2[i][1] << " " << a2[i][2] << " " << a2[i][3] <<endl;
}
fclose(fp2);
(4)数据块读写(二进制读写)

提前声明

1
2
3
4
5
6
7
#define N 100
typedef struct {
int chinese;
int math;
int xxjs;
int tyjs;
}student;

1‘ 二进制写入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    FILE *fp;
student stus[N];
while((fp=fopen("words.txt","wb"))==NULL)
{
printf("error!!!\n");
exit(0);
}
printf("now,put your words:\n");
for(int i=0;i<4;i++)
{
cout<<"请输入语文 数学 信息技术 通用技术成绩:"<<endl;
cin>>stus[i].chinese>>stus[i].math>>stus[i].xxjs>>stus[i].tyjs;
}
fwrite(stus,sizeof(student),N,fp);
fclose(fp);

2’ 二进制读入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    FILE *fp2;
student stu2[N];
while((fp2=fopen("words.txt","rb"))==NULL)
{
printf("error\n");
exit(0);
}
printf("display:\n");
fread(stu2,sizeof(student),N,fp2);
for(int i=0;i<4;i++)
{
cout << stu2[i].chinese << " " << stu2[i].math << " " << stu2[i].xxjs << " " << stu2[i].tyjs <<endl;
}
fclose(fp2);
(5)文件随机读写(文件指针)

1‘ rewind函数 —— 让文件指针重新回到起始位置

1
2
3
4
5
6
long pos = ftell(pf);
printf("重置之前文件指针的位置: %d\n", pos);

rewind(pf);
pos = ftell(pf);
printf("重置之后文件指针的位置: %d\os);n", pos);

2’ ftell 函数 —— 告知当前文件指针相对于起始位

long pos = ftell(pf); printf("%d\n", pos);

3‘ fseek 函数 —— 移动文件指针的位置

fseek(fp, -1, SEEK_CUR); // 将文件指针相对于当前位置向左移动一个单位

第一个参数stream:文件指针(FILE 类型)*

第二个参数offset:相对于起始位置的偏移量。(long类型)

offset < 0,从起始位置起,向左移动 | offset | 个单位
        offset > 0,从起始位置起,向右移动 | offset | 个单位
    第三个参数:起始位置。可选值有三个
        可选值            解析
    SEEK_CUR        当前文件指针指向的位置
    SEEK_END        文件末尾
    SEEK_SET         文件起始位置
    返回值:成功返回0,失败返回一个非零值。