Hello,
my cat implementation is sort of working but when i put multiple options like -ne, it doesnt work properly.
it should print something like this
1 text$
2 txt$
3 abc$
....
mine prints like this
1 text
2 txt
3 abc
text$
txt$
abc$
my main.c
Code:
while ((c = getopt (argc, argv, "ne")) != -1) {
for(int i = 2; i < argc; i++) {
file = fopen ( argv[i], "r" );
if (file == NULL){
puts ( "\nNo such file or directory\n" );
}
}
// printf("%c", optopt);
switch (c) {
case 'n':
optLineNums(argc, argv, file, count, chars);
break;
case 'e':
flagE = 1;
optEndOfLine(file, chars, argc, argv);
break;
case '?':
fprintf (stderr, "Unknown option character '-%c'. Use -e or -n options\n", optopt);
break;
default:
abort ();
}
}
cat.c
Code:
void optLineNums(int argc, char *argv[], FILE *file, int count, char chars) {
int charCount = 0;
while ( argc ) {
chars = fgetc (file);
if ( chars == EOF )
break;
else {
if(charCount == 0){
printf(" %i\t",count);
charCount++;
}
if (chars == '\n'){
count++;
printf("\n");
printf(" %i\t",count);
} else
fputc ( chars, stdout );
}
}
}
void optEndOfLine(FILE *file, char chars, int argc, char *argv[]) {
while ( argc ) {
chars = fgetc (file);
if (chars == '\n')
chars = '$';
if ( chars == EOF )
break;
else
fputc ( chars, stdout );
if (chars == '$')
puts("");
}
}
Thanks in advance