一个txt文件中有中文和普通字符,把他们发送到链表中并由链表输出
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define LEN sizeof(struct Link_list)
struct Link_list {
char a;
struct Link_list*next;
} ;
char *s_gets(char *st,int n);
int n=0;
void put_out(struct Link_list*head);
struct Link_list*creat();
int main()
{ struct Link_list*head;
char b[30];
printf("do you want to use it?\n enter yes to start no to end :");
while(s_gets(b,30)){
if (strcmp(b,"yes")==0){
head=creat();
put_out(head);
printf("\n one time more?just printf yes or no!\n");
continue;}
else if (strcmp(b,"no")==0)
{
printf("bye bye!");
break;
}
else
{
printf("please printf yes or no!\nenter one of them!:");
continue;}}
return 0;
system("pause");
}
struct Link_list*creat()
{
char file[20];
FILE *fp;
struct Link_list*head=NULL;
struct Link_list*current,*prev;
prev=current=(struct Link_list*)malloc(LEN);
printf("please enter your file name:");
fscanf(stdin,"%s",file);
while ((fp=fopen(file,"r"))==NULL)
{
printf("\n your file name is wrong, please check it out enter again:");
fscanf(stdin,"%s",file);
}
fread(¤t->a,sizeof(char),1,fp);
while ((current->a)!=0&&!feof(fp))
{ n=n+1;
if (n==1)
head=current;
else
prev->next=current;
prev=current;
current=(struct Link_list*)malloc(LEN);
fread(¤t->a,sizeof(char),1,fp);
}
prev->next=NULL; if(fclose(fp)!=0)
fprintf(stderr,"ERROR!");
return head;
}
void put_out(struct Link_list*head)
{ struct Link_list*p=head;
if (head==NULL)
{
printf("The Link_list is NULL!\n");
exit(1);}
else
{
printf("The words are:\n");
while (p!=NULL)
{
printf("%c ",p->a);
p=p->next;
}}
}
char *s_gets(char *st,int n)
{
char *ret_val;
int i=0;
ret_val = fgets(st,n,stdin);
if(ret_val)
{ while(st[i]!='\n'&&st[i]!='\0')
i++;
if(st[i]=='\n')
st[i]='\0';
else
while (getchar()!='\n')
continue;}
return ret_val;
}
只能实现TXT文件中普通字符的输出,如果文件中有汉字就会乱码,求大神!!!