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
?

Recommended Answers

All 5 Replies

Could you first explain us what your program is supposed to do, and how the -r command line parameter should affect its output, so we don't have to puzzle everything out?

Usage:

$./todo -a "Hello" #add a todo
$./todo -l #list todos
1: Hello
$./todo -c #clear all todos
$./todo -r 1 remove the first todo - this doesn't work, at the moment it should print all lines apart form the first ('cos of the 1 passed)

Now it sort of works. It compiles with no warnings and writes to the file:

matio@matio-desktop:~/Documents/c/t-do$ ./todo -a "c"
matio@matio-desktop:~/Documents/c/t-do$ ./todo -a "b"
matio@matio-desktop:~/Documents/c/t-do$ ./todo -a "a"
matio@matio-desktop:~/Documents/c/t-do$ ./todo -l
 1: c
 2: b
 3: a
matio@matio-desktop:~/Documents/c/t-do$ ./todo -r 3
matio@matio-desktop:~/Documents/c/t-do$ ./todo -l
 1: a
 2: a
case 'r':	
				f = fopen(p, "r");	
				char *t[100];
				int n = atoi(optarg) - 1;
				int i = 0;		
				int i2 = 0;				
				while (fscanf(f, "%s\n", line) != EOF) {
					if (i != n){
						t[i2] = line;
						i2++;
					}
					i++;
				}
				t[i2] = '\0';
				i2 = 0;
		       		fclose(f);
				f = fopen(p, "w");
				fprintf(f, "");
				fclose(f);
				f = fopen(p, "a");
				for (i2 = 0; t[i2] != '\0'; i2++)
		       		{	
					fprintf(f, "%s\n", t[i2]);
				}

It only writes the last one

So why the double open?

f = fopen(p, "w");
				fprintf(f, "");
				fclose(f);
				f = fopen(p, "a");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.