标题:一个txt文件中有中文和普通字符,把他们发送到链表中并由链表输出
取消只看楼主
gjp377020481
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-10-17
结帖率:75%
已结贴  问题点数:20 回复次数:1 
一个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(&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文件中普通字符的输出,如果文件中有汉字就会乱码,求大神!!!
搜索更多相关主题的帖子: struct char head printf FILE 
2017-10-17 20:01
gjp377020481
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2017-10-17
得分:0 
回复 2楼 吹水佬
谢谢!
2017-10-17 20:53



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-481493-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.197736 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved