Page 1 of 4 123 ... LastLast
Results 1 to 10 of 31

Thread: C todo program

  1. #1
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    C todo program

    I have the following program which works, apart from the '-r':
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
            FILE *f;
            int optchar;
    		char line[300];
            while ((optchar = getopt(argc, argv, "r:a:ldc")) != -1){
                    switch (optchar){
                            case 'a':
                                    f = fopen("/home/matio/todo.txt", "a");
                                    fprintf(f, (char *) strdup (optarg));
                                    fprintf(f, "\n");
                                    break;
                            case 'l':
                                    f = fopen("/home/matio/todo.txt", "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("/home/matio/todo.txt", "w");
                                    fprintf(f, "");
    								break;
    			case 'r':	
    				f = fopen("/home/matio/todo.txt", "r");	
    				FILE *f2;					
    				f2 = fopen("/home/matio/todo2.txt", "a");											
    				int n = atoi(optarg) - 1;
    				int i = 0;		
    				int i2 = 0;					
    				while (fscanf(f, "%s\n", line) != EOF) {
    					if (i != n){
    						fprintf(f2, "%s\n", line);	
    					}
    					i++;
    				}
                                    f = fopen("/home/matio/todo.txt", "w");
                                    fprintf(f, "");							
    				rename("/home/matio/todo2.txt", "/home/matio/todo.txt");							
                   }
    
           }
    }
    example:
    Code:
    matio@matio-desktop:~/Documents/c$ ./todo -l #print todo list (nothing there)
    matio@matio-desktop:~/Documents/c$ ./todo -a "Post this to ubuntu forums"
    matio@matio-desktop:~/Documents/c$ ./todo -a "Post this to ubuntu forums" # oops, I entered it twice
    matio@matio-desktop:~/Documents/c$ ./todo -r 1 #remove the first line of todo.txt
    matio@matio-desktop:~/Documents/c$ ./todo -l
     1: this
     2: to
     3: ubuntu
     4: forums
     5: Post
     6: this
     7: to
     8: ubuntu
     9: forums
    matio@matio-desktop:~/Documents/c$ #?!
    (I know making two files is probably the worst way of doing this, but I can't think of any other way to do it)

  2. #2
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C todo program

    [ BUMP ]
    Should I use an array of pointers:
    Code:
    char *p[100];
    //char line[100]
    //line holds a line of the file
    p[i] = line;
    [ BUMP ]
    Last edited by matmatmat; August 20th, 2009 at 11:48 AM.

  3. #3
    Join Date
    May 2006
    Beans
    1,790

    Re: C todo program

    Quote Originally Posted by matmatmat View Post
    I have the following program which works, apart from the '-r':
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
            FILE *f;
            int optchar;
    		char line[300];
            while ((optchar = getopt(argc, argv, "r:a:ldc")) != -1){
                    switch (optchar){
                            case 'a':
                                    f = fopen("/home/matio/todo.txt", "a");
                                    fprintf(f, (char *) strdup (optarg));
                                    fprintf(f, "\n");
                                    break;
                            case 'l':
                                    f = fopen("/home/matio/todo.txt", "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("/home/matio/todo.txt", "w");
                                    fprintf(f, "");
    								break;
    			case 'r':	
    				f = fopen("/home/matio/todo.txt", "r");	
    				FILE *f2;					
    				f2 = fopen("/home/matio/todo2.txt", "a");											
    				int n = atoi(optarg) - 1;
    				int i = 0;		
    				int i2 = 0;					
    				while (fscanf(f, "%s\n", line) != EOF) {
    					if (i != n){
    						fprintf(f2, "%s\n", line);	
    					}
    					i++;
    				}
                                    f = fopen("/home/matio/todo.txt", "w");
                                    fprintf(f, "");							
    				rename("/home/matio/todo2.txt", "/home/matio/todo.txt");							
                   }
    
           }
    }
    example:
    Code:
    matio@matio-desktop:~/Documents/c$ ./todo -l #print todo list (nothing there)
    matio@matio-desktop:~/Documents/c$ ./todo -a "Post this to ubuntu forums"
    matio@matio-desktop:~/Documents/c$ ./todo -a "Post this to ubuntu forums" # oops, I entered it twice
    matio@matio-desktop:~/Documents/c$ ./todo -r 1 #remove the first line of todo.txt
    matio@matio-desktop:~/Documents/c$ ./todo -l
     1: this
     2: to
     3: ubuntu
     4: forums
     5: Post
     6: this
     7: to
     8: ubuntu
     9: forums
    matio@matio-desktop:~/Documents/c$ #?!
    (I know making two files is probably the worst way of doing this, but I can't think of any other way to do it)
    You seem to always append to todo2, never clearing it. What does the program do that it shouldn't, you're not telling us that.

  4. #4
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C todo program

    Code:
     1: this
     2: to
     3: ubuntu
     4: forums
     5: Post
     6: this
     7: to
     8: ubuntu
     9: forums
    should be
    Code:
    1: Post this to ubuntu forums

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,285
    Distro
    Kubuntu

    Re: C todo program

    I believe you require a format statement (ie. something like "%s") in your first fprintf() below.
    Code:
                            case 'a':
                                    f = fopen("/home/matio/todo.txt", "a");
                                    fprintf(f, (char *) strdup (optarg));
                                    fprintf(f, "\n");
                                    break;
    Also, strdup() allocates memory; since you are not freeing it, you have a memory leak.

    As matmatmat pointed out, you should consider reading in the contents of your file into some sort of container. An array could possibly work, but a linked list would be better.

    Once the contents are in the container, it should be easy to remove and add new entries.

    You could also consider augmenting your app to support the prioritization of the TODO items.

  6. #6
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C todo program

    Thanks, I don't know why I used strdup (I wrote this quite a while ago)
    Linked list?

  7. #7
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C todo program

    Quote Originally Posted by dwhitney67 View Post
    I believe you require a format statement (ie. something like "%s") in your first fprintf() below.
    Code:
                            case 'a':
                                    f = fopen("/home/matio/todo.txt", "a");
                                    fprintf(f, (char *) strdup (optarg));
                                    fprintf(f, "\n");
                                    break;
    The "todo -a "whatever" works, it's the -r that doesn't.

    I now have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
            FILE *f;
            int optchar;
    		char line[300];
            while ((optchar = getopt(argc, argv, "r:a:ldc")) != -1){
                    switch (optchar){
                            case 'a':
                                    f = fopen("/home/matio/todo.txt", "a");
                                    fprintf(f, (char *) strdup (optarg));
                                    fprintf(f, "\n");
                                    break;
                            case 'l':
                                    f = fopen("/home/matio/todo.txt", "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("/home/matio/todo.txt", "w");
                                    fprintf(f, "");
    								break;
    			case 'r':	
    				f = fopen("/home/matio/todo.txt", "r");	
    				FILE *f2;					
    				f2 = fopen("/home/matio/todo.txt", "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;
    						i2++;
    					}
    					i++;
    				}
    				i2 = 0;
    		       		fprintf(f2, "");
    				f2 = fopen("/home/matio/todo.txt", "a");
                                    //error here, the increment
    				for (i2 = 0; *p != '\0'; p++)
    					i2++;
                   }  
    
           }
    }
    when compiled:
    Code:
    todo.c: In function ‘main’:
    todo.c:13: warning: incompatible implicit declaration of built-in function ‘strdup’
    todo.c:13: warning: format not a string literal and no format arguments
    todo.c:54: warning: assignment from incompatible pointer type
    todo.c:62: error: lvalue required as increment operand

    (I'm guessing it's because it's trying to do something with the string that is being pointed 'at')

  8. #8
    Join Date
    May 2007
    Location
    Santiago, Chile
    Beans
    Hidden!
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: C todo program

    For the incompatible pointer type warning (p[i2] = &line it's because you are trying to assign the address of the pointer to line[0] to a value p[i2].

    Remember that when you declare an array of characters, the name of the array is also the address of its first element. Instead you can index the array to get the value at a specific location (e.g. line[i])

    For the increment problem, I think it'd be better to index the arrays instead of incrementing the pointer

    Code:
    for (i2 = 0; p[i2] != '\0'; i2++)
    I didn't really try to completely understand your code (I'm at work, so I don't have too much time) but these things might cause problems.

    Good luck!!

  9. #9
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C todo program

    Thanks, it compiles now
    I didn't notice the warning about the & (I was only trying it to say if it worked)
    I now have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main(int argc, char *argv[]){
            FILE *f;
            int optchar;
    		char line[300];
            while ((optchar = getopt(argc, argv, "r:a:ldc")) != -1){
                    switch (optchar){
                            case 'a':
                                    f = fopen("/home/matio/todo.txt", "a");
                                    fprintf(f, (char *) strdup (optarg));
                                    fprintf(f, "\n");
                                    break;
                            case 'l':
                                    f = fopen("/home/matio/todo.txt", "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("/home/matio/todo.txt", "w");
                                    fprintf(f, "");
    								break;
    			case 'r':	
    				f = fopen("/home/matio/todo.txt", "r");	
    				FILE *f2;					
    				f2 = fopen("/home/matio/todo.txt", "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;
    						i2++;
    					}
    					i++;
    				}
    				i2 = 0;
    		       		fprintf(f2, "");
    				f2 = fopen("/home/matio/todo.txt", "a");
    				for (i2 = 0; p[i2] != '\0'; i2++)
    		       		{	printf(p[i2]);}	
                   }  
    
           }
    }
    running:
    Code:
    matio@matio-desktop:~/Documents/c$ vim todo.c
    matio@matio-desktop:~/Documents/c$ gcc todo.c -o todo
    todo.c: In function ‘main’:
    todo.c:13: warning: incompatible implicit declaration of built-in function ‘strdup’
    todo.c:13: warning: format not a string literal and no format arguments
    todo.c:54: warning: assignment makes pointer from integer without a cast
    todo.c:63: warning: format not a string literal and no format arguments
    matio@matio-desktop:~/Documents/c$ ./todo -a "1"
    matio@matio-desktop:~/Documents/c$ ./todo -l
     1: 1
    matio@matio-desktop:~/Documents/c$ ./todo -r 1
    GLIBC_2.1GLIBC_2.1�@����L$�T$�@e�a: Symbol `H������' has different size in shared object, consider re-linking
    <program name unknown><program name unknown>�@����L$�T$�@e�a: Symbol `H������' has different size in shared object, consider re-linking
    Segmentation fault
    matio@matio-desktop:~/Documents/c$
    Something to do with overwriting line?
    Last edited by matmatmat; August 20th, 2009 at 04:44 PM.

  10. #10
    Join Date
    Apr 2006
    Location
    Atlanta, USA
    Beans
    427

    Re: C todo program

    Sorry to post and not help with your question but I wanted to reiterate something that was said earlier; this warning:

    Code:
    test.c:21: warning: format not a string literal and no format arguments
    when using lines like
    Code:
     fprintf(f, (char *) strdup (optarg));
    is important. Consider this:

    Code:
    $ ./test -l
     1: hello world
    $ ./test -a "buy 2% milk from store"
    $ ./test -l
     1: hello world
     2: buy 2Successilk from store
    $ ./test -a "research %d & %s modifiers"
    Segmentation fault
    Either use fputs or fprintf(fp, "%s", ...) so that any special characters inside the user input don't cause unexpected behavior.

    (Also you should free the memory allocated with strdup, but I wouldn't care too much about it in this instance since the program only runs for a short period of time and the amount leaked is minor).
    Here we are, trapped in the amber of the moment. There is no why.

Page 1 of 4 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •