复制文件

 1 //任意文件复制demo
 2 
 3 #define _CRT_SECURE_NO_WARNINGS
 4 #include<stdio.h>
 5 #include<stdlib.h>
 6 #include<string.h>
 7 #include<time.h>
 8 
 9 void input_file()
10 {
11     char buf[4096] = { 0 };
12     FILE* fp1 = fopen("test.mp4", "rb");//fp1用来读文件数据
13     FILE* fp2 = fopen("test_cp.mp4", "wb");//fp2用来写文件数据
14     while (1)
15     {
16         int ret;
17         ret = fread(buf, 1,sizeof(buf), fp1);
18         if (ret == 0)
19         {
20             break;
21         }
22         fwrite(buf, 1, ret, fp2);
23     }
24     fclose(fp1);
25     fclose(fp2);
26 
27 }
28 
29 int main()
30 {
31     input_file();
32     return 0;
33 }
View Code

 

随机数文件排序

 1 //随机数文件排序
 2 
 3 #define _CRT_SECURE_NO_WARNINGS
 4 #include<stdio.h>
 5 #include<stdlib.h>
 6 #include<string.h>
 7 #include<time.h>
 8 
 9 int a[10];
10 void printf_file()
11 {
12     FILE* fp = fopen("test.txt", "w");
13     if (!fp)
14     {
15         perror("OPEN FILE:\n");
16     }
17     srand(time(NULL));
18     for (size_t i = 0; i < 10; i++)
19     {
20         fprintf(fp,"%d\n",rand() % 100);
21     }
22 
23 
24     fclose(fp);
25 }
26 void read_file_fscanf()
27 {
28     FILE* fp = fopen("test.txt", "r");
29     if (!fp)
30     {
31         perror("OPEN FILE:");
32     }
33     int temp =0;
34     int k = 0;
35     while (1)
36     {
37         if (feof(fp))
38         {
39             break;
40         }
41         fscanf(fp, "%d\n", &temp);
42         printf("num = %d\n", temp);
43         a[k] = temp;
44         k++;
45 
46     }
47     fclose(fp);
48 }
49 void Paixu_File()
50 {
51     read_file_fscanf();
52     FILE* fp = fopen("dst.txt", "w");
53     if (!fp)
54     {
55         perror("NEW file create default:");
56     }
57     int temp;
58     for (int i = 0; i < 9; i++)
59     {
60         for (int j = i + 1; j < 10; j++)
61         {
62             if (a[i] > a[j])
63             {
64                 temp = a[i];
65                 a[i] = a[j];
66                 a[j] = temp;
67             }
68 
69         }
70     }
71     for (int i = 0; i < 10; i++)
72     {
73         fprintf(fp, "%d\n", a[i]);
74     }
75 
76 
77     fclose(fp);
78 
79 }
80 int main()
81 {
82     printf_file();    //给文件输入随机数
83     Paixu_File();
84     return 0;
85 }
View Code

 

任意读写文件

 1 //任意读写文件
 2 
 3 #define _CRT_SECURE_NO_WARNINGS
 4 #include<stdio.h>
 5 #include<stdlib.h>
 6 #include<string.h>
 7 #include<time.h>
 8 typedef struct student {
 9     int age;
10     char name[10];
11     int num;
12 }stu_t;
13 stu_t stu[4] = {
14     18,"hehe",20,
15     19,"FUCK",30,
16     20,"OOOO",40,
17     21,"NONO",50
18 };
19 
20 void Input_file()
21 {
22     FILE* fp = fopen("test.txt", "rb+");
23     if (!fp)
24     {
25         perror("OPEN FILE:");
26         return;
27     }
28     stu_t test1;
29     fwrite(stu, 1,4*sizeof(stu_t), fp);//往里面以二进制写入4个结构体
30     fseek(fp, sizeof(stu_t), SEEK_SET);//光标移到第一个结构体的后面一个
31     fread(&test1, 1, sizeof(test1), fp);
32     printf("The Second age = %d,name = %s,num = %d\n", test1.age, test1.name, test1.num);
33     int lenth = ftell(fp);
34     printf("当前到文件起始位置的字节数为%d\n", lenth);
35     rewind(fp);//将文件光标移到最初始
36     //获取文件大小
37     fseek(fp, 0, SEEK_END);//将文件光标移到末尾
38     lenth = ftell(fp);//获取从当前文件光标到最初始的字节数
39     printf("文件总大小为%d\n", lenth);
40     fclose(fp);
41 }
42 
43 void main()
44 {
45 
46     Input_file();
47 }
View Code

 

不用打开文件获取文件信息

 1 //不用打开文件获取文件属性
 2 #define _CRT_SECURE_NO_WARNINGS
 3 #include<stdio.h>
 4 #include<stdlib.h>
 5 #include<string.h>
 6 #include<time.h>
 7 #include<sys/types.h>
 8 #include<sys/stat.h>
 9 
10 
11 
12 void main()
13 {
14     struct stat buf;
15     int ret = stat("test.txt", &buf);//传输参数,在函数调用结束时,充当函数返回值
16     printf("文件大小 :%d\n", buf.st_size);
17 }
View Code