C的文件输入输出一直纠结
输入的文件地址也不对
求解!!!
这是代码:
代码: 全选
// MopperWhite的加密软件 bate 0.04
// 请编辑过这个文档的人在文档末端留下您的大名及大致解决的问题或增加的功能作为日志!!别忘了标日期!!!
//
// 加密bate0.04.c
//
// Copyright 2011 MopperWhite email:mopperwhite@gmail.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
#include <stdio.h>
#include <string.h>
#define MAXN 1000000
int myin()
{
char i;
printf("本软件暂时不能识别换行(俗称回车)与空格\n本软件能读取的内容长度仅限于%d个字节(%d个汉字)'\n由于算法不同,本软件能解密同一软件加密的数据,当然,您可以自己修改它的源码来更改算法 XD\n",MAXN,MAXN/2);
printf("请问您想加密文件还是解密文件?\n若加密请输“l”,解密请输“u”,退出请输入“e”\nYou:");
scanf("%c",&i);
if(i=='l') return 1;
else
if(i=='u') return 2;
else
if(i=='e') return 3;
else
{
printf("请再看看上面的提示!!\n");
return myin();
}
return 0;
}
void lock(char *rin,char *passwd,char *filename)
{
int n=strlen(rin);
int m=strlen(passwd);
int i,j=0;
for(i=0;i<n;i++,j++)
{
rin[i]=rin[i]+passwd[j]-43;
if(j>=m-1) j=-1;
}
sprintf(filename,"%s%s",filename,".mout");
FILE *fn;
fn=fopen(filename,"w");
fprintf(fn,"%s",rin);
fclose(fn);
}
void unlock(char *rin,char *passwd,char *filename)
{
int n=strlen(rin);
int m=strlen(passwd);
int o=strlen(filename);
char _filename[MAXN];
int i,j=0;
for(i=0;i<o-5;i++)
_filename[i]=filename[i];
for(i=0;i<n;i++,j++)
{
rin[i]=rin[i]-passwd[j]+43;
if(j>=m-1) j=-1;
}
//freopen(_filename,"w",stdout);
FILE *fn;
fn=fopen(_filename,"w");
fprintf(fn,"%s",rin);
fclose(fn);
}
void myout(char *filename,char *passwd,int jinn)
{
char c;
int i;
char rin[MAXN];
//printf("以下为处理结果:\n%s",rin);
printf("\n\n========================我是分割线~=========================\n处理完毕的文件将在软件关闭后被存储在当前目录下\n最后说一下,本软件是开源软件,理论上您应该可以从装它的压缩包里找到源代码\n感谢您的使用~~~~~ XD\n");
//scanf("%c",&c);
sprintf(filename,"%s%s","./",filename);
FILE *_rin;
_rin=fopen(filename,"r");
if(_rin==NULL)
{
printf("出错:文件不存在\n按回车退出");
char pp;
scanf("%c",&pp);
return;
}
for(i=0;!feof(_rin);i++)
{
fscanf(_rin,"%c",&c);
rin[i]=c;
}
fclose(_rin);
if(jinn==1) lock(rin,passwd,filename);
if(jinn==2) unlock(rin,passwd,filename);
}
int main(void)
{
printf("***********************************************************\n本软件使用GPL协议发布,您可以随意更改、复制、使用它的源代码\n***********************************************************\n\n");
char _b[10];
//char rin[MAXN];
char filename[MAXN];
char passwd[MAXN];
int jinn=myin();
//printf("%d",myin());
if(jinn==1) sprintf(_b,"%s","加密");
else
if(jinn==2) sprintf(_b,"%s","解密");
else
if(jinn==3) return 0;
else
{
printf("起始模块发生未知错误");
return 0;
}
printf("请输入您将要%s的文件放在本软件的运行目录下并输入文件名,输完请按回车:\n",_b);
scanf("%s",filename);
printf("请输入密码:");
//gets(passwd);
scanf("%s",passwd);
myout(filename,passwd,jinn);
//if(jinn==1) lock(rin,passwd);
//if(jinn==2) unlock(rin,passwd);
return 0;
}
//软件生来自由!!!!!!!!
//坚定GPL一百年不动摇!!!!
//下面是日志:
//====================Bate0.02===========================================
//备注:0.02版本经过Drazet童鞋的修改与优化,谨此表达感谢!
//同时对MW的编程水平……不发表任何意见……
//重要修正:Drazet童鞋帮助解决了MW的把C存成.cpp的问题以及if后的缩进等不良习惯,同时解决了out函数不能通过编译的问题【要命的*.h啊!!!】
//还有!in和out貌似是被占用的!!
//2011/01/08
//====================Bate0.03==========================================
//MopperWhite将0.02中Drazet优化和修改的部分放进同一个文件= =#(原因么……因为MW不会写*.h……)
//貌似难度不大- -!
//纠正一个错别字~
//修正了一个因为if和else使用不当造成异常退出的BUG
//在注释中要求后面的修改者(别看了,就是你!)写日志~
//增加了文件输出(无效~)
//输入不能有空格的BUG待解决
//今天搞不定中文无法解密的问题,建议用if语句对ASCLL码较大的字符进行特殊处理(>100)
//2011/01/09
//====================Bate0.04====================================
//修改为加密文件
//回车问题依旧……
//把“\n友情提示:OGC伤身体(不要深究它的意思……)”去掉……"OGC"本身很邪恶……
//2011/01/09
viewtopic.php?f=77&t=313241&p=2183178