1.程序
#include
#include
#include
//功能:实现加密功能,将文件中的a变为b,b变成c,到最后z再变为a,大写字母也一样
void encrypt(char *path);
int ReadFile(char *path,char *temp);
int WriteFile(char *path,char *temp);
int main(int argc,char *argv[])
{
int i = 0;
if(argc == 1)
//当文件参数不存在时,提示
{
printf("加密文件不存在!\n");
//return -1;
}
else
{
for(i=0;i= 'a' && temp[i] < 'z') || (temp[i] >= 'A' && temp[i] < 'Z'))
temp[i]++;
else if(temp[i] == 'z')
temp[i] = 'a';
else if(temp[i] == 'Z')
temp[i] = 'A';
}
if(WriteFile(path,temp) == 1) //调用写文件函数
return;
return;
}
int ReadFile(char *path,char *temp)
{
FILE *fin = NULL;
char ch = ' ';
int i=0;
//读文件
fin = fopen(path,"r");
if(!fin) //文件读取失败
{
printf("文件%s读取失败!\n",path);
return 1;
}
//每次读取文件中单个字符,并放到一个数组中
ch = fgetc(fin);
while(ch != EOF)
{
temp[i] = ch;
i++;
ch = fgetc(fin);
}
temp[i] = '\0'; //设置字符串的末尾
fclose(fin); //文件读取结束;
return 0;
}
int WriteFile(char *path,char *temp)
{
FILE *fout = NULL;
//写文件
fout = fopen(path,"w"); //以写操作的形式打开文件,原文件被删除
if(!fout) //文件写失败
{
printf("文件%s写失败!\n",path);
return 1;
}
fprintf(fout,"%s",temp); //将数组中的数据写入文件
fclose(fout); //文件写操作结束
return 0;
}
2.编译
gcc encrypt.c -o a.out