고수님들...제발 도와주세요....이부분만..해결이 안되여....

익명 사용자의 이미지

이 프로그램은 제가 그냥 간단하게 디비를 공부하려구 짠 건데여..

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);
}

익명 사용자의 이미지

rw로 열면 될것입니다.
^^;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.