944,007 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1998
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 3rd, 2009
0

C++ Todo

Expand Post »
I have this code:
#include <unistd.h>
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

void add(char todo[], string priority, string home){
    FILE *file;
    file = fopen(home.c_str(), "a");
    fprintf(file, "%-50s| %-6s\n", todo, priority.c_str());
    fclose(file);
}

void list(string home){
    string line;
    fstream file(home.c_str(),ios::in);
    int i = 0;
    if (file.is_open()){
          while( getline( file, line ) ) {
            cout << "[" << i << "] " << line << '\n';
            i++;
        }
    }
    file.close();
}

void clear(string home){
    FILE *file;
    file = fopen(home.c_str(), "w");
    fprintf(file, "");
    fclose(file);
}

void remove(string home, int n){
    string f;
    string line;
    fstream file(home.c_str(),ios::in);
    int i = 0;
    if (file.is_open()){
          while( getline( file, line ) ) {
            if (i != n){
                cout << i << '\n' << n << '\n';
                files += line;
                files += '\n';
            }
            i++;
        }
    }
    file.close();    
    clear(home);
    fstream fileo(home.c_str(),ios::out);    
    fileo << f;
}

void help(){
    cout << "Usage:\n";
    cout << "\tt-do [options]\n";
    cout << "Options\n";
    cout << "\t-a [TODO]\n";
    cout << "\t\tAdd a todo with summary TODO\n";
    cout << "\t-l\n";
    cout << "\t\tList TODOs\n";
    cout << "\t-c\n";
    cout << "\t\tClear all TODOs\n";
}
int main (int argc, char* argv[]) {
    string home(getenv("HOME"));
    string todo = "/.todo.txt";
    string priority = "Medium";      
    home = home + todo;
    int opt;
    int n;
    while ((opt = getopt(argc,argv, "a:p:f:rlhc")) !=EOF ){
        switch (opt){
            case 'a': 
                add(optarg, priority, home); 
                break; 
            case 'l':
                list(home);
                break;
            case 'c':
                clear(home);
                break;
            case 'r':
                n = atoi(optarg);
                remove(home, n);
                break;
            case 'h':
                help();
                break;
            case '?':
                help();
                break;      
        }
    }
    return 0;
}
when run with -r it should remove a line that the user specified, eg:
C++ Syntax (Toggle Plain Text)
  1. $t-do -r 1 #Should remove the first line
but it produces a segfault
Last edited by pymatio; Sep 3rd, 2009 at 1:44 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Sep 3rd, 2009
0

Re: C++ Todo

Quote ...
C++ Syntax (Toggle Plain Text)
  1. if (i != n){
  2. cout << i << '\n' << n << '\n';
  3. files += line;
  4. files += '\n';
  5. }
What is files ? You use that identifier as if it were a string, but it is never declared or defined anywhere.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Sep 3rd, 2009
-1

Re: C++ Todo

Tom Gunn is right, and another thing, please put the code highlighting, like this: [ code=cplusplus ] ... [ /code ], but without spaces.

Thanks!
Reputation Points: 33
Solved Threads: 6
Junior Poster
Nathan Campos is offline Offline
190 posts
since Jul 2009
Sep 3rd, 2009
0

Re: C++ Todo

I'm also thinking that this
C++ Syntax (Toggle Plain Text)
  1. string todo = "/.todo.txt";
should be "./" instead of "/.".
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Sep 3rd, 2009
0

Re: C++ Todo

Huh, now i'm thinking the same!
Reputation Points: 33
Solved Threads: 6
Junior Poster
Nathan Campos is offline Offline
190 posts
since Jul 2009
Sep 5th, 2009
0

Re: C++ Todo

files should be f & it is "/.todo" because this makes it a hidden file in Linux
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Sep 5th, 2009
0

Re: C++ Todo

Huh, it's hidden, now i understand!
Reputation Points: 33
Solved Threads: 6
Junior Poster
Nathan Campos is offline Offline
190 posts
since Jul 2009
Sep 5th, 2009
0

Re: C++ Todo

Yes, but "/.todo" is a hidden file *in the root of the drive*, and it's extremely probable that you don't want to be writing there. If you want a hidden file, fine, use ".todo" or "./todo" to reference the file ".todo" in the current working directory.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Sep 6th, 2009
0

Re: C++ Todo

But the path will be "/home/USER/.todo"
Reputation Points: 10
Solved Threads: 0
Light Poster
pymatio is offline Offline
48 posts
since Jun 2009
Sep 8th, 2009
0

Re: C++ Todo

Well then use "/home/USER/.todo" or better yet "~/.todo". Saying "/.todo" is like saying "/usr" or "/home"; it gets you a file relative to the root of the filesystem, which as I have said before, is definitely not what you want.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Value returning functions with one or more value parameters
Next Thread in C++ Forum Timeline: crack a cipher with string arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC