I have fixed the -s option to eliminate excess blank lines but still am having some difficulty with parsing multiple command line options eg -ns. Is it possible to do this using this approach that i am trying?
If i attempt to pass ./prog -ns file.txt in the command line it returns
Case n!
File -ns couldn't be opened!
Segmentation fault
Rather than listing lines and eliminating excess blank lines.
When -n file.txt is passed it no longer prints newlines due to my logic problem.
Code:
void concatenate_files(char option){
for(index = optind; index < argc; index++){
fp = fopen(argv[index], "r");
if(fp == NULL){
fprintf(stdout,"File %s couldn't be opened!\n", argv[index]);
}else{
printf("File opened!\n");
}
printf("%c\n", option);
int c;
int aflag = 1;
int bflag = 1;
while((c = fgetc(fp)) != EOF){
if(aflag != 0 && option == 'n'){
printf("%d ", lineCount);
lineCount++;
aflag = 0;
}
if(c == '\n' && bflag != 0 && option == 's'){
printf("%c", c);
bflag = 0;
}
if(c != '\n'){
printf("%c", c);
bflag = 1;
}
if(c == '\n'){
aflag = 1;
}
}
fclose(fp);
}
}
Code:
while((optchar = getopt (argc, argv, "nsvt")) != -1){
switch(optchar){
case 'n':
printf("Case n!\n");
concatenate_files('n');
break;
case 's':
printf("Case s!\n");
concatenate_files('s');
break;
case 'v':
break;
case 't':
break;
}
}