怎么样实现从一个文件中读入信息赋给变量,出现了奇怪的错误?
  • 板块学术版
  • 楼主yxsl
  • 当前回复15
  • 已保存回复15
  • 发布时间2023/3/23 20:18
  • 上次更新2023/10/23 20:45:42
查看原帖
怎么样实现从一个文件中读入信息赋给变量,出现了奇怪的错误?
802842
yxsl楼主2023/3/23 20:18
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ERROR -1
#define OK 1

typedef struct STUDENT
{
	char name[30];
	int id;
	int other_data;
	struct STUDENT *next;
}student;
FILE *f;
char filename[30];
student *head;
int count=0;//记录学生人数 

void init()
{
	printf("----Enter filename:\n");
	scanf("%s",filename);//为新建的目的文件取名
 
	head=(student *)malloc(sizeof(student));
	head->next=NULL;
}

void write()//新建一个文件,并将学生信息写入文件 
{
	student *p;p=head;
	if( (f=fopen(filename,"w+"))==NULL)
	{
		exit(ERROR);
	}
	
	fprintf(f,"            姓名               学号       其他数据\n"); 
	for(int i=0;i<count;i++)
	{
		p=p->next;
		fprintf(f,"%16s %16d %16d\n",p->name,p->id,p->other_data);		
	}
	fprintf(f,"共计%d个学生\n",count);
	
	fclose(f); 
}

void get()//a. 获取学生信息 
{
	char way[30];
	void getfromfile();void getfromkeyboard();
	printf("----Get information from file or keyboard?\n");
	scanf("%s",way);
	if(strcmp(way,"file")==0)
	{
		getfromfile();
	}
	else 
	{
		if(strcmp(way,"keyboard")==0)
		{
			getfromkeyboard();
		}
	    else 
		{
			printf("----Wrong way!\n");
			exit(ERROR);
		}
	}
	printf("----Any other operation?\n"); 
}

void getfromfile()
{
	FILE *origin;
	student *p,*q;q=head;
	char originfile[30];
	printf("----Enter name of the origin file:\n");
	scanf("%s",originfile);
	if( (origin=fopen(originfile,"r"))!=NULL)
	{
		while( !feof(origin) )
		{
			p=(student*)malloc(sizeof(student));
			while(q->next!=NULL)
			{
				q=q->next;
			}
			fscanf(origin,"%s %d %d",&p->name,&p->id,&p->other_data) ;
			p->next=q->next;
			q->next=p;
		}
	}
	else
	{
		printf("----Can not find the file!\n");
		exit(ERROR);
	}
}
/*void getfromfile()
{
	FILE *origin;
	char originfile[30];
	printf("Enter name of the origin file:\n");
	scanf("%s",originfile);
	if( (origin=fopen(originfile,"r"))!=NULL)
	{
		fscanf(originfile,"")
	}
}*/

void getfromkeyboard()
{
	student *p,*q;	
	int i,n;
	printf("----Enter number of student:\n");
	scanf("%d",&n);
	printf("----Enter data:\n");
	
	for(i=0;i<n;i++)
	{
		p=(student*)malloc(sizeof(student));
		q=head;
		while(q->next!=NULL)q=q->next;
		p->next=q->next;
		q->next=p;
		count++;
		scanf("%s %d %d",&p->name,&p->id,&p->other_data);
	}
}

void seek()//b.	查询学生基本信息,输入学生学号或姓名等信息后,显示学生的所有信息。
{
	student *p;		
	p=head->next;
	char a[30];
	int b=0;
	printf("----Enter related information:\n");
	scanf("%s",a);
	if('0'<=a[0]&&a[0]<='9')
	{
		b=atoi(a);
	}
	if(b==0)
	{
		while(strcmp(p->name,a)!=0)
		{
			if(p->next==NULL)
			{
				printf("----Do not exist!\n");
				exit(ERROR);
			}
			p=p->next;
		}
	}
	else
	{
		while(p->id!=b)
		{
			if(p->next==NULL)
			{
				printf("----Do not exist!\n");
				exit(ERROR);
			}
			p=p->next;
		}
	}
	printf("----Target data:\n %s %d %d\n",p->name,p->id,p->other_data); 
	printf("----Any other operation?\n"); 
}

void alter()//c.修改第n个学生的信息 
{
	int i,n;
	char item[30];
	student *p;p=head;
	printf("----Which one to alter?\n");
	scanf("%d",&n);
	if(n>count)exit(ERROR);
	for(i=n;i>0;i--)
	{
		p=p->next;
	}
	
	printf("----Which item to alter?\n");
	scanf("%s",item);
	
	printf("----Enter correct data:\n");
	if(strcmp(item,"name")==0)scanf("%s",&p->name);
	if(strcmp(item,"id")==0)scanf("%d",&p->id);
	if(strcmp(item,"other_data")==0)scanf("%d",&p->other_data);
	
	printf("----Any other operation?\n"); 

}

void add()//d.添加在第n个学生前面 
{
	int i,n;
	student *p,*q;q=head;
	p=(student*)malloc(sizeof(student));
	printf("----Where to add?\n");
	scanf("%d",&n);
	if(n>count)exit(ERROR);
	
	for(i=0;i<n-1;i++)
	{
		q=q->next;
	}//q定位在第n个学生前面
	printf("----Enter data:\n");
	scanf("%s %d %d",&p->name,&p->id,&p->other_data);
	p->next=q->next;
	q->next=p; 
	count++;
	
	printf("----Any other operation?\n"); 
}

void deletee()//e.删除第n个学生信息 
{
	int i,n;
	student *p,*q;q=head;
	printf("----Which one to delete?\n");
	scanf("%d",&n);
	if(n>count)exit(ERROR);
	for(i=0;i<n-1;i++)
	{
		q=q->next;
	}
	p=q->next;
	q->next=q->next->next;
	free(p);
	count--;
	printf("----Any other operation?\n"); 
}

char transform(char *operation)
{
	k:
	if(strcmp(operation,"get")==0)return 'A';
 	if(strcmp(operation,"seek")==0)return 'B';
 	if(strcmp(operation,"alter")==0)return 'C';
 	if(strcmp(operation,"add")==0)return 'D';
 	if(strcmp(operation,"delete")==0)return 'E';
 	if(strcmp(operation,"end")==0)return 'Z';
 	printf("----Wrong operation!Please enter operation again!\n");
 	scanf("%s",operation);
 	goto k;
}

int main()
{
	init();
 	char operation[30],flag;//用对应字母代表操作 :输入操作,自动转化成对应字母进入switch 
 	printf("----Enter operation:\n");
 	do
 	{
 		scanf("%s",operation);
 		flag=transform(operation);
 		
 		if(flag=='Z')break;
 		switch(flag)
 		{
 			case 'A':get(); write();break;
 			case 'B':seek(); break;
 			case 'C':alter(); write();break;
 			case 'D':add(); write();break;
 			case 'E':deletee(); write();break;
		}
	}while(1);
 	
return 0;	
}

![一个小测试]("D:\OneDrive\桌面\YO{KK20)3PFT}G7$U{HAAQP.png")
在这个测试中,程序好像找到了文件(因为没有显示Can not find the file!),但又没有成功从getfromfile函数跳回get函数,这是为什么?
2023/3/23 20:18
加载中...