Created
May 6, 2020 19:06
-
-
Save deeev-sb/e94864ca9538548d77581ca377d000bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <fcntl.h> | |
#define NUM_RECORDS 100 // record에는 최대 100개의 정보 기록 가능 | |
struct record { // 하나의 record에 기록되는 정보 | |
char name[20]; // account owner | |
int id; // account number | |
int balance; // 계좌 잔고 | |
}; | |
void get_new_record(struct record *curr); | |
void display_record(struct record *curr); | |
int main( int argc, char *argv[]) { | |
FILE *fp; | |
struct record current; | |
int record_no; | |
int fd; | |
long pos; | |
char yes; | |
fd = open( argv[1], O_RDWR | O_CREAT , S_IRUSR | S_IWUSR); // account file open | |
if(( fp = fdopen( fd, "r+")) == NULL) { // file descriptor로 open한 file을 불러옴 | |
perror(argv[1]); | |
exit(2); | |
} | |
printf("enter record number: "); // user로부터 record number를 입력받음 | |
scanf("%d",&record_no); // record_no는 입력받은 record number | |
getchar(); | |
while (record_no >= 0 && record_no < NUM_RECORDS) { | |
pos = record_no * sizeof(struct record); // record number에 해당되는 file position 계산 | |
fseek( fp, pos, SEEK_SET); // record의 해당 position 위치를 찾아냄 | |
fread(¤t, sizeof(struct record), 1, fp); // 읽은 record 정보를 current에 저장 | |
display_record (¤t); // current (record 정보 - name, id, balance) 출력 | |
printf("update records? yes = y\n"); // user에게 record 정보를 update 할 것인지 물어봄 | |
scanf("%c", &yes); getchar(); | |
if (yes == 'y') { // user가 record 정보 update 요청 시 | |
get_new_record(¤t); // record 정보 업데이트 | |
fseek( fp, pos, SEEK_SET); // 해당 record 위치에 | |
fwrite( ¤t, sizeof( struct record), 1, fp); // update된 정보 입력 | |
printf("update done\n"); | |
} | |
printf("enter record number: "); | |
scanf("%d",&record_no); | |
getchar(); | |
} | |
fclose(fp); | |
} | |
void get_new_record(struct record *curr) { // record 정보 업데이트 함수 | |
printf("> id? "); | |
scanf("%d", &curr->id); | |
printf("> name? "); | |
scanf("%s", curr->name); | |
printf("> balance? "); | |
scanf("%d", &curr->balance); | |
} | |
void display_record(struct record *curr) { // record 정보 출력 함수 | |
printf("\n"); | |
printf("id: %d \n", curr->id); | |
printf("name: %s \n", curr->name); | |
printf("balance: %d \n", curr->balance); | |
printf("\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment