我的C文件中有三個函數用於二進制文件:accadd用於添加記錄,用於在文本文件中查看輸出並修改用於修改記錄。C - 在二進制文件中修改記錄,輸出爲文本文件將0s插入文本文件
A記錄的結構如下:
struct date
{
int day;
int month;
int year;
};
struct customer
{
char name[40], acctype[10];
int accno, age;
double phone;
float amount;
struct date deposit;
} add;
視圖功能犯規輸出所有的變量,只有名稱,登錄號和電話。例如,對於2條記錄添加,這是輸出文件:
Customer's List
Customer's Name: Account Number: Customer's Phone No:
John 1 777777777
Mary 2 111111111
這是好的,當我運行的修改功能,並再次輸出文件不同的是:
Customer's List
Customer's Name: Account Number: Customer's Phone No:
John 1 999999999
Mary 2 111111111
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0
的記錄得到了修改,但我得到一堆領先的0。任何理由?
的修改功能:
void modify(char file[30]){
FILE *view;
int counter = 0;
view = fopen(file, "rb+");
if (view == NULL)
exit(1);
while (fread(&add, sizeof(add), 1, view) != 0)
{
if(add.phone==777777777){
printf("Old phone is: %lf\n",add.phone);
printf("New phone:\n");
scanf("%lf", &add.phone);
printf("New phone is: %lf\n",add.phone);
fseek(view,sizeof(add)*counter,SEEK_SET);
fwrite(&add,sizeof(add),1,view);
}
counter++;
}
fclose(view);
if (counter == 0)
{
printf("NO RECORDS FOUND!\n");
}
else{
printf("add.phone printed\n");
}
}
EDIT1:視圖函數。
void view(char file[30])
{
FILE *view,*output;
int test = 0;
output=fopen("output.txt", "w");
fprintf(output,"Customer's List\n");
fprintf(output,"\tCustomer's Name:");
fprintf(output,"\tAccount Number:");
fprintf(output,"\tCustomer's Phone No:\n");
view = fopen(file, "rb");
if (view == NULL)
exit(1);
while (fread(&add, sizeof(add), 1, view) != 0)
{
fprintf(output,"\t%16s", add.name);
fprintf(output,"\t%15d", add.accno);
fprintf(output,"\t%20.0f", add.phone);
fprintf(output,"\n");
test++;
}
fclose(view);
fclose(output);
if (test == 0)
{
printf("NO RECORDS FOUND!\n");
}
else{
printf("Output updated in file output.txt\n");
}
}
EDIT2:MCVE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void menu(void);
void accadd(void);
void modify(char file[30]);
void view(char file[30]);
struct date
{
int day;
int month;
int year;
};
struct customer
{
char name[40], acctype[10];
int accno, age;
double phone;
float amount;
struct date deposit;
} add;
void accadd(void)
{
FILE *fp = fopen("cus.dat", "ab+");
if (fp == NULL)
exit(1);
printf("ADD RECORD\n");
printf("Enter today's date(date/month/year) \n");
if (scanf("%d/%d/%d", &add.deposit.day, &add.deposit.month, &add.deposit.year) != 3)
exit(1);
printf("Enter account number\n");
if (scanf("%d", &add.accno) != 1)
exit(1);
printf("Enter customer's name\n");
if (scanf("%s", add.name) != 1)
exit(1);
printf("Enter customer's age\n");
if (scanf("%d", &add.age) != 1)
exit(1);
printf("Enter customer's phone num\n");
if (scanf("%lf", &add.phone) != 1)
exit(1);
printf("Enter the account type(in words): \n\t 1:Current\n\t 2:Saving\n\t 3:Fixed\n");
if (scanf("%s", add.acctype) != 1)
exit(1);
printf("Almost done! Just enter the amount you want to deposit: ");
if (scanf("%f", &add.amount) != 1)
exit(1);
fwrite(&add, sizeof(add), 1, fp);
fclose(fp);
}
void view(char file[30])
{
FILE *view,*output;
int test = 0;
output=fopen("output.txt", "w");
fprintf(output,"Customer's List\n");
fprintf(output,"\tCustomer's Name:");
fprintf(output,"\tAccount Number:");
fprintf(output,"\tCustomer's Phone No:\n");
view = fopen(file, "rb");
if (view == NULL)
exit(1);
while (fread(&add, sizeof(add), 1, view) != 0)
{
fprintf(output,"\t%16s", add.name);
fprintf(output,"\t%15d", add.accno);
fprintf(output,"\t%20.0f", add.phone);
fprintf(output,"\n");
test++;
}
fclose(view);
fclose(output);
if (test == 0)
{
printf("NO RECORDS FOUND!\n");
}
else{
printf("Output updated in file output.txt\n");
}
}
void modify(char file[30]){
FILE *view;
int counter = 0;
view = fopen(file, "rb+");
if (view == NULL)
exit(1);
while (fread(&add, sizeof(add), 1, view) != 0)
{
if(add.phone==777777777){
printf("Old phone is: %lf\n",add.phone);
printf("New phone:\n");
scanf("%lf", &add.phone);
printf("New phone is: %lf\n",add.phone);
fseek(view,sizeof(add)*counter,SEEK_SET);
fwrite(&add,sizeof(add),1,view);
}
counter++;
}
fclose(view);
if (counter == 0)
{
printf("NO RECORDS FOUND!\n");
}
else{
printf("add.phone printed: %i\n",counter);
printf("add length %d\n",sizeof(add));
}
}
void menu(void)
{
int n,account_number;
char file[30],account_name[30];
printf("Enter your choice 1, 2, 3\n");
while(1){
if (scanf("%d", &n) != 1)
exit(1);
switch (n){
case 1:
accadd();
break;
case 2:
printf("Enter the file\n");
scanf("%s",&account_name);
view(account_name);
break;
case 3:
modify("cus.dat");
break;
}
printf("Enter your choice 1, 2, 3\n");
}
}
int main(void)
{
menu();
return 0;
}
什麼是'add'?它在哪裏宣佈? –
請查看第一個代碼塊,我在那裏定義了結構。 – RaresD
啊我看到了,它是定義結構時定義的全局。這裏不需要全局變量,建議在函數內定義變量。 –