2016-03-08 41 views
-1

我的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; 
} 
+1

什麼是'add'?它在哪裏宣佈? –

+0

請查看第一個代碼塊,我在那裏定義了結構。 – RaresD

+0

啊我看到了,它是定義結構時定義的全局。這裏不需要全局變量,建議在函數內定義變量。 –

回答

1

一些可變印刷後,我設法承擔我被卡在寫入模式和fwrite所以我用一個

fseek(f,0,SEEK_SET); 

後要回讀模式。我不知道這個解釋是否準確,但它解決了我的問題。

我還簡化了我的修改功能,使其更清晰。

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){ 
      add.phone=145; 
      fseek(view,sizeof(add)*counter,SEEK_SET); 
      fwrite(&add,sizeof(add),1,view); 
      fseek(view,0,SEEK_SET); 
     } 
      counter++; 
    } 
    fclose(view); 
} 

我希望對任何想要從二進制文件修改記錄的人都有用。

0

我最好的猜測是,你的fread()方法以某種方式行爲不端。要進行調試,您可能需要捕獲返回值,以便在調試器中進行檢查。因此,更改視圖()函數做這樣的事情:

void view(char file[30]) 
{ 
    FILE *view,*output; 
    size_t ret; 
    int test = 0; 

    /* ... */ 

    while ((ret = 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); 

    /* ... */ 
} 

將在調試器設置一個斷點上循環的第一線,檢查每個迭代的RET變量。對於這兩個預期的迭代,ret的值應該是1,但是迭代打印零點的值是多少?

一種可能性是,它在某些情況下選擇通過返回-1來表示錯誤(是的,這將被打破)。在這種情況下,您可以將條件從測試非零變爲測試實際返回值fread()讀取1條記錄時應該具有的值。

+0

問題在於修改功能。如果在第一篇文章(選擇選項1)中添加2條記錄,其中一條電​​話= 777777777,另一條沒有,然後使用選項2打印它,則會得到文件output.txt。這個文件很好,輸出很好,文件中只有2條記錄。但是當你修改它的時候,你會在文件中得到那些奇怪的輸入。 – RaresD