帮我看看这条c语言程序设计哪错了

21:06 提问
c,请帮我看看这个程序,怎么都找不到错误在哪,编译没问题调试一直出错
#define MAXTITL 40
#define MAXAUTL 40
#define MAXBKS 10
#define CONTINUE 0
#define DONE 1
struct book
char title[MAXTITL];
char author[MAXAUTL];
struct pack
bool delete_
char * s_gets(char *st, int n);
int getlet(const char *s);
int getbook(struct pack*pb);
void update(struct pack*item);
int main(void)
struct pack library[MAXBKS];
int count = 0;
int deleted = 0;
int index, filecount,
int size = sizeof(struct book);
if ((pbooks = fopen("book.dat", "r")) != NULL)
//无法读取内存?但这里我看了很多遍都没有问题呀
while (count & MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
if (count == 0)
puts("current contents of book.dat:");
printf("%s by %s:$%.2f\n", library[count].book.title,
library[count].book.author, library[count].book.value);
printf("do you wish to change or delete this entry?&y/n& ");
if (getlet("yn") == 'y')
printf("enter c to change,d to delete entry:");
if (getlet("cd") == 'd')
library[count].delete_me =
deleted++;
puts("entry marked for deletion.");
update(&library[count]);
fclose(pbooks);
filecount = count -
if (count == MAXBKS)
fputs("the book.dat is full.", stderr);
exit(EXIT_FAILURE);
puts("add new book titles.");
puts("press [enter] at the start of a line to stop.");
while (filecount & MAXBKS)
if (filecount & count)
while (library[open].delete_me == false)
if (getbook(&library[open]) == DONE)
else if (getbook(&library[open]) == DONE)
filecount++;
if (filecount & MAXBKS)
puts("enter the next book title:");
puts("here is the list of your book:");
for (index = 0; index & index++)
if (library[index].delete_me == false)
printf("%s by %s:$%.2f\n", library[index].book.title,
library[index].book.author, library[index].book.value);
if ((pbooks = fopen("book.dat", "w")) == NULL)
fputs("can't open book.dat for output.", stderr);
exit(EXIT_FAILURE);
for (index = 0; index & index++)
if (library[index].delete_me == false)
fwrite(&(library[index].book), size, 1, pbooks);
fclose(pbooks);
puts("done");
getchar();
int getlet(const char * s)
c = getchar();
while (strchr(s,c)==NULL)
printf("enter a character in the list %s\n", s);
while (getchar() != '\n');
c = getchar();
while (getchar() != '\n');
int getbook(struct pack * pb)
int status = CONTINUE;
if (s_gets(pb-&book.title,MAXTITL) == NULL || pb-&book.title[0] == '\0')
status = DONE;
puts("enter the author:");
s_gets(pb-&book.author, MAXAUTL);
puts("enter the value:");
while (scanf("%f", &pb-&book.value) != 1)
puts("use numeric input:");
scanf("%*s");
while (getchar() != '\n');
pb-&delete_me =
void update(struct pack * item)
copy = item-&
puts("enter the letter that indicates your choice:");
puts("t)modify title
a)modify author");
puts("v)modify value
s)saving changes");
puts("q)quit,ignore changes");
while ((c = getlet("tavsq")) != 's'&&c != 'q')
switch (c)
case't':puts("enter new title:");
s_gets(copy.title, MAXTITL);
case'a':puts("enter new author:");
s_gets(copy.author, MAXAUTL);
case'v':puts("enter new value:");
while (scanf("%f", &copy.value) != 1)
puts("enter a numeric value:");
scanf("%*s");
while (getchar() != '\n');
puts("enter the letter that indicates your choice:");
puts("t)modify title
a)modify author");
puts("v)modify value
s)saving changes");
puts("q)quit,ignore changes");
if (c == 's');
item-&book =
char * s_gets(char * st, int n)
char *ret_
ret_val = fgets(st, n, stdin);
if (ret_val)
find = strchr(st, '\n');
*find = '\0';
while (getchar() != '\n');
return ret_
按赞数排序
book.bat的路径写对了吗?这种写法应该是放在项目根目录下吧
pbooks指向的是空地址,无法获取内存值。
pbooks = fopen("book.dat", "r")) != NULL这句话应该是先赋值再判空吧?空的时候就异常了,不懂说得对不对,你试试看!电脑上没环境我就不尝试了。
r方式文件必须存在,你可以换成a+.我怀疑你看了假书
1、初始化指针,否则为野指针
FILE *pbooks=NULL;
2、if语句记得加{}后再写中间的代码,否则if和while作为一句执行
if ((pbooks = fopen("book.dat", "r")) != NULL)
//无法读取内存?但这里我看了很多遍都没有问题呀
while (count & MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
.............
应该是你第一次的 fclose(pbooks);的问题
因为如果第一次运行程序的时候,是不存在book.dat的。
所以pbook是没有指向文件的,然后你又释放文件——fclose(pbooks);
而pbook本身是没有指向文件的,所有释放不了导致出错。
应该把fclose(pbooks);放到 if 的中:
if ((pbooks = fopen("book.dat", "r")) != NULL)
//无法读取内存?但这里我看了很多遍都没有问题呀
while (count & MAXBKS&&fread(&library[count], size, 1, pbooks) == 1)
if (count == 0)
puts("current contents of book.dat:");
printf("%s by %s:$%.2f\n", library[count].book.title,
library[count].book.author, library[count].book.value);
printf("do you wish to change or delete this entry? ");
if (getlet("yn") == 'y')
printf("enter c to change,d to delete entry:");
if (getlet("cd") == 'd')
library[count].delete_me =
deleted++;
puts("entry marked for deletion.");
update(&library[count]);
fclose(pbooks);
其他相关推荐那位高手来帮我看看这条程序哪错了?【c语言吧】_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:555,204贴子:
那位高手来帮我看看这条程序哪错了?收藏
#include &stdio.h&int main(){
int Centigrade , F Fahrenhite = printf("请输入 x "); scanf(%f",&x); Centigeade = ( x - 32 ) * 5 / 9; printf ("/d Fahrenhite\n" , Centigrade);
return 0 ;}
上市公司达内c语言,打造专为培养精通Unix和windows双系统开发c语言工程师c语言,指定c语言名师授课,让您精通应用极其&广泛&,&流行&的编程语言.
"/d Fahrenhite\n"
最后的printf里面的/d改为%d试试看
#include &stdio.h&int main(){
int Centigrade , F Fahrenhite = printf("请输入 x "); scanf("%d",&x); Centigrade = ( x - 32 ) * 5 / 9; printf ("/d Fahrenhite\n" , Centigrade);
return 0 ;}
最后我修改成了这个但是执行后我输入数字后 却没有对应的数值
Fahrenhite =int
如果我是那x,我会疯的!
能执行了 但我输入41后并没有像公式一样算出是5
错误多多,都是低级错误
注意你的编译器提示
时光IT学院,免费在线直播教学,大牛现场答疑;拒绝纯理论学习,以c/c++/java项目实战为核心.针对c/c++/java零基础学员,快速入门,高薪就业.
这是我输入后 他的提示
x你声明成了一个char型的变量,又用%f来输入 Fahrenhite 变量没有使用正确的做法是变量使用float声明
注意输入输出参数要和变量类型一致
那我把char 改成float?
修改后输入数值 还是远来的情况
Fahrenhite 你也太抬举他了!
char=int 难道我又火星了???
我照着改了 还是不行
终于改好了
错误。。。。。
我觉得用VC++2010,就可以很好的提示你的基本错误
登录百度帐号推荐应用

我要回帖

更多关于 c语言程序设计 的文章

 

随机推荐