我试图从头文件中加密一个文件,每次我运行该程序时,它都会删除文件AccountInformation.txt中的信息。是否有任何理由从帐户信息中删除信息.txt以及如何修复我的代码?
任何帮助都会很棒,谢谢。
c代码如下:
#include <stdio.h>
#include <string.h>
#include "Encryption1.h"
// Variable for signup and login
char fname[120], lname[120], username[120], password[120];
int session;
FILE * accountInfoPointer;
// Main Fucntion for sign up and login
int main (void)
{
// Asking user is they want to sign up or login
printf("What would you like to do?n");
printf("1. Sign un");
printf("2. Loginn");
printf("3. Admin login");
scanf("%d", &session);
// signup
switch(session)
{
case 1:
// Retreiving all of the user's data
printf("What is your FIRST name?n");
scanf(" %s", fname);
printf("What is your LAST name?n");
scanf(" %s", lname);
printf("Please enter a username?n");
scanf(" %s", username);
printf("Create a passwordn");
scanf(" %s", password);
accountInfoPointer = fopen("AccountInformation.txt", "a");
fprintf(accountInfoPointer, "Name: %s %snUsername: %snPassword: %snn", fname, lname, username, password);
fclose(accountInfoPointer);
goto Encrypt; //goes to calender menu
Encrypt:{
//char Title;{
printf("nntttt::::::Encrypt::::::n");
encrypted1(1);
}
break;
}
}
这是头文件代码:
#include<stdio.h>
#include<stdlib.h>
int encrypt(void);
int decrypt(void);
int encrypt_view(void);
int decrypt_view(void);
FILE *fp1, *fp2;
char ch;
void encrypted1(int Encrypt)
{
int choice;
while(1)
{
printf("Select One of the Following:n");
printf("n1. Encryptn");
printf("2. Decryptn");
printf("3. View The Encypted Filen");
printf("4. View The Decrypted Filen");
printf("5. Exitn");
printf("nEnter Your Choice:t");
scanf("%d", &choice);
switch(choice)
{
case 1: encrypt();
break;
case 2: decrypt();
break;
case 3: encrypt_view();
break;
case 4: decrypt_view();
break;
case 5: exit(1);
}
}
printf("n");
// return 0;
}
int encrypt()
{
printf("n");
fp1 = fopen("AccountInformation.txt","r");
if(fp1 == NULL)
{
printf("Source File Could Not Be Foundn");
}
fp2 = fopen("AccountInformation.txt","w");
if(fp2 == NULL)
{
printf("Target File Could Not Be Foundn");
}
while(1)
{
ch = fgetc(fp1);
if(ch == EOF)
{
printf("nEnd Of Filen");
break;
}
else
{
ch = ch - (8 * 5 - 3);
fputc(ch, fp2);
}
}
fclose(fp1);
fclose(fp2);
printf("n");
//return 0;
}
int decrypt()
{
printf("n");
fp1 = fopen("AccountInformation.txt","r");
if(fp1 == NULL)
{
printf("Source File Could Not Be Foundn");
}
fp2 = fopen("AccountInformation.txt","w");
if(fp2 == NULL)
{
printf("Target File Could Not Be Foundn");
}
while(1)
{
ch = fgetc(fp1);
if(ch == EOF)
{
printf("nEnd Of Filen");
break;
}
else
{
ch = ch + (8 * 5 - 3);
fputc(ch, fp2);
}
}
fclose(fp1);
fclose(fp2);
printf("n");
//return 0;
}
int encrypt_view()
{
printf("n");
fp1 = fopen("AccountInformation.txt","r");
if(fp1 == NULL)
{
printf("No File Foundn");
exit(1);
}
else
{
while(1)
{
ch = fgetc(fp1);
if(ch == EOF)
{
break;
}
else
{
printf("%c", ch);
}
}
printf("n");
fclose(fp1);
}
printf("n");
//return 0;
}
int decrypt_view()
{
printf("n");
fp1 = fopen("AccountInformation.txt","r");
if(fp1 == NULL)
{
printf("No File Foundn");
exit(1);
}
else
{
while(1)
{
ch = fgetc(fp1);
if(ch == EOF)
{
break;
}
else
{
printf("%c", ch);
}
}
printf("n");
fclose(fp1);
}
// return 0;
printf("n");
}
fp1 = fopen("AccountInformation.txt","r");
if(fp1 == NULL)
{
printf("Source File Could Not Be Foundn");
}
fp2 = fopen("AccountInformation.txt","w");
if(fp2 == NULL)
{
printf("Target File Could Not Be Foundn");
}
我对此感到畏缩,但这个想法应该适用于 unix 衍生品。你正在破坏你的数据,因为你应该使用"r+",但你使用了"w"。+
会同时打开读取和写入,但w+
将被截断。你不想要a
.除非您正在编写日志文件,否则您不希望a
所以不要在这里尝试。
至少这个问题:
无法正确区分所有输入和fgetc()
char
作为 8 位,可以编码 256 种组合。int fgetc()
返回 257 个值:[0-UCHAR_MAX]
和EOF
。
代码使用ch = ch - (8 * 5 - 3); fputc(ch, fp2);
进行加密,这很容易生成一个char
,该在以后的读取中与EOF
发生冲突,从而使文件不足。
char ch; // Bad
ch = fgetc(fp1);
if(ch == EOF) // Was ch an EOF or some character?
使用int ch
区分返回值和fgetc()
。
对于加密/解密,最好以二进制模式打开文件,而不是文本模式。 追加"b"
。
// fp1 = fopen("AccountInformation.txt","r");
fp1 = fopen("AccountInformation.txt","rb");
>替代示例代码:
int encrypt_view(void) {
printf("n");
FILE *fp = fopen("AccountInformation.txt", "rb");
if (fp == NULL) {
printf("No File Foundn");
return 1; // Let calling code exit
}
int ch;
while ((ch = fgetc(fp)) != EOF) {
if (ch >= ' ' && ch <= '~') {
printf("%c", ch);
} else {
printf("<%d>", ch);
}
}
printf("n");
fclose(fp);
return 0;
}