고수님들...제발 도와주세요....이부분만..해결이 안되여....
이 프로그램은 제가 그냥 간단하게 디비를 공부하려구 짠 건데여..
del_record 함수에서 파일을 수정모드로 여는방법을 모르겠어여
fopen으로 파일을 열때 r+모드로 열면된다고하는데
왜 데이터를 읽고나서 바로 그자리에 수정된 데이터를 기록할 수 없는지
모르겠어영ㅜㅜ
고수님들,, del_record 함수에서 데이터를 고쳐서 기록할 수 있게 쫌 고쳐
주세여
/************************/
/* HEADER FILE for FS.c */
/************************/
#define DATAFILE "datafile.txt"
#define MAX_REC 20
#define MAX_SCORE 3
#define MAX_DEPNUM 10
#define MAX_STRLEN 20
#define RECORD_OFFSET 34
struct record_struct {
char mark;
char dep_no[MAX_DEPNUM];
char name[MAX_STRLEN];
char score[MAX_SCORE];
};
typedef struct record_struct student_type;
int display_menu(void);
int get_student(student_type *);
void print_student(student_type *);
int getin(void);
void getstr(char *, int);
int file_append(student_type *);
student_type *find_record(char *);
void del_record(char *);
/* End of Header FS.h */
/*********************************************/
/* By kwool Created 02/01/14 135457 */
/* source modified by 9757070 Jeong Sangjun */
/* FS.c */
/*********************************************/
#include
#include
#include
#include "fs.h"
int record_number;
// ***************
// Function Main
// ***************
int main(int argc, char **argv)
{
int ch = -1;
char rec_num[MAX_DEPNUM];
student_type student;
student_type *find_result;
while( ch != 0 ) {
printf("\n");
printf(" 1 insert record \n");
printf(" 2 select record \n");
printf(" 3 delete record \n");
printf(" 0 exit record \n");
printf("Enter a choice ");
switch( ch = getin() ) {
case 1
if( get_student(&student) )
file_append(&student);
break;
case 2
printf("Enter your student
number ");
getstr(rec_num, MAX_DEPNUM);
find_result = find_record
(rec_num);
if( find_result !=
(student_type *)NULL ) print_student(find_result);
break;
case 3
printf("Enter your student
number ");
getstr(rec_num, MAX_DEPNUM);
del_record(rec_num);
break;
case 0
printf("Bye !\n");
break;
default
printf("Please, choose correct
menu number!\n");
}
}
return 0;
}
// **********************
// Function file_append
// **********************
int file_append(student_type *rec)
{
FILE *fp;
int wcnt;
print_student(rec);
if( (fp = fopen(DATAFILE, "ab")) == (FILE *)NULL) {
printf("ERROR File open error!\n");
return(-1);
}
if( (wcnt = fwrite((char *)rec, 1, RECORD_OFFSET, fp)) <=
0 ) {
perror(" File Write Error\n");
fclose(fp);
return(-1);
}
printf("ERROR File Append Succes (%d)\n", wcnt);
fclose(fp);
return(wcnt);
}
// **********************
// Function find_record
// **********************
student_type *find_record(char *rec_num)
{
FILE *fp;
int flag, len;
long fpointer;
student_type *record;
len = strlen(rec_num);
if( (len < 1) || (len > 7) ) {
printf("ERROR Wrong input (enter 7-digit
number).\n");
return(NULL);
}
if( ( fp=fopen(DATAFILE, "r") ) == (FILE *)NULL ) {
printf("ERROR File open error\n");
return(NULL);
}
record = (student_type *)malloc(RECORD_OFFSET);
record_number = 0;
fpointer = 0;
flag = 0;
while( !feof(fp) ) {
fseek(fp, fpointer, SEEK_SET);
if( fread((void *)record, 1, RECORD_OFFSET, fp) >
0 ) {
if( !strncmp(rec_num, record->dep_no,
strlen(rec_num)) ) {
flag = 1;
break;
}
record_number++;
fpointer += RECORD_OFFSET;
}
if(flag == 1) break;
}
fclose(fp);
if(flag == 1) {
printf("*** Record Found ***\n");
return(record);
}
printf("*** Record Not Found ***\n");
return(NULL);
}
// *********************
// Function del_record
// *********************
void del_record(char *rec_num)
{
FILE *fp;
long fpointer;
student_type *record;
if( (fp=fopen(DATAFILE, "r+")) == (FILE *)NULL ) {
printf("ERROR File open error!\n");
return;
}
record_number = 0;
fpointer = 0;
while( !feof(fp) ) {
record = (student_type *)malloc(RECORD_OFFSET);
fseek(fp, fpointer, SEEK_SET);
if( fread((void *)record, 1, RECORD_OFFSET, fp) >
0 ) {
if( !strncmp(rec_num, record->dep_no,
strlen(rec_num)) ) {
record->mark = 0;
memset(record->name, 0x00,
MAX_STRLEN);
if( fwrite((char *)record, 1,
RECORD_OFFSET, fp) > 0 ) printf("*** Record deleted ***\n");
else printf("*** Cannot delete
***\n");
}
record_number++;
fpointer += RECORD_OFFSET;
}
free(record);
}
fclose(fp);
}
// **********************
// Function get_student
// **********************
int get_student(student_type *record)
{
int i, len, score;
char str[128];
// 학번 입력
printf("Enter student number ");
getstr(str, MAX_DEPNUM);
// 학번 check
len = strlen(str);
if( (len < 1) || (len > 7) ) {
printf("ERROR Wrong input (enter 7-digit
number).\n");
return 0;
}
strncpy(record->dep_no, str, MAX_STRLEN);
// 이름 입력
printf("Enter student name ");
getstr(str, MAX_STRLEN);
// 이름 check
len = strlen(str);
if( len > MAX_STRLEN ) {
printf("ERROR Wrong input.\n");
return 0;
}
strncpy(record->name, str, MAX_STRLEN);
// 성적 입력
printf("Enter total %d scores\n", MAX_SCORE);
for(i=0; i
printf("Enter score %02d ", i+1);
scanf("%d", &score);
if( (score < 0) || (score > 100) ) { // 성적 check
printf("ERROR Wrong range.\n");
return 0;
}
record->score[i] = score;
}
// 새 레코드 표시
record->mark = 1;
return 1;
}
// ************************
// Function print_student
// ************************
void print_student(student_type *record)
{
int i;
printf("\nRecord Number %d\n", record_number);
printf("***************************************\n");
printf("* Student No. %7s *\n", record-
>dep_no);
printf("* Name %-10s *\n", record-
>name);
printf("* ----------------------------------- *\n");
for(i=0; i
printf("* Score%02d %3d
*\n", i, record->score[i]);
}
printf("***************************************\n");
}
// ****************
// Function getin
// ****************
int getin()
{
int input;
scanf("%d", &input);
return(input);
}
// *****************
// Function getstr
// *****************
void getstr(char *buf, int maxlength)
{
memset(buf, 0, maxlength+1);
scanf("%s", buf);
}
Re: 고수님들...제발 도와주세요....이부분만..해결이 안되여...
rw로 열면 될것입니다.
^^;
댓글 달기