user-0 提问: C程序，模拟酒店住房管理系统，TXT文件输出住房信息时遇到问题，华电大一狗求助

回复:
user: user-1
content: 请检查scanf和fscanf的返回值。  推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。  不要把 fopen("...","...");fscanf,fprintf,fclose //读时把\r\n替换成\n，写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待 和 fopen("...","...b");fread,fwrite,fclose  //不作以上替换，遇到\x1a仍继续读;读写的内容当字节看待 弄混了  代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试（F11，Shift+F11，F10）和设断点调试（F9、F5）是程序员必须掌握的技能之一。

user: user-0
content: 引用 8 楼 zhao4zhong1 的回复:[quote=引用 7 楼 u012494298 的回复:] [quote=引用 6 楼 zhao4zhong1 的回复:] fflush(fu);  将fprintf改成fflush后，CFree报错。  [Error] E:\C\酒店管理系统\模拟酒店住房管理系统.c:559: error: too many arguments to function `fflush'[/quote] 我的意思不是将fprintf改为fflush；而是在每个fprintf语句后加一句fflush(fu); [/quote]  赵老师，我已经再加了fflush(fu);user.txt还是空白，没用 void rewrite() { 	FILE *fa, *fb, *fc, *fu; 	int i, j; 	fa=fopen("single.txt", "w"); 	if(fa==NULL){ 		printf("文件打开错误！\n"); 		exit(1); 	} 	for(i=0; i<aroom; i++) 		fprintf(fa, "%5d", a[i]);                 fflush(fa); 	fclose(fa); 	fb=fopen("double.txt", "w"); 	if(fb==NULL){ 		printf("文件打开错误！\n"); 		exit(1); 	} 	for(i=0; i<broom; i++){ 		for(j=0; j<bbed; j++) 			fprintf(fb, "%5d", b[i][j]);                         fflush(fb); 		fprintf(fb,"%c", '\n');	                 fflush(fb); 	} 		 	fclose(fb); 	fc=fopen("triple.txt", "w"); 	if(fb==NULL){ 		printf("文件打开错误！\n"); 		exit(1); 	} 	for(i=0; i<croom; i++){ 		for(j=0; j<cbed; j++) 			fprintf(fc, "%5d", c[i][j]);                         fflush(fc); 		fprintf(fc,"%c", '\n');	                 fflush(fc); 	} 		 	fclose(fc); 	fu=fopen("users.txt", "w"); 		if(fu=NULL){ 			printf("文件打开错误!"); 			exit(1); 		}     for(i=0; i<people; i++){ 		fprintf(fu, "%s\t%d\t%d\t%s\t%d", user[i].name, user[i].age, user[i].sex, user[i].date, user[i].rank);                 fflush(fu); 		fprintf(fu,"%c", '\n');                 fflush(fu); 	}  fclose(fu);	 }

user: user-1
content: fflush Flushes a stream.  int fflush( FILE *stream );  Function Required Header Compatibility  fflush <stdio.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.  Note   If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions.  Parameter  stream  Pointer to FILE structure  Remarks  The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.  Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program’s object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ.  For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen.  Example  /* FFLUSH.C */  #include <stdio.h> #include <conio.h>  void main( void ) {    int integer;    char string[81];     /* Read each word as a string. */    printf( "Enter a sentence of four words with scanf: " );    for( integer = 0; integer < 4; integer++ )    {       scanf( "%s", string );       printf( "%s\n", string );    }     /* You must flush the input buffer before using gets. */    fflush( stdin );    printf( "Enter the same sentence with gets: " );    gets( string );    printf( "%s\n", string ); }   Output  Enter a sentence of four words with scanf: This is a test This is a test Enter the same sentence with gets: This is a test This is a test   Stream I/O Routines  See Also   fclose, _flushall, setvbuf

user: user-1
content: 引用 7 楼 u012494298 的回复:[quote=引用 6 楼 zhao4zhong1 的回复:] fflush(fu);  将fprintf改成fflush后，CFree报错。  [Error] E:\C\酒店管理系统\模拟酒店住房管理系统.c:559: error: too many arguments to function `fflush'[/quote] 我的意思不是将fprintf改为fflush；而是在每个fprintf语句后加一句fflush(fu);

user: user-0
content: 引用 6 楼 zhao4zhong1 的回复:fflush(fu);  将fprintf改成fflush后，CFree报错。  [Error] E:\C\酒店管理系统\模拟酒店住房管理系统.c:559: error: too many arguments to function `fflush'

user: user-1
content: fflush(fu);

user: user-0
content: 引用 4 楼 focuslight 的回复:for(i=0; i<people; i++){         fprintf(fu, "%s\t%d\t%d\t%s\t%d", user[i].name, user[i].age, user[i].sex, user[i].date, user[i].rank);         fprintf(fu,"%c", '\n');     }    这里断点看看  断了，user.txt还是一样空白，所以这段代码相当于没作用？

user: user-2
content: for(i=0; i<people; i++){         fprintf(fu, "%s\t%d\t%d\t%s\t%d", user[i].name, user[i].age, user[i].sex, user[i].date, user[i].rank);         fprintf(fu,"%c", '\n');     }    这里断点看看

user: user-0
content: void rewrite() { 	FILE *fa, *fb, *fc, *fu; 	int i, j; 	fa=fopen("single.txt", "w"); 	if(fa==NULL){ 		printf("文件打开错误！\n"); 		exit(1); 	} 	for(i=0; i<aroom; i++) 		fprintf(fa, "%5d", a[i]); 	fclose(fa); 	fb=fopen("double.txt", "w"); 	if(fb==NULL){ 		printf("文件打开错误！\n"); 		exit(1); 	} 	for(i=0; i<broom; i++){ 		for(j=0; j<bbed; j++) 			fprintf(fb, "%5d", b[i][j]); 		fprintf(fb,"%c", '\n');	 	} 		 	fclose(fb); 	fc=fopen("triple.txt", "w"); 	if(fb==NULL){ 		printf("文件打开错误！\n"); 		exit(1); 	} 	for(i=0; i<croom; i++){ 		for(j=0; j<cbed; j++) 			fprintf(fc, "%5d", c[i][j]); 		fprintf(fc,"%c", '\n');	 	} 		 	fclose(fc); 	fu=fopen("users.txt", "w"); 		if(fu=NULL){ 			printf("文件打开错误!"); 			exit(1); 		}     for(i=0; i<people; i++){ 		fprintf(fu, "%s\t%d\t%d\t%s\t%d", user[i].name, user[i].age, user[i].sex, user[i].date, user[i].rank); 		fprintf(fu,"%c", '\n'); 	}  fclose(fu);	 }

user: user-0
content: 源代码也贴上来吧 #include <stdio.h> #include <stdlib.h> #include<string.h> #include<process.h> #include <dos.h> #define aroom 6 #define broom 3 #define bbed 2 #define croom 2 #define cbed 3  #define people 18  /*people=aroom+(broom*bbed)+(croom*cbed); */ int a[aroom]={0};  int b[broom][bbed]={0}; int c[croom][cbed]={0};   struct  { 	char name[20]; 	int age; 	int sex; 	char date[15]; 	int rank; }user[people]; int main() { 	int a; 	void prepare(); 	void Register(); 	void Checkout(); 	void rewrite(); 	printf("\n      =================================================================\n"); 	printf("                           欢迎来到海天大酒店!\n"); 	printf("\t(本酒店单人间6个，双人间3个，三人间2个，总共可住18人)\n"); 	prepare(); 	while(1){ 		printf(" \n请选择你所需要的服务(例如：1)：\n"); 		printf(" 1、旅客登记\n 2、退房\n 3、退出\n"); 		scanf("%d", &a); 		getchar(); 		switch(a){ 			case 1:  				Register(); break; 			case 2: 				Checkout(); break; 			case 3: 				rewrite(); 				printf("             	        欢迎下次光临!"); 				printf("\n     ==================================================================\n"); 				system("pause"); 				exit(1);  			default:  				printf("选择服务错误，请重新选择！\n"); 	 		}	 	} 	return 0; }  void prepare() { 	FILE *fa,*fb,*fc,*fu; 	int i, j; 	fa=fopen("single.txt","r"); 	if(fa==NULL) 	{ 		printf("\n清场!"); 		fa=fopen("single.txt","w"); 		for(i=0;i<aroom;i++) 		{ 			fprintf(fa,"%5d",0); 		} 		fclose(fa);	 	} 	int aa; 	i=0; 	while(fscanf(fa,"%d",&aa)!=EOF) 	{ 		a[i]=aa; 		i++; 	} 	fclose(fa); 	fb=fopen("double.txt","r"); 	if(fb==NULL) 	{ 		printf("\n清场!"); 		fb=fopen("double.txt","w"); 		for(i=0;i<broom;i++) 		{ 			for(j=0;j<bbed;j++) 			{ 				fprintf(fb,"%5d",0);	 			} 				fprintf(fb,"%c", '\n');	 		} 		fclose(fb);	 	} 	int bb; 	j=0, i=0; 	while(fscanf(fb,"%d",&bb)!=EOF){ 		if(j==bbed) 		{ 			i++; 			j=0;		 		} 		b[i][j]=bb; 		j++; 	} 	fclose(fb); 	fc=fopen("triple.txt","r"); 	if(fc==NULL) 	{ 		printf("\n清场!"); 		fc=fopen("triple.txt","w"); 		for(i=0;i<croom;i++) 		{ 			for(j=0;j<cbed;j++) 			{ 				fprintf(fc,"%5d",0);	 			} 				fprintf(fc,"%c", '\n');		 		} 		fclose(fc);	 	}     int cc;     i=0, j=0; 	while(fscanf(fc,"%d",&cc)!=EOF){ 		if(j==cbed) 		{ 			i++; 			j=0;		 		} 		c[i][j]=cc; 		j++; 	} 	fclose(fc); 	fu=fopen("users.txt","r"); 	if(fu==NULL) 	{ 		printf("\n清场!"); 		fu=fopen("users.txt","w"); 		for(i=0;i<people;i++) 		{ 				fprintf(fu,"%s\t%d\t%d\t%s\t%d","0",0,0,"0",0); 				fprintf(fu,"%c", '\n');	 		} 		fclose(fu);	 	} 	int age,sex,rank; 	char name[20],data[15]; 	i=0; 	while(fscanf(fu,"%s %d %d %s %d",name,&age,&sex,data,&rank)!=EOF){ 		strcpy(user[i].name,name); 		user[i].age=age; 		user[i].sex=sex; 		strcpy(user[i].date,data); 		user[i].rank=rank; 		i++; 	} 	fclose(fu); 	 }  void Register() { 	char name[20],  date[15]; 	int age, rank, i, j, sex; 	printf("register\n"); 	printf("请输入您的姓名:\n"); 	scanf("%s",name); 	getchar(); 	while(1){ 		printf("请输入您的年龄:\n"); 		scanf("%d", &age); 		if(age>0 && age<200) 			break; 		else 			printf("输入年龄格式有错，请重新输入！\n");	 			getchar(); 	} 	while(1) 	{ 		printf("请输入您的性别(1代表男士， 2代表女士):\n"); 		scanf("%d", &sex); 		if(sex<1|| sex>2) 		{ 			printf("输入性别格式错误，请重新输入!\n"); 			getchar(); 			continue; 		} 		else 			break; 			 	} 	 	printf("请输入您的到达日期:\n"); 	scanf("%s", date); 	while(1) 	{ 		int flag1=0, flag2=0; 		while(1) 		{ 			printf("请输入您所需要的房间等级\n输入1（为单人标间，代号A）\n输入2（为双人标间，代号B）\n输入3（为三人标间，代号C）\n"); 			scanf("%d", &rank);  			if(rank<1 || rank>3) 			{ 				printf("输入房间等级错误，请重新输入！\n"); 				getchar(); 				continue; 			}  			else 				break; 			 		} 		 		switch(rank){ 			case 1:  				for(i=0; i<aroom; i++)  				{ 					if(a[i]==0) 					{	 						a[i]=sex;  						strcpy(user[i].name,name); 						user[i].age=age; 						user[i].sex=sex; 						strcpy(user[i].date, date); 						user[i].rank=rank; 						printf("恭喜您订房成功！\n"); 						printf("您的订单信息如下:\n"); 						printf("姓名\t年龄\t性别(1男2女)\t入住日期\t房间号码\n"); 						printf("%-s\t%-d\t%-d\t\t%-s\t\tA%d", user[i].name, user[i].age, user[i].sex=sex, user[i].date, i+1); 						flag2=1; 						break; 					} 				} 				if(i==aroom) 				{ 					printf("单人标间已满！\n"); 					flag1=1;	 				} 				break; 			case 2:  				 				for(i=0; i<broom; i++)  				{ 					for(j=0; j<bbed; j++) 					{ 					 						if(b[i][j]==0) 							if(j==0) 							{	 								int order=aroom+i*bbed+j; 								b[i][j]=sex;  								strcpy(user[order].name,name); 								user[order].age=age; 								user[order].sex=sex; 								strcpy(user[order].date, date); 								user[order].rank=rank; 								printf("恭喜您订房成功！\n"); 								printf("您的订单信息如下:\n"); 								printf("姓名\t年龄\t性别(1男2女)\t入住日期\t房间号码\n"); 								printf("%-s\t%-d\t%-d\t\t%-s\t\tB%d床位%d", user[order].name, user[order].age, user[order].sex=sex, user[order].date, i+1, j+1); 								flag2=1; 								break; 								 							} 							else 								if(sex==b[i][j-1]) 								{ 									int order=aroom+i*bbed+j; 									b[i][j]=sex; 									strcpy(user[order].name,name); 									user[order].age=age; 									user[order].sex=sex; 									strcpy(user[order].date, date); 									user[order].rank=rank; 									printf("恭喜您订房成功！\n"); 									printf("您的订单信息如下:\n"); 									printf("姓名\t年龄\t性别(1男2女)\t入住日期\t房间号码\n"); 									printf("%-s\t%-d\t%-d\t\t%-s\t\tB%d床位%d", user[order].name, user[order].age, user[order].sex=sex, user[order].date, i+1, j+1); 									flag2=1; 									break; 								} 								else 									break; 					} 					if(flag2==1) 						break; 				} 				if(i==broom) 				{ 					printf("双人标间已满！\n"); 					flag1=1;	 				} 				break; 	 			case 3:  				 				for(i=0; i<croom; i++)  				{ 					for(j=0; j<cbed; j++) 					{ 						if(c[i][j]==0) 							if(j==0) 							{ 								int order=aroom+broom*bbed+i*cbed+j;	 								c[i][j]=sex;  								strcpy(user[order].name,name); 								user[order].age=age; 								user[order].sex=sex; 								strcpy(user[order].date, date); 								user[order].rank=rank; 								printf("恭喜您订房成功！\n"); 								printf("您的订单信息如下:\n"); 								printf("姓名\t年龄\t性别(1男2女)\t入住日期\t房间号码\n"); 								printf("%-s\t%-d\t%-d\t\t%-s\t\tC%d床位%d", user[order].name, user[order].age, user[order].sex=sex, user[order].date, i+1, j+1); 								flag2=1; 								break; 							} 							else 								if(sex==c[i][j-1]) 								{ 									int order=aroom+broom*bbed+i*cbed+j; 									c[i][j]=sex; 									strcpy(user[order].name,name); 									user[order].age=age; 									user[order].sex=sex; 									strcpy(user[order].date, date); 									user[order].rank=rank; 									printf("恭喜您订房成功！\n"); 									printf("您的订单信息如下:\n"); 									printf("姓名\t年龄\t性别(1男2女)\t入住日期\t房间号码\n"); 									printf("%-s\t%-d\t%-d\t\t%-s\t\tC%d床位%d", user[order].name, user[order].age, user[order].sex=sex, user[order].date, i+1, j+1); 									flag2=1; 									break; 								} 								else 									break; 					} 					if(flag2==1) 						break; 				} 				if(i==croom) 				{ 					printf("三人标间已满！\n"); 					flag1=1;	 				} 				break; 	 			default:  				printf("没有此类等级，请重新选择！\n");  		} 		int choice, flag3=0;  		if(flag1==1) 		{ 			while(1) 			{ 				printf("您选择的此类等级房间已满！退出请输入0，重新选择请按1\n");  				scanf("%d", &choice); 				switch(choice) 				{ 					case 0:  						rewrite(); 						printf("             	             欢迎下次光临!"); 						printf("\n     ==================================================================\n"); 						exit(1); 					case 1: 						flag3=1; 						break; 					default: 						printf("您输入的格式有错，请重新选择！\n");	  				} 				if(flag3==1) 					break;  			} 		} 		if(flag2==1) 		{ 			break;	 		} 		for(i=0;i<people;i++) 		{ 			if(user[i].age==0) 				break; 			else  				continue;	 		} 		if(i==people) 		{ 			printf("                  全店客满，欢迎下次再来！\n"); 			printf("     ==================================================================\n"); 			system("pause"); 			rewrite(); 			exit(1);  		}		  	}  } void Checkout() {  	int rank,  bed, room, flag=0; 		while(1)  	{ 		printf("checkout\n"); 		while(1) 		{ 			printf("请输入房间等级:\n"); 			scanf("%d",&rank);   			if(rank<1 || rank>3) 			{ 				printf("输入房间等级错误，请重新输入！\n"); 				getchar(); 				continue; 			}  			else 				break; 		} 		printf("请输入房间号（输入数字即可，例如A1，只需输入1，B2只需输入2）:\n"); 		scanf("%d",&room);  		getchar();  		printf("请输入床位号(单人标间请输入1):\n"); 		scanf("%d",&bed);  		switch(rank) 		{ 			case 1:  				if(room<0 || room>aroom) 				{  					printf("输入房间号错误，请重新输入！\n"); 					break;  				}  					if(bed==1)  					{ 						if(a[room-1]==0)				 						{  							printf("输入房间未住人，请重新输入相关信息！\n"); 							break; 						} 						else 						{ 							a[room-1]=0;  							strcpy(user[room-1].name,"0"); 							user[room-1].age=0; 							user[room-1].sex=0; 							strcpy(user[room-1].date,"0"); 							user[room-1].rank=0; 							flag=1; 							printf("退房成功!\n"); 						}	 					} 					else 						printf("输入床位号码有错，请重新输入！\n"); 					break; 			case 2: 				if(bed<0 || bed>broom) 				{  					printf("输入房间号错误，请重新输入！\n"); 					break;  				}  					if(room>=0 && room<=bbed) 					{ 						if(b[room-1][bed-1]==0) 							printf("输入房间未住人，请重新输入相关信息！\n"); 						else 						{ 							int order=aroom+(room-1)*bbed+bed-1; 							b[room-1][bed-1]=0;  							strcpy(user[order].name,"0"); 							user[order].age=0; 							user[order].sex=0; 							strcpy(user[room].date,"0"); 							user[order].rank=0; 							flag=1; 							printf("退房成功!\n"); 						} 			 					} 					else 						printf("输入床位号码有错，请重新输入！\n"); 					break; 				 			case 3: 				if(room<0 || room>croom) 				{  					printf("输入房间号错误，请重新输入！\n"); 					break;  				}  					if(bed>=0 && bed<=cbed) 					{ 						if(c[room-1][bed-1]==0) 							printf("输入房间未住人，请重新输入相关信息！\n"); 						else 						{ 							int order=aroom+broom*bbed+(room-1)*cbed+bed-1; 							c[room-1][bed-1]=0;  							strcpy(user[order].name,"0"); 							user[order].age=0; 							user[order].sex=0; 							strcpy(user[room].date,"0"); 							user[order].rank=0; 							flag=1; 							printf("退房成功!\n"); 						} 			 					} 					else 						printf("输入床位号码有错，请重新输入！\n"); 					break; 				 				default:  					printf("房间类型错误，请重新输入信息！\n");	 			 		} 		if(flag==1) 			break; 	} 	 }

user: user-0