C++ Todo

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster

C++ Todo

 
0
  #1
Sep 3rd, 2009
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: C++ Todo

 
0
  #2
Sep 3rd, 2009
  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.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 179
Reputation: Nathan Campos is an unknown quantity at this point 
Solved Threads: 6
Nathan Campos's Avatar
Nathan Campos Nathan Campos is offline Offline
Junior Poster

Re: C++ Todo

 
-1
  #3
Sep 3rd, 2009
Tom Gunn is right, and another thing, please put the code highlighting, like this: [ code=cplusplus ] ... [ /code ], but without spaces.

Thanks!
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: C++ Todo

 
0
  #4
Sep 3rd, 2009
I'm also thinking that this
  1. string todo = "/.todo.txt";
should be "./" instead of "/.".
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 179
Reputation: Nathan Campos is an unknown quantity at this point 
Solved Threads: 6
Nathan Campos's Avatar
Nathan Campos Nathan Campos is offline Offline
Junior Poster

Re: C++ Todo

 
0
  #5
Sep 3rd, 2009
Huh, now i'm thinking the same!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster

Re: C++ Todo

 
0
  #6
Sep 5th, 2009
files should be f & it is "/.todo" because this makes it a hidden file in Linux
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 179
Reputation: Nathan Campos is an unknown quantity at this point 
Solved Threads: 6
Nathan Campos's Avatar
Nathan Campos Nathan Campos is offline Offline
Junior Poster

Re: C++ Todo

 
0
  #7
Sep 5th, 2009
Huh, it's hidden, now i understand!
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: C++ Todo

 
0
  #8
Sep 5th, 2009
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 41
Reputation: pymatio is an unknown quantity at this point 
Solved Threads: 0
pymatio pymatio is offline Offline
Light Poster

Re: C++ Todo

 
0
  #9
Sep 6th, 2009
But the path will be "/home/USER/.todo"
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: C++ Todo

 
0
  #10
Sep 8th, 2009
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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 662 | Replies: 13
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC