注册 登录
编程论坛 C语言论坛

一个txt文件中有中文和普通字符,把他们发送到链表中并由链表输出

gjp377020481 发布于 2017-10-17 20:01, 611 次点击
#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(&current->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(&current->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文件中普通字符的输出,如果文件中有汉字就会乱码,求大神!!!
2 回复
#2
吹水佬2017-10-17 20:32
//printf("%c ",p->a);
printf("%c",p->a);
#3
gjp3770204812017-10-17 20:53
回复 2楼 吹水佬
谢谢!
1