user-0 提问: 大神帮看看下面的代码！！从文件中读取数据并建立链表然后输出到屏幕上，结果没有输出原数据

回复:
user: user-0
content: 我知道了!应该在从文件中读数据的这段代码 fscanf_s(fp,"%f %f %f %f %f", &p_o->eng, &p_o->math, &p_o->computer, &p_o->ave, &p_o->sum);  的格式控制串最后加一个\n就行了啦！ 还是谢谢大神的耐心解答啦！

user: user-1
content: 引用 11 楼 baidu_27937801 的回复:[quote=引用 10 楼 zhangxiangDavaid 的回复:] 9.00后回车 "%ld %s %.1f %.1f %.1f %.2f %.2f\n"  这是我写入文件时的数据格式，是要用\r字符吗[/quote] 贴出所以源码和stu.txt，我直接给你改好

user: user-0
content: 引用 10 楼 zhangxiangDavaid 的回复:9.00后回车 "%ld %s %.1f %.1f %.1f %.2f %.2f\n"  这是我写入文件时的数据格式，是要用\r字符吗

user: user-1
content: 9.00后回车

user: user-0
content: 这个是我保存数据到文件的代码 void save_file(STUDENT *head) { 	FILE *fp_sa; 	STUDENT *p; 	char ch1 = '\n',ch2='\r'; 	system("cls"); 	if (fopen_s(&fp_sa,"stu.txt", "w") != 0)//文件读指针fp_s指向文件信息区,打开方式为写 	{ 		printf("文件打开失败！\n");//文件打开失败提示语 		exit(0); 	} 	p = head->next; 	while (p != NULL)//输出数据到文件 	{ 		fprintf(fp_sa, FORMAT_DATA, p->ID, p->name, p->eng, p->math, p->computer, p->ave, p->sum); 		p = p->next; 	} 	fputc(ch2,fp_sa); 	fputc(ch1, fp_sa); 	fclose(fp_sa); 	printf("\n******************************保存完毕****************************\n"); }

user: user-0
content: 引用 7 楼 zhangxiangDavaid 的回复:采用换行结束 这个具体应该怎么用呢？我刚才试了下没成功

user: user-1
content: 采用换行结束

user: user-1
content: 引用 5 楼 baidu_27937801 的回复:[quote=引用 4 楼 zhangxiangDavaid 的回复:] 一次性读出，试试：  fscanf(fp, "%ld%s%lf%lf%lf%lf%lf\n", &p_s->ID, p_s->name, &p_s->eng, &p_s->math, &p_s->computer, &p_s->ave, &p_s->sum);  不好意思哈点错啦 我又改了下输出前面的数据是没问题了，但最后还是会输出一堆乱码。代码如下 ************************************************************************************** void function_output() { 	STUDENT *head_s, *p_o, *last_s, *p; 	FILE *fp; 	system("cls"); 	if (fopen_s(&fp, "stu.txt", "r") != 0) 	{ 		printf("文件打开失败！\n");//打开失败提示语 		exit(0); 	} 	head_s = (STUDENT*)malloc(L); 	last_s = head_s; 	while (!feof(fp))//读取文件并创建链表 	{ 		p_o = (STUDENT *)malloc(L); 		fscanf_s(fp,"%ld", &p_o->ID); 		fscanf_s(fp, "%*c"); 		fgets(p_o->name,sizeof(p_o->name),fp); 		fscanf_s(fp,"%f %f %f %f %f", &p_o->eng, &p_o->math, &p_o->computer, &p_o->ave, &p_o->sum); 		last_s->next = p_o; 		last_s = last_s->next; 	} 	last_s->next = NULL; 	fclose(fp); 	p = head_s->next;//指向头结点 	printf("\n所有学生信息如下：\n"); 	printf(FORMAT_HEAD, "学号", "姓名", "英语", "数学", "计算机", "平均成绩", "总成绩"); 	while (p != NULL) 	{ 		printf(FORMAT_DATA, p->ID, p->name, p->eng, p->math, p->computer, p->ave, p->sum);//输出当前结点包含的信息 		p = p->next;//指向下一个结点	 	} 	printf("\t\t   ┌─────┐\n"); 	printf("\t\t   │输出完成！│\n"); 	printf("\t\t   └─────┘\n"); 	system("pause"); } ************************************************************************************************************************************ 截图如下 [/quote] 把stu.txt中最后一行数据一换行结束

user: user-0
content: 引用 4 楼 zhangxiangDavaid 的回复:一次性读出，试试：  fscanf(fp, "%ld%s%lf%lf%lf%lf%lf\n", &p_s->ID, p_s->name, &p_s->eng, &p_s->math, &p_s->computer, &p_s->ave, &p_s->sum);  不好意思哈点错啦 我又改了下输出前面的数据是没问题了，但最后还是会输出一堆乱码。代码如下 ************************************************************************************** void function_output() { 	STUDENT *head_s, *p_o, *last_s, *p; 	FILE *fp; 	system("cls"); 	if (fopen_s(&fp, "stu.txt", "r") != 0) 	{ 		printf("文件打开失败！\n");//打开失败提示语 		exit(0); 	} 	head_s = (STUDENT*)malloc(L); 	last_s = head_s; 	while (!feof(fp))//读取文件并创建链表 	{ 		p_o = (STUDENT *)malloc(L); 		fscanf_s(fp,"%ld", &p_o->ID); 		fscanf_s(fp, "%*c"); 		fgets(p_o->name,sizeof(p_o->name),fp); 		fscanf_s(fp,"%f %f %f %f %f", &p_o->eng, &p_o->math, &p_o->computer, &p_o->ave, &p_o->sum); 		last_s->next = p_o; 		last_s = last_s->next; 	} 	last_s->next = NULL; 	fclose(fp); 	p = head_s->next;//指向头结点 	printf("\n所有学生信息如下：\n"); 	printf(FORMAT_HEAD, "学号", "姓名", "英语", "数学", "计算机", "平均成绩", "总成绩"); 	while (p != NULL) 	{ 		printf(FORMAT_DATA, p->ID, p->name, p->eng, p->math, p->computer, p->ave, p->sum);//输出当前结点包含的信息 		p = p->next;//指向下一个结点	 	} 	printf("\t\t   ┌─────┐\n"); 	printf("\t\t   │输出完成！│\n"); 	printf("\t\t   └─────┘\n"); 	system("pause"); } ************************************************************************************************************************************ 截图如下

user: user-1
content: 一次性读出，试试：  fscanf(fp, "%ld%s%lf%lf%lf%lf%lf\n", &p_s->ID, p_s->name, &p_s->eng, &p_s->math, &p_s->computer, &p_s->ave, &p_s->sum);

user: user-0
content: 引用 2 楼 zhangxiangDavaid 的回复:试试：  fscanf_s(fp, "%ld", &p_s->ID); //fscanf_s(fp, "%*c"); //fgets(p_s, sizeof(p_s->name), fp); fscanf(fp, "%s", p_s->name);   //如果姓名用字符串存储 fscanf_s(fp, "%lf%lf%lf%lf%lf", &p_s->eng, &p_s->math, &p_s->computer, &p_s->ave, &p_s->sum);  姓名是用字符串儿储存的，但这样不行 报fscanf出错 ********************************************************************************** 错误	1	error C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	d:\实验三\1.c	148	1	实验三 ****************************************************************************************** 改成fscanf_s后会在fscanf(fp, "%s", p_s->name);  报访问冲突的错

user: user-1
content: 试试：  fscanf_s(fp, "%ld", &p_s->ID); //fscanf_s(fp, "%*c"); //fgets(p_s, sizeof(p_s->name), fp); fscanf(fp, "%s", p_s->name);   //如果姓名用字符串存储 fscanf_s(fp, "%lf%lf%lf%lf%lf", &p_s->eng, &p_s->math, &p_s->computer, &p_s->ave, &p_s->sum);

user: user-1
content: 姓名使用什么类型存储的？