gpt4 book ai didi

c - 我的 C 程序跳过 fprinf 并且没有将数据写入指定文件,特别是在第一个 printf 之后

转载 作者:行者123 更新时间:2023-11-30 19:34:39 25 4
gpt4 key购买 nike

问题

程序在第一个 printf 之后没有向文件写入任何内容(请删除 getchar() 以测试程序)。我不知道程序发生了什么:它运行顺利,但没有写入文件。

代码

#include <stdio.h>
#include <stdlib.h>
#include <windows.h> // Header for Windows API that is used by Windows programmers
#include <stdlib.h>
int add_book(){
// begin function
char choice;
int book_continue=0;

char book_title[200], book_author[200];
int book_isbn;
int lib_id;
} ;
struct Library profile[60];//Declaration of array of struct.
FILE *libraryrecords;

system("Color B0");
libraryrecords = fopen("Library Book Records.txt","w");//file logs has been opened in append mode
while (book_continue<30) {
printf("Enter the Title of a Book\n\n");
scanf("%s", profile[library_amount].book_title);// reads the variable
printf("Enter the Book's Author\n\n");
scanf("%s", profile[library_amount].book_author);
printf("Enter the Book's ISBN\n\n");
scanf("%d", &profile[library_amount].book_isbn);
printf("Enter the state of the Book\n\n");
scanf(" %c",&state_of_book);//allows the program to recognise a single character with white space behined %c
printf("Please enter the borrowed date of the book :\n\n");
scanf("%d %d %d", &day_borrowed, &month_borrowed, &year_borrowed);//this reads three integer variables
printf("Please enter the Return Date :\n\n");
scanf("%d %d %d", &day_returned, &month_returned, &year_returned);
printf("Enter his/her library id\n\n");
scanf("%d", &profile[library_amount].lib_id);
printf("Type a to Continue or b to Stop \n\n\n");
scanf(" %c", &choice);

printf("\n\n\n\n\n\n\n\n\n\t\t\t\t Loading");
system("cls");
Sleep(600);
printf("...");

system("cls"); // Clears the screen
if(choice== 'b'){
printf("\n||===================================================================================||\n");
printf("The book record:\t Book Title: %s, \t Book isbn: %d, \t Author : %s", profile[library_amount].book_title, profile[library_amount].book_isbn, profile[library_amount].book_author);
printf("\n||===================================================================================||\n");
Sleep(200);
getchar()
fprintf(libraryrecords,"\n||===================================================================================||\n");
fprintf(libraryrecords,"The book record:\t Book Title: %s, \t Book isbn: %d, \t Author : %s", profile[library_amount].book_title, profile[library_amount].book_isbn, profile[library_amount].book_author);
fprintf(libraryrecords,"\n||===================================================================================||\n");
Sleep(100);
}

system("cls");
if (day_returned-day_borrowed>10){
day_returned=day_late;
month_returned=month_late;
year_returned=year_late;
fprintf(libraryrecords,"\n================================================================\n");
fprintf(libraryrecords,"\n\tThe Late Book Return Fee is:\t Fee: %f", late_bookreturn_fee);
Sleep(600);
fprintf(libraryrecords,"\n==================================================================================\n");
fprintf(libraryrecords,"\nPrint The Personal Information of the Person(s) Who is/are Expected to Pay the Fee:\t Name: %s, \t Library id: %d", profile[library_amount].student_mem_name, profile[library_amount].lib_id);
Sleep(600);
printf("...");
system("cls"); // Clears the screen
/* This is the nested if else statement due to the amount of expressions where the the { starts after the else if statement*/
if (state_of_book== 'a'){//if condition of if 'a' is true
fprintf(libraryrecords,"\nThe book is in excellent condition\n\n");//
Sleep(600);
printf("...");
}//end if
else if (state_of_book== 'b'){
fprintf(libraryrecords,"\nThe Book is in Good Condition\n\n");
printf("...");
Sleep(600);
}//end else if

else if (state_of_book== 'c'){
fprintf(libraryrecords,"\n===============================================\n");
fprintf(libraryrecords,"\nThe Book is in Terrible Condition\n");
book_repayment++;
fprintf(libraryrecords,"\nThe person(s) Who are Required to Repay for a Book are: \n \t Book repayment: %f, \t Library id: %d", book_repayment, profile[library_amount].lib_id);
printf("...");
Sleep(800);
system("cls");
break;
fclose(libraryrecords);
}//end else if
}//end if
}//end if
return 0;
}//end function

最佳答案

大部分代码不在函数中,因此它不执行任何操作。 add_book() 提前结束。

int add_book(){
// begin function
char choice;
int book_continue=0;

char book_title[200], book_author[200];
int book_isbn;
int lib_id;
} ; <<<<<<<<------ add_book() ENDS HERE
struct Library profile[60];//Declaration of array of struct.
FILE *libraryrecords;

system("Color B0");
libraryrecords = fopen("Library Book Records.txt","w");//file logs has been opened in append mode
while (book_continue<30) {
printf("Enter the Title of a Book\n\n");
scanf("%s", profile[library_amount].book_title);// reads the variable

您应该收到一条警告,指出存在如下问题:

test.c:12:1: warning: control reaches end of non-void function [-Wreturn-type]
} ;
^
test.c:12:3: warning: extra ';' outside of a function [-Wextra-semi]
} ;
^

大多数编译器默认情况下没有警告。使用命令行编译器,通常可以使用 -Wall 标志打开大多数警告。打开它们,修复它们。

这也是关于正确缩进的一个很好的教训。如果代码缩进正确,问题就会立即显而易见......

int add_book(){
// begin function
char choice;
int book_continue=0;

char book_title[200], book_author[200];
int book_isbn;
int lib_id;
} ;
struct Library profile[60];//Declaration of array of struct.
FILE *libraryrecords;

system("Color B0");
libraryrecords = fopen("Library Book Records.txt","w");//file logs has been opened in append mode
while (book_continue<30) {
printf("Enter the Title of a Book\n\n");
scanf("%s", profile[library_amount].book_title);// reads the variable

请注意 struct Library profile[60]; 及其后面的所有内容如何从 add_book 的其余部分向左缩进。这在视觉上表明它已经逃脱了 add_book 的范围。

大多数优秀的编辑器都有一种自动为您缩进代码的方法。如果你的没有,Atom是一个不错的选择。它是免费的,与 Git 集成良好,跨平台,有很多功能和插件。自动缩进位于编辑 -> 行 -> 自动缩进中。

<小时/>

从这里开始,下一个问题是你永远不会检查 fopen 是否工作。检查每个系统调用是否成功非常重要。典型的模式是这样的:

#include <stdlib.h>   // for exit()
#include <string.h> // for strerror()
#include <errno.h> // for errno

const char library_file[] = "Library Book Records.txt";
FILE *library_fp = fopen(library_file,"w");
if( library_fp == NULL ) {
fprintf( stderr, "Could not open '%s' for writing: %s", library_file, strerror(errno) );
exit(1);
}

您尝试系统操作,检查是否成功。如果没有,请打印有用的错误消息并退出(或执行其他操作来处理问题)。 errno 是一个全局变量,包含上次系统操作的错误,如 fopen。但它是一个数字,strerror(errno) 将其转换为人类可读的字符串。

关于c - 我的 C 程序跳过 fprinf 并且没有将数据写入指定文件,特别是在第一个 printf 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43566049/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com