strsep 分割字符串后,如何控制只输出部分子串
一个一百列由制表符分割的文本文件,每行读入分割后,该如何只输出第11列到最后一列的内容呢?先谢谢大家!
程序代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/types.h> #include <regex.h> int main(int argc,const char *argv[]) { FILE *fp; size_t len = 0; char *str = NULL; ssize_t read; // reg compile regex_t reg; const char *pat = "^##"; int cflags = REG_EXTENDED | REG_NEWLINE; int ret = regcomp(®,pat,cflags); char err_info[256]; // search const size_t nmatch = 1; regmatch_t pmatch[nmatch]; if(argc != 2) { fprintf(stderr,"usage: %s <src>\n",argv[0]); exit(1); } if((fp=fopen(argv[1],"r")) == NULL) { perror("fopen()"); exit(1); } if(ret!=0) { regerror(ret,®,err_info,256); fprintf(stderr,"%s\n",err_info); regfree(®); exit(1); } char *line = NULL; while((read = getline(&str,&len,fp)) != -1) { str[read-1]='\0'; int reg_res=regexec(®,str,nmatch,pmatch,0); if(reg_res == 0) continue; while((line = strsep(&str,"\t")) != NULL) { fprintf(stdout,"%s-->",line); ## 就是这里不知道该怎么处理了 } printf("\n"); } regfree(®); fclose(fp); exit(0); }