错的令人摸不着头脑
#include <Fcntl.h>
#include <dos.h>
#include<string.h>
#define NULL 0
#define LEN sizeof(struct library)
#include<stdio.h>
#include <string.h>
/*设置显示模式——80*25彩色文本*/
void textmode_set(void)
{
_AX=0x03;
__int__(0x10);
}
/*置光标位置*/
void gotoxy(int x,int y)
{
_AH=2;
_BH=0;
_DH=y-1;
_DL=x-1;
__int__(0x10);
}
/*显示光标*/
void cur_show(void)
{
_AH=1;
_CH=6;
_CL=7;
__int__(0x10);
}
struct library
{ char category;
char bookname[30];
char author[20];
char information[100];
struct library*next;
}
main()
{FILE *fp;
struct library libr;
int i,j;
textmode_set();
printf("input category:\n");
gotoxy(16,1);cur_show();scanf("%c",&libr.category);
printf("bookname:\n");
gotoxy(10,2);cur_show();scanf("%s",libr.bookname);
printf("\nauthor:\n");
gotoxy(8,3);cur_show();scanf("%s",libr.author);
printf("information:");
gotoxy(13,4);cur_show();scanf("%s",libr.information);
/*将数据写入文件*/
if((fp=fopen("library.c","w"))==NULL)
{printf("Can not open the file.");
exit(0);
}
for(i=0;i<1;i++)
if(fwrite(&libr,sizeof(struct library),1,fp)!=1)
printf("error!\n");
fclose(fp);
}
8 个解决方案
#1
看一看
#2
WAITING!
#3
printf("input category:\n");
gotoxy(16,1);cur_show();scanf("%c",&libr.category);
printf("bookname:\n");
gotoxy(10,2);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.bookname);
printf("\nauthor:\n");
gotoxy(8,3);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.author);
printf("information:");
gotoxy(13,4);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.information);
gotoxy(16,1);cur_show();scanf("%c",&libr.category);
printf("bookname:\n");
gotoxy(10,2);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.bookname);
printf("\nauthor:\n");
gotoxy(8,3);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.author);
printf("information:");
gotoxy(13,4);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.information);
#4
我缺编程经验,对修改不是很明白,
请问菜鸟唧唧,缓冲区的输入流,输出流怎么了?
stdin是什么?
请问菜鸟唧唧,缓冲区的输入流,输出流怎么了?
stdin是什么?
#5
fflush(stdin);//清空标准输入流.
stdin // standard input stream
stdin // standard input stream
#6
为什么不用fflush(stdin);就不行呢?
#7
因为当执行这一句时:
scanf("%c",&libr.category);
只从输入缓冲中取走一个字符,通常还残留一个回车符'\n'在输入缓冲中;
所以,接下来执行这一句时:
scanf("%s",libr.bookname);
从输入缓冲中取出那个残留的'\n',这一句就算读完了,这样libr.bookname就是一个空字符串.
scanf("%c",&libr.category);
只从输入缓冲中取走一个字符,通常还残留一个回车符'\n'在输入缓冲中;
所以,接下来执行这一句时:
scanf("%s",libr.bookname);
从输入缓冲中取出那个残留的'\n',这一句就算读完了,这样libr.bookname就是一个空字符串.
#8
原来如此
谢谢唧唧
谢谢唧唧
#1
看一看
#2
WAITING!
#3
printf("input category:\n");
gotoxy(16,1);cur_show();scanf("%c",&libr.category);
printf("bookname:\n");
gotoxy(10,2);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.bookname);
printf("\nauthor:\n");
gotoxy(8,3);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.author);
printf("information:");
gotoxy(13,4);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.information);
gotoxy(16,1);cur_show();scanf("%c",&libr.category);
printf("bookname:\n");
gotoxy(10,2);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.bookname);
printf("\nauthor:\n");
gotoxy(8,3);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.author);
printf("information:");
gotoxy(13,4);cur_show();
fflush(stdin); //add this statement!
scanf("%s",libr.information);
#4
我缺编程经验,对修改不是很明白,
请问菜鸟唧唧,缓冲区的输入流,输出流怎么了?
stdin是什么?
请问菜鸟唧唧,缓冲区的输入流,输出流怎么了?
stdin是什么?
#5
fflush(stdin);//清空标准输入流.
stdin // standard input stream
stdin // standard input stream
#6
为什么不用fflush(stdin);就不行呢?
#7
因为当执行这一句时:
scanf("%c",&libr.category);
只从输入缓冲中取走一个字符,通常还残留一个回车符'\n'在输入缓冲中;
所以,接下来执行这一句时:
scanf("%s",libr.bookname);
从输入缓冲中取出那个残留的'\n',这一句就算读完了,这样libr.bookname就是一个空字符串.
scanf("%c",&libr.category);
只从输入缓冲中取走一个字符,通常还残留一个回车符'\n'在输入缓冲中;
所以,接下来执行这一句时:
scanf("%s",libr.bookname);
从输入缓冲中取出那个残留的'\n',这一句就算读完了,这样libr.bookname就是一个空字符串.
#8
原来如此
谢谢唧唧
谢谢唧唧