从文本内容中提取有效信息

例如a.txt中有如下内容
$ABC,eqwe,0123,N,we23,E,234
$ABD,fkjd,2454,N,fwer,E,456
$AB,fhew,9478,N,wewf,E,rnju
$ABC,wefn,9834,N,riwqj,E,42

要求:

提取的有效信息为
eqwe,0123,we23
wefn,9834,riwqj

代码实现:

#include
#include
#include
#include
#include
int i=0; //存放信息时使用
int main(){
     int fd=open( "a.txt" ,O_RDONLY,0644);
     
     char c; //取出每一个字符
     ssize_t bytes;
     char xinxi[256]; //存放提取信息
     if (fd==-1)
     {
         perror ( "open" );
         return -1;
         }
     //不断的读
     while (bytes=(read(fd,&c, sizeof (c)))>0)
     {
         if (c== '$' )
             { //文件位置向后移动一位
             read(fd,&c, sizeof (c));
             if (c== 'A' )
             {
                             read(fd,&c, sizeof (c));
                 if (c== 'B' )
                     {
                                 read(fd,&c, sizeof (c));
                         if (c== 'C' )
                         {
                                     read(fd,&c, sizeof (c)); //指针移到逗号前
                                     read(fd,&c, sizeof (c)); //
                         if (c!= ',' ){
                             while (c!= 'N' ) //提取信息
                             {
                                 {xinxi[i]=c; //将有用字符存入数组
                                 ++i;
                                             read(fd,&c, sizeof (c));}
 
                             }
                                         read(fd,&c, sizeof (c)); //
                             if (c!= ',' )
                             continue ;}
                         }
                         else
                         {
                         while (c!= '\n' )
                                     read(fd,&c, sizeof (c));
                         continue ; //跳出while循环,并开始读取信息
                         }
                     }
             }
             }
     else if (c!= 'E' &&c!= ',' )
         {
         xinxi[i]=c;
         ++i;
         }
         else {
             while (c!= '\n' )
                     read(fd,&c, sizeof (c));
             continue ;}
         } //成功读取后的操作
     if (bytes==-1)
     {
         perror ( "read" );
         return -1;
         }
         int m;
         for (m=0;xinxi[m];++m)
             printf ( "%c" ,xinxi[m]);
             printf ( "\n" );
     
     return 0;
     }

你可能感兴趣的:(C++)