在下面的代码中,我试图从file.txt中插入,显示和删除数据。我能够成功插入,但是我的显示功能显示不正确,并且我不知道如何设置执行删除功能的逻辑。我希望能在更改代码方面有所帮助,使其能够正常工作。我该如何设置逻辑?
#include <stdio.h>
void menu() {
printf("Option 1 - Create a file and insert data:\n");
printf("Option 2 - Read file and display:\n");
printf("Option 3 - Delete content:\n");
printf("Option 4 - Exit:\n");
};
struct Book {
char title[256];
char author[256];
int pages;
int price;
};
void get() {
struct Book *book;
book = (struct Book*)malloc(sizeof(struct Book));
FILE *fPtr;
fPtr = fopen("file.txt", "w");
if (fPtr == NULL) {
printf("Fail creating file");
getch();
exit(1);
};
for (int i = 0; i < 1; i++) {
printf("Enter the book title:\n");
scanf("%s", &book[i].title);
fprintf(fPtr, "Title = %s", book[i].title);
printf("Enter the author of the book:\n");
scanf("%s", &book[i].author);
fprintf(fPtr, "Author = %s", book[i].author);
printf("Enter the number of pages:\n");
scanf("%d", &book[i].pages);
fprintf(fPtr, "Pages = %d", book[i].pages);
printf("Enter the price:\n");
scanf("%d", &book[i].price);
fprintf(fPtr, "Price = %d", book[i].price);
};
/*for (int i = 0; i < 1; i++) {
printf("%s\n", book[i].title);
printf("%s\n", book[i].author);
printf("%d\n", book[i].pages);
printf("%d\n", book[i].price);
};*/
fclose(fPtr);
};
void display() {
FILE *fPtr;
fPtr = fopen("file.txt", "r");
printf("The content of file are:\n", fPtr);
/*struct Book *book;
book = (struct Book*)malloc(sizeof(struct Book));
printf("%s %s %d %d", book.title, book.author, book.pages, book.price);*/
free(book);
fclose(fPtr);
}
int main()
{
int opt = 0;
int opt2 = 0;
int var = 0;
int validation = 0;
while (opt != 4) {
menu();
do
{
printf("Choose an option:\n");
validation = scanf_s("%d", &opt);
while (getchar() != '\n');
} while (validation != 1);
switch (opt) {
case 1:
get();
printf("Option 5 - Display data:\n");
printf("Option 6 - Delete:\n");
scanf("%d", &var);
if (var == 5) {
//FILE *fp1;
/*fp1 = fopen("file.txt", "r");*/
display();
}
else if (var == 6) {
printf("delete!");
}
break;
case 2:
printf("First insert data:\n");
break;
case 3:
printf("First insert data:\n");
break;
case 4:
return 0;
}
}
}
答案 0 :(得分:1)
我建议您将 Books 重命名为 Book ,因为 struct 管理一本书,而不是几本书。
关于要输入的命令代码,而不是1 .. 4“ c”表示创建,“ r”表示读取,“ d”表示删除,“ e”表示退出怎么办?
关于get()
:
struct Book book;
for (int i = 0; i < 1; i++)
是无用的,相当于int i = 0
,您真的只管理一本书吗?Title =
这样的每个数据加上前缀?使用这些前缀读取文件内容更加复杂关于display()
:
for (int i = 0; i < 1; i++)
是无用的,等效于int i = 0
struct Book book;
关于display()
:
for (int i = 0; i < 1; i++)
是无用的,等效于int i = 0
struct Book book;
关于deleteStudentRecord()
:
getNoOfRecords()
未定义,但当前它必须返回1,因为您仅管理文件中的一本书fread(&var, sizeof(struct student), 1, ptr)
是错误的,因为它假定一行始终是sizeof(struct student)
,但是引用get()
的情况并非如此fcloseall()
?只需执行fclose(ptr);
和fclose(ptr2)
,将它们重命名为fpIn和fpOut或类似的方法即可使您的代码更具可读性答案 1 :(得分:1)
注意:display()提案2
void display() {
FILE *fp;
fp = fopen("file.txt", "r");
struct Book book;
printf("%s %s %d %d", book.title, book.author, book.pages, book.price);
free(book);
fclose(fp);
}
答案 2 :(得分:1)
“ get();提案2”
void get() {
FILE *fPtr;
fPtr = fopen("file.txt", "w");
struct Book book;
if (fPtr == NULL) {
printf("Fail creating file");
getch();
exit(1);
};
printf("Enter the book title:\n");
scanf("%s", &book.title);
fprintf(fPtr, "%s", book.title);
printf("Enter the author of the book:\n");
scanf("%s", &book.author);
fprintf(fPtr, "%s", book.author);
printf("Enter the number of pages:\n");
scanf("%d", &book.pages);
fprintf(fPtr, "%d", book.pages);
printf("Enter the price:\n");
scanf("%d", &book.price);
fprintf(fPtr, "%d", book.price);
/*for (int i = 0; i < 1; i++) {
printf("%s\n", book[i].title);
printf("%s\n", book[i].author);
printf("%d\n", book[i].pages);
printf("%d\n", book[i].price);
};*/
fclose(fPtr);
};