Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

c語言 資料刪除的方法

我要做刪除的功能

輸入姓名然後把他在 input.txt,的資料整筆刪除

姓名(32)+(1)空格 電話(10)+(1)空格 地址(32)

John Smith 2022233322 300 E.Oak Taipei 10843

Jane Smith 3435245564 200 E.Oak Taipei 10843

John Jorda 3254656555 100 E.Oak Taipei 10843

----> 例如我輸入 Jane Smith -------> 第2筆資料就被刪除

我想到的辦法是把不等於這個姓名的資料用迴圈存在另外一個檔案 ie temp.txt

最後用這個temp.txt 取代 input.txt

程式如下

#include<stdio.h>

#include<string.h>

int main (void)

{

typedef struct

{

char name[32];

long long int telephone;

char address[32];

} RECORD;

RECORD customer;

FILE *fpInput;

FILE *fpt;

fpInput=fopen("input.txt","r");

char buffer[100]={0};

char buffer1[50]={0};

char tem[80];

char text[500];

char filename[]="input.txt";

int i,found=0;

system("clear");

printf("請輸入您要刪除的名字");

fgets(a,sizeof(a),stdin);

while(fgets(buffer,sizeof(buffer),fpInput)!=NULL)

{

for(i=0;i<32;i++)

customer.name[i]=buffer[i];

customer.name[31]='\0';

sscanf(buffer,"%d",&customer.telephone);

for(i=0;i<32;i++)

customer.address[i]=buffer[i+44];

customer.address[31]='\0';

for(i=33;i<43;i++)

buffer1[i-33]=buffer[i];

sscanf(buffer1,"%ld",&customer.telephone);

從下面開始比較有問題 complier 過不了 可以幫我看看 ??

if(strncmp(a,customer.name,6)==0) //如果前6碼吻合 就把資料寫進 temp.txt

{

printf("%32s %lld %32s\n",customer.name,customer.telephone,customer.address);

sprintf(text,"%32s %lld %32s\n",customer.name,customer.telephone,customer.address);

fpt=fopen("temp.txt","w");

fwrite(text,1,strlen(text),fpt); ------->如果寫進temp.txt成功的話要怎麼取代現有的input.txt??

}

else

printf("error");

}

fclose(fpInput);

fclose(fpt);

}

1 Answer

Rating
  • Tai
    Lv 5
    8 years ago
    Favorite Answer

    "不等於這個姓名的資料用迴圈存在另外一個檔案 ie temp.txt

    最後用這個temp.txt 取代 input.txt"

    => 我按照你的意思作了, 但貌似還不需要寫個 strcut.

    #include < stdio.h >

    #include < stdlib.h > // system

    #include < string.h > // strcmp

    // http://www.cmlab.csie.ntu.edu.tw/~perng/course/cha...

    int main (void)

    {

    FILE *fpInput;

    FILE *fpt;

    char buffer[200]={0}, target[2][32];

    char *p, name1[32], name2[32];

    int filelen;

    // 讀入 input.txt 並印出. 輸入某人後刪除, 寫入 temp.txt

    fpInput=fopen("input.txt","r");

    fpt=fopen("temp.txt", "w");

    // 印出 input.txt

    while(fgets(buffer,sizeof(buffer),fpInput)!=NULL)

    printf("%s", buffer);

    printf("\nWho do you want to delete?\n");

    scanf("%s %s", target[0], target[1]);

    fseek(fpInput, 0, SEEK_SET); // 回到檔頭

    fgets(buffer,sizeof(buffer),fpInput); // 第一行是格式, 略過

    fputs(buffer,fpt); // 但複製第一行到 temp.txt

    while(fgets(buffer,sizeof(buffer),fpInput)!=NULL) {

    sscanf(buffer, "%s %s", name1, name2);

    // strcmp()==0 表示兩字串相等. 若不相等, 不刪, 寫入 temp.txt

    if (strcmp(target[0], name1) || strcmp(target[1], name2))

    fputs(buffer,fpt);

    }

    fclose(fpt);

    fclose(fpInput);

    // copy temp.txt to input.txt

    fpInput=fopen("input.txt","w"); // 開 input.txt 準備寫入

    fpt=fopen("temp.txt", "r");// 開 temp.txt 準備讀出

    fseek( fpt, 0, SEEK_END);// 讀到 temp.txt 尾處

    filelen = ftell( fpt );// 由 ftell() 告知長度

    fseek( fpt, 0, SEEK_SET);// 回到 temp.txt 起始處

    unsigned char *data=new unsigned char[filelen]; // 配置一塊可以讀整個檔案的空間

    fread( data, 1, filelen, fpt ); // 把整個 temp.txt 讀進記憶體

    fwrite( data, 1, filelen, fpInput ); // 把整個記憶體寫到寫到 input.txt

    fclose(fpt);

    fclose(fpInput);

    // 印出刪除後的 input.txt

    fpInput=fopen("input.txt","r"); // 開 input.txt 準備讀出

    printf("\nAfter delete\n");

    while(fgets(buffer,sizeof(buffer),fpInput)!=NULL)

    printf("%s", buffer);

    printf("\n");

    fclose(fpInput);

    system("pause");

    return 1;

    }

Still have questions? Get your answers by asking now.