Em có 2 hàm sau!
Hàm ghi file
void WriteFile(const char* link, const char* mode){
char* content = (char*)malloc(BUFFER);
FILE* opf = OpenFile(link, mode);
if(opf != NULL){
while(TRUE){
if(WriteContent(&content)){
fputs(content, opf);
}
else{
puts("jk inserted!");
break;
}
}
}
else{
fputs("Cann't open file!", stdout);
}
}
hàm nhận nội dung từ người dùng
int WriteContent(char** content){
char* tmp = (char*)malloc(BUFFER);
char* condition = (char*)malloc(4);
condition[0] = 'j';
condition[1] = 'k';
condition[2] = '\n';
condition[3] = '\0';
fflush(stdin);
fgets(tmp, BUFFER, stdin);
if(strlen(tmp) == 3){
if(strcmp(tmp, condition) == 0){
free(condition);
return FALSE;
}
}
strcpy(*content, tmp);
free(tmp);
free(condition);
return TRUE;
}
- Khi viết nội dung vào file thì luôn có 1 dòng đầu tiên là rỗng
- Nhập jk thì thông báo “jk inserted” nhưng sau đó chương trình lại thông báo core dump
Mong mọi người hướng dẫn giúp!
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?