打印文件内数据(重复数据只输出一次)并排序

打印文件内数据

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void main()
{
       int fd,i = 0;
       char buf = '\0';
       char str[512] = {'\0'};
       int arr[1500] = {0};
       fd = open("data.txt",O_RDONLY);
       while(0 != read(fd,&buf,1))
       {
              
              if(' ' == buf || '\n' == buf)
              {
                     arr[atoi(str)]++;
                     memset(str,0,sizeof(str));
                     i = 0;
                     continue;
              }
              str[i] = buf;
              i++;                 
       }
       arr[atoi(str)]++;
       
       for (int i = 1; i < 1500; ++i)
       {
              if(arr[i] == 0)
              {
                     continue;
              }
              else
              {
                     printf("%d\r\n",i);
              }
       }
       close(fd);
}

 

posted @ 2022-09-13 16:11  西北小蚂蚁  阅读(89)  评论(0编辑  收藏  举报