detab.cpp [C++]
// detab
#include <iostream>
using namespace std;
void detab(FILE *src, FILE *dst, int width){
int ch, pos = 1;
while((ch = fgetc(src)) != EOF){
int num;
switch(ch){
case '\t': //タブ文字
num = width - (pos - 1) % width;
for( ;num > 0; num--){
fputc(' ', dst);
pos++;
}
break;
case '\n': //改行文字
fputc(ch, dst);
pos = 1;
break;
default: //通常文字
fputc(ch, dst);
pos++;
break;
}
}
}
int main(int argc, char *argv[]){
int width = 8;
FILE *fp;
if(argc < 2){
detab(stdin, stdout, width);
}
else{
while(--argc > 0){
if(**(++argv) == '-'){
if(*++(*argv) == 't'){
width = atoi(++*argv);
}
else{
fputs("パラメータが不正です。\n", stderr);
return(1);
}
}
else if((fp = fopen(*argv, "r")) == NULL){
fprintf(stderr, "ファイル%sがオープンできません。\n", *argv);
return(1);
}
else{
detab(fp, stdout, width); //ファイルから標準出力へ
fclose(fp);
}
}
}
return(0);
}
─────
コメント 0