#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
int main()
{FILE*fp;//文件指针
int bh,m=0;//储存字符的ASCLL
char ch=NULL,filemane[10],way[20];//储存字符的变量,文件名
printf("请输入所用的文件名:");
scanf("%s",filemane);
printf("请输入所用的文件路径:");
scanf("%s",way);
strcpy(way,filemane);
strcpy(way,".txt");
getchar();//消化回车
if((fp=fopen(way,"w"))==NULL)//判断文件是否可以打开
{
printf("cannot open file\n");
exit(0);
}
printf("请输入信息:student number, first name, last name, gender, major, address of dormitory, names of roommates (if has)(以#结束)\n");//输入信息并储存
ch=getchar();
//getchar();
m=m+1;
while(ch!='#')
{ if(ch!='\n')
{ fputc(ch,fp);}//输入进磁盘
ch=getchar();
m++;
}//实际有m-1个字符
fclose(fp);//关闭文件
putchar(10);//输出一个回车
fopen(way,"a");
if((fp=fopen(way,"a"))==NULL)//判断文件是否可以打开
{
printf("cannot open file\n");
exit(0);
}
fflush(stdin);
int i=0;//记录输入 字符数
int n[100]={0};
printf("请输入自我介绍:(以#结束)");
ch=getchar();
i=i+1;
while(ch!='#')//
{
bh=ch;
ch=(char)((ch*ch)%127);//以y=x*x形式加密 为防止超过127进行取余
n[i-1]=bh*bh/127;//记录余数用于之后的还原
fputc(ch,fp);//输入进磁盘
ch=getchar();
//getchar();
i++; }//实际上有i-1个字符
pclose(fp);
fopen(way,"r");
if((fp=fopen(way,"r"))==NULL)//判断文件是否可以打开
{
printf("cannot open file\n");
exit(0);}
char str[100];//储存文件里的字符
fgets(str,m+i-1,fp);
int k=0;
for(;k<=m-1;k++)//输出未加密部分
{
printf("%c",str[k]);
}
printf("\n");
for(int p=0;k<=m+i-1;k++,p++)
{
int nn=n[p];
double mi=0.5;
int num=str[k];
int asc=pow(num+nn*127,mi);
printf("%c",asc);
}
return 0;
}
strcpy(way,filemane);
strcpy(way,".txt");
都用错了,应该是strcat
所以最后的way根本不是你想象要打开的文件
scanf("%s",way);
strcat(way,filemane);
strcat(way,".txt");
因为你在上面写文件的时候,从键盘输入数据时候输入了回车符,回车符也被写入文件了。而fgets函数时逐行读取文件,所以在下面读取文件的时候,只能读取回车符前面的字符。