<C> 头文件 文件

一.头文件

1.头文件不仅仅只是系统中自带的 也可以自定义

2.举例:

1 #include "My.h"

自定义的头文件中:可以放系统头文件 可以放typedef 可以放 可以放自定义声明的函数

注:自定义头文件的时候要用 双引号" "

系统的头文件都是用尖括号< > 但是引系统用双引号也可以

用双引号的话 就是先在自定义的头文件中找 如果没找到的话 就在系统头文件中找

二.文件

(PS:下面所有代码中的C:\My.txt中存的是:1234567890)

1.特点:可以长久的保存数据

2.文件存在的两种形式:

①字符形式(ASCⅡ):文本文件

例如:txt文档 xx.c 头文件 这些都是以字符形式存储的 

注:文本文件的结尾是EOF

②二进制文件:

大部分文件的存在方式都是以二进制文件的形式存在的

3.文件缓冲区:

①写:数据→缓冲区→物理文件

②读:数据←缓冲区←物理文件

4.文件使用:

打开文件:FILE* fopen("文件路径","打开方式")

1 #include<stdio.h>
2 int main()
3 {
4     FILE* pFile = fopen("C:\\My.txt","r");
5 
6     return 0;
7 }

FILE是返回一个结构体的指针

文件路径是绝对路径 本身所在的位置 

但是输入文件路径的时候 最好不要直接粘过来 最好手打

并且需要特别特别注意的是:"\"是一个转义字符 是把前后内容连接上的 得打两个"\" 才能获取到正常的文件

打开方式分为:"w" "w+" "r" "r+" "a" "a+"

"w":以只读方式打开

"w+":以可读写的方式打开

"r":以只写方式打开 如果文件中有内容就删除 若文件不存在则建立

"r+":以可读写方式打开 如果文件中有内容就删除 若文件不存在则建立

"a":以附加方式打开 如果文件不存在则建立 若存在则写入的数据会加入文件末尾

"a+":以附加可读写打开 如果文件不存在则建立 若存在则写入的数据会加入文件末尾

如果在后面加上了b 如"wb" 就是对二进制文件进行操作

但是!fopen这个函数是不安全的

所以我们最好用这个安全的函数用来打开文件:errno_t fopen_s(文件名地址,"文件路径","打开方式")

1 #include<stdio.h>
2 int main()
3 {
4     FILE* pFile = NULL;
5     int n = fopen_s(&pFile,"C:\\My.txt","r");
6 
7     return 0;
8 }

它的其中一个返回值是0 代表成功打开这个文件

②读文件:size_t fread(放到哪个地址,一次读几个,一共读几次,从哪个文件读)

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE* pFile = NULL;
 5     int n = fopen_s(&pFile,"C:\\My.txt","r");
 6 
 7     char str[10] = {0};
 8     int nRead = fread(str,1,5,pFile);
 9 
10     printf("%s\n",str);
11 }

执行结果为12345

上面的代码中nRead的值 就是读到的字节数 读几块

例题:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE* pFile = NULL;
 5     int n = fopen_s(&pFile,"C:\\My.txt","r");
 6 
 7     char str[10] = {0};
 8     char str1[10] = {0};
 9     int nRead = fread(str,1,5,pFile);
10     int nRead1 = fread(str1,1,3,pFile);
11 
12     printf("%s\n",str);
13     printf("%s\n",str1);
14 
15     return 0;
16 }

这段代码的执行结果是:

12345

678

这就说明了指针是停留在读到的地方的

如果继续进行读操作 那么指针是在停留的地方继续往下移动的

③写文件:size_t fwrite(从哪个地址走,一次写几个,一共写几次,写入哪个文件中)

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE* pFile = NULL;
 5     int n = fopen_s(&pFile,"C:\\My.txt","r+");
 6 
 7     fwrite("abc",1,3,pFile);
 8 
 9     return 0;
10 }

当打开文件方式为"r+"的时候 执行结果文件My.txt被写为:abc4567890

当打开文件方式为"w+"的时候 执行结果文件My.txt被写为:abc

当打开文件方式为"a+"的时候 执行结果文件My.txt被写为:1234567890abc

④关闭文件:

1 fclose(pFile);

有打开就一定要有关闭 以后一定要写关闭!

⑤进行连续的读写:fseek(文件指针,偏移量,从开始/结尾/当前位置开始)

引入:我们不难发现 我们不可以对一个文件同时进行读写 也就是说 我们不能读完了马上写 或者写完了马上读

那么解决这个文件我们就可以用到fseek函数

从哪个位置开始有三种:SEEK_SET(开始) SEEK_END(结尾) SEEK_CUR(当前)

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE* pFile = NULL;
 5     int n = fopen_s(&pFile,"C:\\My.txt","r+");
 6     int nRead;
 7     char str[10] = {0};
 8 
 9     fwrite("abc",1,3,pFile);
10 
11     fseek(pFile,0,SEEK_CUR);
12 
13     nRead = fread(str,3,1,pFile);
14     printf("%s\n",str);
15 
16     fclose(pFile);
17 
18     return 0;
19 }

执行结果:456

My.txt被改为:abc4567890

5.文件的简单简单简简单单加密:

#include<stdio.h>
int main()
{
    FILE* pFile = NULL;
    FILE* pFile1 = NULL;
    char c;

    fopen_s(&pFile,"C:\\My.txt","r+");
    fopen_s(&pFile1,"C:\\MySecret.txt","w+");
    
    while((c = getc(pFile)) != EOF)
    {
        putc(~c,pFile1);
    }

    fclose(pFile);
    fclose(pFile1);

    return 0;
}

这里需要特别注意的是:

在对文件进行操作的时候 获得字符用getc() 放字符用putc()

6.文件复制:

字符文件的复制:

和上面简单简单简简单单加密的代码是一样 只不过放的时候c就不取反了

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE* pFile = NULL;
 5     FILE* pFile1 = NULL;
 6     char c;
 7 
 8     fopen_s(&pFile,"C:\\My.txt","r+");
 9     fopen_s(&pFile1,"C:\\MyCopy.txt","w+");
10     
11     while((c = getc(pFile)) != EOF)
12     {
13         putc(c,pFile1);
14     }
15 
16     fclose(pFile);
17     fclose(pFile1);
18 
19     return 0;
20 }

二进制文件的复制:

 1 #include<stdio.h>
 2 int main()
 3 {
 4     FILE* pFile = NULL;
 5     FILE* pFile1 = NULL;
 6     char buf[1024] = {0};
 7     int nRead;
 8 
 9     fopen_s(&pFile,"C:\\Users\\pc\\Desktop\\1.jpg","rb");
10     fopen_s(&pFile1,"C:\\Users\\pc\\Desktop\\2.jpg","wb");
11     
12     while(1)
13     {
14         nRead = fread(buf,sizeof(char),1024,pFile);
15         if(nRead <= 0)
16         {
17             break;
18         }
19         fwrite(buf,sizeof(char),1024,pFile1);
20     }
21 
22     fclose(pFile);
23     fclose(pFile1);
24 
25     return 0;
26 }

这里需要注意的是:打开方式一定要用二进制文件的打开方式 rb和wb

猜你喜欢

转载自www.cnblogs.com/Aaaaaalei0612/p/8998678.html