#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Eitem{
char item[100];
}Eitem;
Eitem table[7777][70];
char line[1000];
const char *split = "~";
char sep;
int skip, i,j,k,h,count;
int opt[80];
int main(int argc, char * argv[])
{
int ch;
while ((ch = getopt(argc, argv, "s:i:")) != -1)
{
printf("optind: %d\n", optind);
switch (ch) {
case 's':
sep = *optarg;
printf("HAVE option: -s, seperated by %s\n", optarg);
break;
case 'i':
skip = atoi(optarg);
printf("HAVE option: -i ");
printf("The argument of -i is %s\n", optarg);
break;
case '?':
printf("Unknown option: %c\n",(char)optopt);
break;
}
}
for(i = optind; i < argc; i ++){
opt[i - optind] = atoi(argv[i]);
}
//check rule
if( opt[0] =='\0'){
printf("Error : you are supposed to choose fields to display ! \n");
exit(0);
}
for(i = 0; opt[i+1] != '\0'; i++){
if( opt[i] >= opt[i+1]){
printf("Error : Field numbers are supposed to be provided in increasing sequence and no repeatation !\n");
exit(0);
}
}
//unix input already
// scan the file and establish the table
while(fgets(line, sizeof(line), stdin)){
for(i = 0; ; i++){
if( line[i] == '\"')
count++;
if( line[i] == ',' && count%2 == 0)
line[i] = '~';
if( line[i] == '\0')
break;
}
char *str = strtok(line, split);
j = 0;
while (str != NULL) {
strcpy(table[k][j].item,str);
str = strtok(NULL, split);
j ++;
}
k ++;
}
//table already
for(i = skip; table[i][0].item[0] != '\0';i++){
for(j = 0; opt[j] != '\0'; j++){
printf("%s %c ",table[i][opt[j] - 1].item,sep);
}
printf("\n");
}
}
anet
相关分类