诗岑
2021-06-23 12:35
采纳率: 98.3%
浏览 28

【C语言】读取txt文件乱码

把编码改成了ANSI还是没用

而且它乱码是这样:

 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 1
                                 0
                                 0
                                 0
                                 0
                        ?r       0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 0
                                 38940
                4?魭     0
                ,?       38044
                        財       0
                ?       38286
                        葧       0
                ?       38474
                        t? 0
                               38618
                        鰱       0
                ?       38720
                        ^? 0
                x?       38826
                        0+官  32767
                柏       -659594368
                     `j  32767
                滥       -659595632
                     朝?    32767
                 ┆?          -643124800
                     €?   32767
                              -643195760
                     |  32767
                Px       -643003184
                     喇  32767
                性       1698955480
                leteCriticalSection     ? 28271
                ?GetCurrentProcess      ?GetCurrentProcessId     30539876
                GetCurrentThreadId      GetLastError   1635013492
                rtupInfoA       mTimeAsFileTime  1667847284
                kCount  CriticalSection  1130722913
                riticalSection  eryPerformanceCounter    67174514
                RtlAddFunctionTable     RtlCaptureContext      1951532041
                lLookupFunctionEntry             77529088
                SetUnhandledExceptionFilter     nFilter  1852403058
                ateProcess      Value    1769238629
                onFilter        rotect   31090
                7       er       1701273439
                tmainargs       v        1852793708
                v_init  type     1920099688
                        exit     1701080941
                        k        67502187
                abort   it       70254702
                fprintf canf     1634231156
                r                1668482192
                anf     mp       28261
                ?strncmp        ?vfprintf        36864
                                 36864
                                 842222661
                .dll    ? 36884
                ?? 36884
                ?? 36884
                ?? 0

附上源代码:

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
//定义结构体数组
typedef struct {
	char num[20];//学号
	char name[20];//学生姓名
	int score;//分数
}STUDENT;
STUDENT stu[20]; 
//home page
void home_page()
{
	printf("\n\t\t******************************** \n");
	printf("\t\t     学生成绩信息管理系统       \n\n");
	printf("\t\t        1、录入信息            \n\n");
	printf("\t\t        2、浏览信息            \n\n");
	printf("\t\t        3、查询信息            \n\n");
	printf("\t\t        4、修改信息            \n\n");
	printf("\t\t        5、成绩排序            \n\n");
	printf("\t\t        6、删除信息            \n\n");
	printf("\t\t        7、向文件写入数据       \n\n");
	printf("\t\t        8、从文件读出数据       \n\n");
	printf("\t\t        9、退出系统            \n\n");
	printf("\t\t******************************** \n\n");
}
// add information
int add_infor(STUDENT stu[], int n)
{
	int i = 0;
	char sign = 'Y';

	while ((sign == 'Y') || (sign == 'y'))
	{
		printf("\n\t\t\t学号:");
		scanf("\t\t%s", stu[n + i].num);
		printf("\t\t\t姓名:");
		scanf("\t\t%s", stu[n + i].name);
		printf("\t\t\t成绩:");
		scanf("\t\t%d", &stu[n + i].score);

		printf("\n\t\t\t是否继续录入?(Y/N):");
		//清空缓存
		scanf("%*[^\n]") ;
		scanf("%*c");
		sign=getchar();

		i++;
	}

	return (n + i);
}
//browse information
void browse_infor(STUDENT stu[], int n)
{
	int i;

	printf("\n\n");
	printf("\t\t学号\t姓名\t成绩\n\n");

	for (i = 0; i < n; i++)
	{
		printf("\t\t%s\t%s\t %d\n",stu[i].num,stu[i].name,stu[i].score);
	}
}
//find information
void find_infor(STUDENT stu[], int n)
{
	int option;
	int i = 0;
	char search_num[10] = { 0 };
	char search_name[20] = { 0 };

	printf("\n\t\t\t1、按学号查找\n");
	printf("\t\t\t2、按姓名查找\n");
	printf("\n\t\t\t请选择相应的选项:");
	scanf("%d",&option);

	system("cls");

	if (1 == option)
	{
		printf("\n\t\t请输入要查找的学号:");
		scanf("%s", search_num);

		while ((strcmp(stu[i].num, search_num) != 0) && i < n)
		{
			i++;
		}
	}

	if (2 == option)
	{
		printf("\n\t\t请输入要查找的姓名:");
		scanf("%s", search_name);

		while ((strcmp(stu[i].name, search_name) != 0) && i < n)
		{
			i++;
		}
	}

	if (i == n)
	{
		printf("\n\t\t未找到!");
		return;
	}

	printf("\n\t\t学号:%s\n", stu[i].num);
	printf("\t\t姓名:%s\n", stu[i].name);
	printf("\t\t成绩:%d\n", stu[i].score);
}
//modify information
int modify_infor(STUDENT stu[], int n)
{
	int option, choice;
	int i = 0;
	char old_temp[20] = { 0 };
	char new_temp[20] = { 0 };
	float new_score;

	printf("\n\t\t\t1、按学号查找修改\n");
	printf("\t\t\t2、按姓名查找修改\n");
	printf("\n\t\t请输入选项选择功能:");
	scanf("%d", &option);

	system("cls");

	if (1 == option)
	{
		printf("\n\t\t请输入学号:");
		scanf("%s", &old_temp);

		while ((strcmp(stu[i].num, old_temp) != 0) && i < n)
		{
			i++;
		}

		if (i == n)
		{
			printf("\n\t\t未找到!");
		}
	}

	if (2 == option)
	{
		printf("\n\t\t请输入姓名:");
		scanf("%s", &old_temp);

		while ((strcmp(stu[i].name, old_temp) != 0) && i < n)
		{
			i++;
		}

		if (i == n)
		{
			printf("\n\t\t未找到!");
		}
	}

	printf("\n\t\t待修改学生的信息:\n");
	printf("\n\t\t学号:%s\n\t\t姓名:%s\n\t\t成绩:%d\n", stu[i].num, stu[i].name, stu[i].score);

	printf("\n\t\t请选择要修改的信息(1.学号 2.姓名 3.成绩):");
	scanf("%d", &choice);

	if (1 == choice)
	{
		printf("\n\t\t输入修改后的学号:");
		scanf("%s", &new_temp);

		strcpy(stu[i].num, new_temp);
		printf("\t\t修改成功!\n");
	}

	if (2 == choice)
	{
		printf("\n\t\t输入修改后的姓名:");
		scanf("%s", &new_temp);

		strcpy(stu[i].name, new_temp);
		printf("\t\t修改成功!\n");
	}

	if (3 == choice)
	{
		printf("\n\t\t输入修改后的成绩:");
		scanf("%f", &new_score);

		stu[i].score = new_score;
		printf("\t\t修改成功!\n");
	}
	return n;
}
//sort the score
void sort_score(STUDENT stu[], int n)
{
	int i, j;
	int * p, * q, s;
	char temp[20];

	for (i = 0; i < n - 1; i++)
	{
		for (j = 0; j < n - 1 - i; j++)
		{
			if (stu[j].score > stu[j + 1].score)
			{
				strcpy(temp, stu[j].num);
				strcpy(stu[j].num, stu[j + 1].num);
				strcpy(stu[j + 1].num, temp);

				strcpy(temp, stu[j].name);
				strcpy(stu[j].name, stu[j+1].name);
				strcpy(stu[j+1].name,temp);

				p = &stu[j].score;
				q = &stu[j + 1].score;
				s = *p;
				*p = *q;
				*q = s;
			}
		}
	}
}
//delete information
int delete_infor(STUDENT stu[], int n)
{
	char search_num[10] = { 0 };
	char search_name[20] = { 0 };
	int i = 0;
	int j;
	int option;
	
	printf("\n\t\t\t1、按学号查找删除\n");
	printf("\t\t\t2、按姓名查找删除\n");
	printf("\n\t\t\t请输入选项选择功能:");
	scanf("%d", &option);
	
	system("cls");

	if (1 == option)
	{
		printf("\n\t\t请输入要删除的学生的学号:");
		scanf("%s", &search_num);

		while ((strcmp(stu[i].num, search_num)) != 0 && (i < n))
		{
			i++;
		}

		if (i == n)
		{
			printf("\t\t未找到!\n");
			return n;
		}
	}
	
	if (2 == option)
	{
		printf("\n\t\t请输入要删除的学生的姓名:");
		scanf("%s", &search_name);

		while ((strcmp(stu[i].num, search_name) != 0) && (i < n))
		{
			i++;
		}

		if (i == n)
		{
			printf("\t\t未找到!\n");
			return n;
		}
	}

	for (j = i; j < n; j++)
	{
		strcpy(stu[j].num, stu[j + 1].num);
		strcpy(stu[j].name, stu[j + 1].name);
		stu[j].score = stu[j + 1].score;
	}

	printf("\t\t删除成功!\n");
	return (n - 1);
}
//write to file
void writetofile(STUDENT stu[], int n)
{
	FILE* fp;
	int i = 0;
	char filename[20] = { 0 };
	
	printf("\n\t\t\t请输入文件名称:");
	scanf("%s", filename);

	if ((fp = fopen(filename, "w")) == NULL)
	{
		printf("\n\t\t\t打开文件失败!\n");
		return;
	}

	fprintf(fp, "%d\n", n);

	while (i < n)
	{
		fprintf(fp, "%-6s%-10s%d\n", stu[i].num, stu[i].name, stu[i].score);
		i++;
	}

	fclose(fp);

	printf("\n\t\t\t写入文件成功!\n");
}
//read from file
int readfromfile(STUDENT stu[], int n)
{
	FILE* fp;
	int i, num;
	char filename[20];

	i = 0;

	printf("\n\t\t\t请输入文件名称:");
	scanf("%s", filename);

	if ((fp = fopen(filename, "r")) == NULL)
	{
		printf("\n\t\t\t无法打开文件!\n");

		return n;
	}

	fscanf(fp, "%d", &num);

	while (i < num)
	{
		fscanf(fp, "%-5s%-10s%d\n", stu[n + i].num, stu[n + i].name, stu[n + i].score);
		i++;
	}

	n += num;
	fclose(fp);

	printf("\n\t\t\t读出数据成功!\n");

	return n;
}
int main()
{
	int select = 0;
	int n = 0;

	while (1)
	{
		system("cls");
		
		home_page();

		printf("\t\t输入标号选择功能: ");
		scanf("%d", &select);

		switch (select)
		{
			case 1:
				system("cls");
				printf("\n\t\t********录入学生成绩信息********\n");
				n = add_infor(stu, n);
				break;
			case 2:
				system("cls");
				printf("\n\t\t********浏览学生成绩信息********\n");
				browse_infor(stu, n);
				getchar();
				break;			
			case 3:
				system("cls");
				printf("\n\t\t**********查询学生信息**********\n");
				find_infor(stu, n);
				getchar();
				break;
			case 4:
				system("cls");
				printf("\n\t\t*********修改学生信息*********\n");
				n = modify_infor(stu, n);
				getchar();
				break;
			case 5:
				system("cls");
				printf("\n\t\t*********按成绩排序*********\n");
				sort_score(stu, n);
				printf("\n\t\t\t排序成功!");
				getchar();
				break;	
			case 6:
				system("cls");
				printf("\n\t\t*********删除学生信息*********\n");
				n = delete_infor(stu, n);
				getchar();
				break;
			case 7:
				system("cls");
				printf("\n\t\t********向文件写入数据********\n");
				writetofile(stu, n);
				getchar();
				break;
			case 8:
				system("cls");
				printf("\n\t\t********从文件读出数据********\n");
				n = readfromfile(stu, n);
				getchar();
				break;
			default:
				system("cls");
				printf("\n\n\t\t**********谢谢使用!**********\n");
				exit(0);
				break;
		}

		getchar();
		 
	}
}

  • 收藏

2条回答 默认 最新

  • qfl_sdu 2021-06-23 14:58
    已采纳

    336行,读文件的地方fscanf(fp, "%-5s%-10s%d\n", stu[n + i].num, stu[n + i].name, stu[n + i].score);

    stu[n + i].score前面需要&,改成:

    fscanf(fp, "%-5s%-10s%d\n", stu[n + i].num, stu[n + i].name, &stu[n + i].score);

    打赏 评论
  • qzjhjxj 2021-06-23 16:28

    代码没什么大问题,修改建议如下,供参考:

    //函数 int readfromfile(STUDENT stu[], int n)
    
    //第336行: fscanf(fp, "%-5s%-10s%d\n", stu[n + i].num, stu[n + i].name, stu[n + i].score);
    //改为:   
              fscanf(fp,"%s %s %d\n", stu[n + i].num, stu[n + i].name, &stu[n + i].score);
    打赏 评论

相关推荐 更多相似问题