I have this code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]){
char *s = getenv ("HOME");
FILE *f;
int optchar;
char line[300];
char p[100];
strcpy(p, s);
strcat(p, "/todo.txt");
while ((optchar = getopt(argc, argv, "r:a:ldc")) != -1){
switch (optchar){
case 'a':
f = fopen(p, "a");
fprintf(f, (char *) strdup (optarg));
fprintf(f, "\n");
break;
case 'l':
f = fopen(p, "r");
int count = 1;
printf("%2i: ", count);
while (! feof(f)){
char ch = getc(f);
if (ch == EOF){
break;
}
if (ch == '\n'){
printf("%c", ch);
count++;
ch = getc(f);
if (ch != EOF){
printf("%2i: ", count);
}
else{
break;
}
}
printf("%c",ch);
}
break;
case 'c':
f = fopen(p, "w");
fprintf(f, "");
break;
case 'r':
f = fopen(p, "r");
FILE *f2;
f2 = fopen(p, "w");
char *p[100];
int n = atoi(optarg) - 1;
int i = 0;
int i2 = 0;
while (fscanf(f, "%s\n", line) != EOF) {
if (i != n){
p[i2] = line[0];
i2++;
}
i++;
}
i2 = 0;
fprintf(f2, "");
f2 = fopen(p, "a");
for (i2 = 0; p[i2] != '\0'; i2++)
{ printf(p[i2]);}
}
}
}
the '-r' option should remove the line specified by the user. When called nothing is printed out
?