Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 5 của 5
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    đây là nguyên source code. Nhờ mọi người sửa giùm đoạn READ_FROM_FILE

    Mã:
    #include "stdafx.h"#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <malloc.h>#include <conio.h>#include <ctype.h>#include <memory.h>#include <io.h>#include <fcntl.h>#include <sys/stat.h>#include <errno.h>#ifdef _MSC_VER#define _creat(p,m) _open(p, O_CREAT|O_TRUNC|O_WRONLY,m|SH_DENYNO)#endif typedef struct nguoi{    char ten[127];    char ngaysinh[127];    char diachi[127];    char sodt[127];    char sott[127]; //create index        struct nguoi *next;}nguoi; typedef struct {    nguoi *FIRST;    nguoi *LAST;}list; /**** Global 変数 ****/list DS;/*********************/ int input_cstring(char* szString, int size);int input_process(list &DS,int stt, int phanloai, nguoi *sua); void output_all(list DS, int phanloai);void output_1ken(list DS,int sothu); void create_file();void read_from_file(list &DS);void clear_data_in_memory(list &DS); void delete_index (list &DS, int somuonxoa);void edit_index(list &DS,int somuonsua);int edit_index_number(list &DS); int kiemtra_solan(char *s);int kiemtraName(char *ss);int kiemtrabirthday(char *ss);int kiemtrasdt(char *ss);  int kiemtra_solan(char *s){    int i=0,cnt=0,j=0;    /********************************        * j = 1 ---> Input OK        * j = 2 ---> Re input    *********************************/    char ss[5]="111";    strcpy(ss,s);     for(cnt=0;cnt<strlen(ss);cnt++)    {        if(isalnum(ss[cnt]))        {            if(!isalpha(ss[cnt]))            {                j=1;            }            else            {                j=2;                break;            }        }        else        {            j=2;            break;        }    }    if(j==1)    {        if(atoi(s) > 9999 || strlen(s) > 4 || atoi(s) == 0)            j=2;    }     fflush(stdin);    return j;} int main(){    nguoi *dan;     int solan,i,cnt,check_input=0,phanloai=0;    char tam[5];    printf("◎ 件数」は9999まで入力してください。
    \t又、半角の数字以外入力しないでください。
    ");    printf("\t件数はEnterキーだけを押せば、終了する
    ");    printf("◎ 名前・住所・生年月日・電話番号は必須です。全てスペース又タッブはダメです。
    ");    printf("◎ 生年月日は「YYYY/MM/DD」に入力してください
    ");    printf("◎ 電話番号は「xxx-xxxx-xxxx」に入力してください
    ");    printf("◎ 入力文字のルール
    ");    printf("\t◆半角の場合、名前が20桁・住所が127桁まで入力してください。
    ");    printf("\t◆全角の場合、名前が10桁・住所が64桁まで入力してください。
    
    ");    printf("\t∵ 注意 :入力文字は上記の宣言を超える文字が警報せず保存してない
    ");    printf("◆ 全項目は入力必須です。入力しないと、終了する。
    
    ");    printf("件数入力 : ");     /************** CREATE FILE ************/    FILE *file_name;    if((file_name=fopen("kadai2-5.csv","w")) == NULL)    {        printf("Can not open this file
    ");    }     /* 件数入力 */    do    {        if(0==input_cstring(tam,4))            printf("件数を入力してください
    件数再入力 : ");         fflush(stdin);        check_input=kiemtra_solan(tam);        solan=atoi(tam);        if(check_input==2)            printf("件数再入力 : ");    }while(check_input!=1);        fflush(stdin);    dan = (nguoi *)malloc(solan*sizeof(nguoi));     for(cnt = 0; cnt < solan; cnt++)    {        if (input_process(DS,cnt+1,0, 0) == 0)        {             break;        }     }    //system("cls");    cnt++;/****************** SELECT MENU ****************/    int a=0;    do    {        printf("
    ◆◆◆◆MENU◆◆◆◆
    1. 追加入力
    2. 変更
    3. 削除
    4. 1件表示
    5. 一覧表示
    6. 保存
    7. 読み込み
    0. 終了
    ◆◆◆◆◆◆◆◆◆◆
    
    入力番号 : ");        //scanf("%d",&a);                if(0==input_cstring(tam,4))        {            printf("再入力選択項目: ");            system("cls");        }                fflush(stdin);        check_input=0;        check_input=kiemtra_solan(tam);        solan=atoi(tam);        switch(solan)        {                        case 1:                if (input_process(DS,cnt,0, 0) == 0)                {                     break;                }                fflush(stdin);                cnt=edit_index_number(DS);                break;            case 2:                system("cls");                printf("変更していデータの項目番号を選択してください
    ");                output_all(DS,2);                printf("更新項目番号選択 : ");                if(0 == input_cstring(tam,4))                    printf("データがない");                solan=atoi(tam);                fflush(stdin);                edit_index(DS,solan);                printf("修正後の一覧リスト
    ");                output_all(DS,2);                break;            case 3:                system("cls");                printf("削除したいの項目番号を選択してください
     ");                output_all(DS,2);                printf("削除項目番号 : ");                if(0 == input_cstring(tam,4))                    printf("データがない");                solan=atoi(tam);                fflush(stdin);                delete_index(DS,solan);                printf("
    
    ----- 削除後 -----
    ");                edit_index_number(DS);                output_all(DS,2);                solan=1;                break;            case 4:                printf("表示したいデータの番号を選んでください
    表示番号 : ");                output_all(DS,2);                printf("表示件目入力 : ");                if(0==input_cstring(tam,4))                    printf("表示件目再入力 : ");                solan=atoi(tam);                fflush(stdin);                output_1ken(DS, solan);                solan=1;                break;            case 5:                output_all(DS, 1);                break;            case 6:                printf("ファイル保存しました。
    ファイル名は [kadai2-5.csv]です
    ");                //goi ham chep file                create_file();                //xoa bo nho                break;            case 7:                clear_data_in_memory(DS);                printf("DA XOA XONG
    ");                Sleep(2000);                read_from_file(DS);                break;            case 0:                exit(1);        }        if(check_input==2 || solan>7)        {            system("cls");            printf("選択数字が違っている。再選択してください
    
    ");            check_input=2;        }                    }while(check_input!=1 || solan!=0);     free(dan);    return 0;} int input_cstring(char* szString, int size){    char ch = 0;    int count = 0;    for (count = 0; count < size; count++)    {        if(count<size)        {            ch = getchar();            if (ch == '
    ')                break;            szString[count] = ch;        }    }    szString[count] = '\0';    return count;}int input_process(list &DS,int stt, int phanloai, nguoi *sua){    /******************* phanloai *******************            * phanloai = 0 ---> 入力            * phanloai = 1 ---> 修正    *************************************************/    nguoi *p;    p = new nguoi;    if(phanloai == 1)//修正する場合            p = sua;     sprintf(p->sott,"%d",stt);        int kiemtra=3;    printf("--------------入力------------
    ");    printf("名前入力 : ");    do    {        if(0==input_cstring(p->ten,20))            return 0;        fflush(stdin);        kiemtra = kiemtraName(p->ten);        if(kiemtra == 1)            printf("名前再入力 : ");                }while(kiemtra!=0);  /*************************************************************/     printf("住所入力 : ");    do    {        if(0==input_cstring(p->diachi,128))            return 0;        fflush(stdin);        kiemtra = kiemtraName(p->diachi);        if (kiemtra == 1)            printf("住所再入力 : ");    }while (kiemtra!=0);  /*************************************************************/        printf("生年月日入力 : ");    do    {        if (0 == input_cstring(p->ngaysinh, 10) )            return 0;        fflush(stdin);        kiemtra = kiemtrabirthday(p->ngaysinh);        if(kiemtra == 1)            printf("生年月日入力は間違っている。
    生年月日再入力 : ");    }while (kiemtra != 0);  /*************************************************************/        printf("電話番号入力 : ");    do    {        if (0 == input_cstring(p->sodt, 16) )            return 0;        fflush(stdin);        kiemtra=kiemtrasdt(p->sodt);        if(kiemtra==1)            printf("電話番号入力は間違っている。
    電話番号再入力 : ");    }while ( kiemtra != 0);            /************* DSに繋がる **************/    /*-------- phanloai = 1 ---> EDIT DATA -----*/      if(phanloai!=1)    {        if(DS.FIRST == NULL)        {            DS.FIRST = p;            DS.LAST = p;        }        else        {            DS.LAST->next = p;            DS.LAST = p;        }        DS.LAST->next=NULL;    }     return 1;} void create_file(){    /************* WRITE TO FILE *************/    nguoi *p;    p = DS.FIRST;    FILE *file_name;    char ten[20], diachi[127], ngaysinh[10], sodt[16],sott[127];    int ret;    if(p==NULL)        printf("現在のデータがない。
    ");    else    {        if((file_name=fopen("kadai2-5.csv","w")) == NULL)        {            puts("Can not open this file
    ");        }        else        {            while(p!=NULL)            {                fprintf(file_name,"%s,%s,%s,%s,%s
    ",p->sott,p->ten,p->diachi,p->ngaysinh,p->sodt);                p=p->next;            }        }        fclose(file_name);    }} void read_from_file(list &DS){    nguoi *p,*p1;    nguoi start_point;    p = &start_point;    p->next=NULL;    //p = new nguoi;    FILE *file_name;    int ret;    char ten[20], diachi[127], ngaysinh[10], sodt[16],sott[127];    DS.FIRST=DS.LAST=NULL;    if((file_name=fopen("kadai2-5.csv","r")) == NULL)    {        puts("Can not open this file
    ");    }    else    {        p=(nguoi *)malloc(sizeof(nguoi));         while((ret=fscanf(file_name,"%[^,], %[^,], %[^,], %[^,], %[^
    ]
    ",&p->sott,&p->ten,&p->diachi,&p->ngaysinh,&p->sodt))!=EOF)        {            if(DS.FIRST!=NULL)            {                DS.LAST->next=p;                DS.LAST=p;                          }            else            {                DS.FIRST=p;                DS.LAST=p;            }            printf("%s:::%s:::%s:::%s:::%s
    ",p->sott,p->ten,p->diachi,p->ngaysinh,p->sodt);        }        DS.LAST->next=NULL;    }    } void clear_data_in_memory(list &DS){    nguoi *p1;    p1=DS.FIRST;    while(DS.FIRST!=NULL)    {        p1=DS.FIRST;        DS.FIRST=p1->next;        delete p1;    }    DS.LAST==NULL;}void output_all(list DS, int phanloai){    /************ phanloai ****************        * phanloai = 1 ---> 一覧表示        * phanloai = 2 ---> 一件表示のインデックス    **************************************/    nguoi *genzai;    genzai = DS.FIRST;    if(genzai == NULL)        printf("入力件数は0だった
    
    ");    else// if(phanloai == 1)    {        printf("△△△△△入力データ△△△△△
    ");        while(genzai != NULL)        {                       if(phanloai==1)            {                printf("----------------------------------------
    ");                printf("名前 : %s
    住所 : %s
    生年月日 : %s
    電話番号 : %s
    ",genzai->ten,genzai->diachi,genzai->ngaysinh,genzai->sodt);            }            else            {                printf("%s/  %s
    ",genzai->sott,genzai->ten);            }            genzai = genzai->next;        }        printf("
    ----------- 表示終了 -----------
    ");    }}void output_1ken(list DS,int sothu){    char s[5];     sprintf(s,"%d",sothu);    nguoi *hien;    hien = DS.FIRST;    if(hien==NULL)        printf("データがない
    ");    else    {        while(hien!=NULL)        {            if(strcmp(hien->sott,s)==0)            {                printf("---------------------
    ");                printf("名前 : %s
    住所 : %s
    生年月日 : %s
    電話番号 : %s
    
    ",hien->ten,hien->diachi,hien->ngaysinh,hien->sodt);                break;              }            hien=hien->next;        }    }}            void delete_index (list &DS, int somuonxoa){    nguoi *truockhixoa, *saukhixoa;    truockhixoa = DS.FIRST;    char muonxoa[5];    sprintf(muonxoa,"%d",somuonxoa);    while(truockhixoa->next!=NULL  && strcmp(truockhixoa->sott, muonxoa) !=0)    {        saukhixoa = truockhixoa;        truockhixoa = truockhixoa->next;    }    if(truockhixoa->next == NULL && strcmp(truockhixoa->sott, muonxoa) != 0 )        printf("削除したいデータがない
    ");    else    {        if(truockhixoa == DS.FIRST)        {            DS.FIRST=DS.FIRST->next;            delete truockhixoa;         }        else        {            if(truockhixoa == DS.LAST)            {                saukhixoa->next = NULL;                DS.LAST = saukhixoa;                delete truockhixoa;            }            else            {                saukhixoa->next = truockhixoa->next;                delete truockhixoa;            }        }    }} void edit_index(list &DS,int somuonsua){    nguoi *sua;    sua = DS.FIRST;    char muonsua[5];    sprintf(muonsua,"%d",somuonsua);    sua = DS.FIRST;    if(sua == NULL)        printf("修正したいデータがない。
    ");    else    {        while(sua!=NULL)        {            if(strcmp(sua->sott,muonsua) == 0)            {                input_process(DS,somuonsua,1,sua);                break;            }            sua = sua->next;        }    }} int edit_index_number(list &DS){    int cnt=1;    nguoi *p;    p=DS.FIRST;    char somuonsua[5];        while(p!=NULL)    {        sprintf(somuonsua,"%d",cnt);        cnt++;        strcpy(p->sott,somuonsua);        p=p->next;    }    return cnt;}             /********* CHECK NAME *********/int kiemtraName(char *ss){    /*************************************************        * b = 0 : IS OKIE        * b = 1 : FULL SPACE or TAB    **************************************************/    /********** phanloai **************        * phanloai = 1 ---> 名前 : 限定入力桁数:20        * phanloai = 2 ---> 住所 : 限定入力桁数:128    **********************************/        int b=0,cnt=0,dem,i;    char s[128];    char *p;        strcpy(s,ss);    dem=strcmp(s," ");    i=strlen(s);     for(cnt=0;cnt<i;cnt++)    {        if(s[cnt]!=' ' && s[cnt]!=0x09)        {            b=0;            break;        }        else            b=1;    }            return b;} /********* CHECK BIRTHDAY **********/int kiemtrabirthday(char *ss){    /***************************************        * b = 0 : IS OKIE        * b = 1 : スラッシュと数字以外    ***************************************/    int b=0,i;    char s[127];    char *p;        strcpy(s,ss);    i=strlen(s);    for(p=s;*p!='\0';p++)    {        if(isalnum(*p) || *p=='/')        {            if((!isalpha(*p)) || *p=='/')            {                b=0;            }            else            {                b=1;                break;            }        }        else        {            b=1;            break;        }    }     return b;} /********* CHECK TEL NUMBER ***********/int kiemtrasdt(char *ss){    /******************************************        * b = 0 : IS OKIE        * b = 1 : スラッシュと数字以外    *******************************************/    int b=0,i=0;    char s[127];    char *p;        strcpy(s,ss);    i=strlen(s);    for(p=s;*p!='\0';p++)    {        if(isalnum(*p) || *p=='-')        {            if((!isalpha(*p)) || *p=='-')            {                b=0;            }            else            {                b=1;                break;            }        }        else        {            b=1;            break;        }    }    return b;}

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Try this:

    Mã:
    void read_from_file(list &DS){    nguoi *p;    FILE *fInput;    int ret;        DS.FIRST = DS.LAST = NULL;        if((fInput = fopen("kadai2-5.csv","r")) == NULL)        puts("Can not open this file
    ");    else    {        do        {            p = (nguoi*)malloc(sizeof(nguoi));                        ret = fscanf(fInput, "%[^,], %[^,], %[^,], %[^,], %[^
    ]
    ",            p->sott, p->ten, p->diachi, p->ngaysinh, p->sodt);                        if (ret == EOF)            {                free(p);                break;            }            if(DS.FIRST == NULL)                DS.FIRST = p;            else                DS.LAST->next = p;             DS.LAST = p;             printf("%s:::%s:::%s:::%s:::%s
    ",p->sott,p->ten,p->diachi,p->ngaysinh,p->sodt);         }while(1);        DS.LAST->next=NULL;    }}

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    okie, đã chạy ^-^
    thanks doicanhden nhiều.

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    hỏi về cách xóa toàn bộ dữ liệu trong DSLK đơn

    mình có code nhập dữ liệu như sau :

    Mã:
    int input_cstring(char* szString, int size){    char ch = 0;    int count = 0;    for (count = 0; count < size; count++)    {        if(count<size)        {            ch = getchar();            if (ch == '
    ')                break;            szString[count] = ch;        }    }    szString[count] = '\0';    return count;}int input_process(list &DS,int stt, int phanloai, nguoi *sua){    /******************* phanloai *******************            * phanloai = 0 ---> 入力            * phanloai = 1 ---> 修正    *************************************************/    nguoi *p;    p = new nguoi;    if(phanloai == 1)//修正する場合            p = sua;     sprintf(p->sott,"%d",stt);        int kiemtra=3;    printf("--------------入力------------
    ");    printf("名前入力 : ");    do    {        if(0==input_cstring(p->ten,20))            return 0;        fflush(stdin);        kiemtra = kiemtraName(p->ten);        if(kiemtra == 1)            printf("名前再入力 : ");                }while(kiemtra!=0);  /*************************************************************/     printf("住所入力 : ");    do    {        if(0==input_cstring(p->diachi,128))            return 0;        fflush(stdin);        kiemtra = kiemtraName(p->diachi);        if (kiemtra == 1)            printf("住所再入力 : ");    }while (kiemtra!=0);  /*************************************************************/        printf("生年月日入力 : ");    do    {        if (0 == input_cstring(p->ngaysinh, 10) )            return 0;        fflush(stdin);        kiemtra = kiemtrabirthday(p->ngaysinh);        if(kiemtra == 1)            printf("生年月日入力は間違っている。
    生年月日再入力 : ");    }while (kiemtra != 0);  /*************************************************************/        printf("電話番号入力 : ");    do    {        if (0 == input_cstring(p->sodt, 16) )            return 0;        fflush(stdin);        kiemtra=kiemtrasdt(p->sodt);        if(kiemtra==1)            printf("電話番号入力は間違っている。
    電話番号再入力 : ");    }while ( kiemtra != 0);            /************* DSに繋がる **************/    /*-------- phanloai = 1 ---> EDIT DATA -----*/      if(phanloai!=1)    {        if(DS.FIRST == NULL)        {            DS.FIRST = p;            DS.LAST = p;        }        else        {            DS.LAST->next = p;            DS.LAST = p;        }        DS.LAST->next=NULL;    }     return 1;}
    và code xóa toàn bộ list như sau

    Mã:
    void clear_data_in_memory(struct nguoi *p){    struct nguoi *p1;    while(p!=NULL)    {        p1=p->next;        free(p);        p=p1;    }}
    nhưng khi chạy phần xóa danh sách liên kết thì nó bị văng ra.
    Bạn nào sửa lại giùm mình với, cám ơn nhiều !!!

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    thanks mọi người đã đọc, mình làm được rồi [IMG]images/smilies/smile.png[/IMG] nhưng giờ bị vướng ở chỗ đọc dữ liệu từ file vào danh sách liên kết đơn.
    Code đọc dữ liệu từ file như sau

    Mã:
    void read_from_file(list &DS){    nguoi *p;    p = new nguoi;    FILE *file_name;    int ret;     if((file_name=fopen("kadai2-5.csv","r")) == NULL)    {        puts("Can not open this file
    ");    }    else    {        while((ret=fscanf(file_name,"%[^,], %[^,], %[^,], %[^,], %[^
    ]
    ",p->sott,p->ten,p->diachi,p->ngaysinh,p->sodt))!=EOF)        {            if(DS.FIRST==NULL)            {                DS.FIRST=p;                DS.LAST=p;                printf("Da doc danh DS.FIRST
    ");            }            else            {                //if(strcmp(p->sott,"
    ")!=0)                //{                    DS.LAST->next=p;                    DS.LAST=p;                    printf("DS ke tiep
    ");                //}            }            DS.LAST->next=NULL;        }        printf("DS.FIRST = %s
    ",DS.FIRST);    }}
    Nhưng khi chạy thì nó chỉ đọc dc DS.FIRST luôn luôn là giá trị cuối cùng trong file. File được định dạng theo kiểu : sott,tên,địa chỉ,ngày sinh,số đt

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •