社員番号(5byte)氏名(20byte)給与(7byte)CR/LF固定(2byte)
このデータをファイルに登録するプログラムを作っています。
新規で登録する場合と更新する場合を考えなければならないのですが、
更新がうまく作動していません。
具体的にはレコードの検索処理内のbの値がずっと0のままになっています。
原因が分かるでしょうか?
#include <stdio.h> #include <fcntl.h> #include <memory.h> #include <string.h> #include <io.h> #include <errno.h> #include <math.h> #include <stdlib.h> #include <sys\types.h> #include <sys\stat.h> /*********************************************************************/ /* 定数の定義 */ /*********************************************************************/ #define CR 0x0D #define LF 0x0A #define DATA "SYAIN.DAT" /*********************************************************************/ /* 構造体の宣言 */ /*********************************************************************/ struct syain_k { char sno[5]; /* 社員番号 */ char name[20]; /* 氏名 */ char salary[7]; /* 給与 */ char crlf[2]; /* CR/LF */ }; /*********************************************************************/ /* 静的変数の定義 */ /*********************************************************************/ /*********************************************************************/ /* 内部関数のプロトタイプ宣言 */ /*********************************************************************/ int numeric_chk(char *str,int n); int search(char *sno,struct syain_k *rec); void data_disp(struct syain_k *rec); void touroku(int recno, char *sno,char *name,char *salary); /*********************************************************************/ /* main() メイン関数 */ /*********************************************************************/ int Fd; initial_syori() { Fd = open(DATA,O_RDWR|O_CREAT, S_IREAD | S_IWRITE); } main() { struct syain_k rdbuf; int a; long d; int recno; char sno[6]; char name[21]; char salary[8]; initial_syori(); while(1) { printf("社員番号を入力してください\n"); scanf("%s",sno); a=numeric_chk(sno, 5); if (a==-1) { printf("NUMERICエラー\n"); continue; } d=atol(sno); if (d==99999L) { return 0; } else if ((d<1000L)||(d>90000L)) { printf("範囲エラー\n"); continue; } recno = search(sno, &rdbuf); if (recno!=0) data_disp(&rdbuf); printf("氏名の入力\n"); scanf("%s",name); printf("氏名[%s]\n",name); printf("給与を入力してください\n"); scanf("%s",salary); a=numeric_chk(salary, 7); if (a==-1) { printf("NUMERICエラー\n"); continue; } printf("給与:[%s]\n",salary); touroku(recno, sno, name, salary); } } /**************************************l*******************************/ /* 数字か文字かのチェック */ /* IN: char *str チェックを行う文字列 */ /* in: int n チェックを行う文字列のバイト数 */ /* 戻り値 0:全て数字: -1:数字以外の文字あり */ /*********************************************************************/ int numeric_chk(char *str, int n) { int i; for(i=0;i<n;i++) { if ((str[i]>='0')&&(str[i]<='9')) { continue; } else { return(-1); } } return 0; } /*********************************************************************/ /* レコードの検索処理 */ /* IN: *sno キー入力された社員番号 */ /* OUT: *rec 社員番号が一致した時の既存レコード */ /* 戻り値 0・・・存在しない */ /* 1~・・・既存のレコード番号 */ /*********************************************************************/ int search(char *sno, struct syain_k *rec) { int recno; int n; long a, b; recno = 0; a = atol(sno); lseek(Fd,0L,SEEK_SET); while(1) { n = read(Fd,&rec,sizeof(struct syain_k)); b=atol(rec->sno); if (n==0) { recno=0; return 0; } else { recno++; if(a==b) printf("P1"); getch(); return recno; } } } /*********************************************************************/ /* 既存データの表示処理 */ /* IN: rec 社員番号が一致した時のレコード */ /*********************************************************************/ void data_disp(struct syain_k *rec) /*表示処理*/ { int X; X=atoi(rec->sno); switch(X) { case0: printf("役員\n"); break; case1: printf("部長\n"); break; case2: printf("課長\n"); break; case3: printf("係長\n"); break; case4: printf("主任\n"); break; default: printf(" \n"); } printf("%s", rec->name); printf("%s", rec->salary); } /*********************************************************************/ /* 登録処理 */ /* IN: recno・・・レコード番号 */ /* 0:更新レコードは存在しない */ /* 1~:更新するレコード番号 */ /* IN: *sno・・・社員番号 */ /* IN: *name・・・氏名 */ /* IN: *salary・・・給与 */ /*********************************************************************/ void touroku(recno, sno, name, salary) int recno; char *sno, *name, *salary; { struct syain_k wtbuf; memcpy(wtbuf.sno, sno, 5); memcpy(wtbuf.name, name, 20); memcpy(wtbuf.salary, salary, 7); wtbuf.crlf[0]=CR; wtbuf.crlf[1]=LF; if (recno==0) lseek(Fd,0L,SEEK_END); else lseek(Fd,34*(recno-1),SEEK_SET); write(Fd,&wtbuf,sizeof(wtbuf)); } コード
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/24 01:27