| | |
C++ Todo
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 41
Reputation:
Solved Threads: 0
I have this code:
when run with -r it should remove a line that the user specified, eg:
but it produces a segfault
#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;
} C++ Syntax (Toggle Plain Text)
$t-do -r 1 #Should remove the first line
Last edited by pymatio; Sep 3rd, 2009 at 1:44 pm.
•
•
•
•
C++ Syntax (Toggle Plain Text)
if (i != n){ cout << i << '\n' << n << '\n'; files += line; files += '\n'; }
files ? You use that identifier as if it were a string, but it is never declared or defined anywhere. -Tommy (For Great Justice!) Gunn
I'm also thinking that this
should be "./" instead of "/.".
C++ Syntax (Toggle Plain Text)
string todo = "/.todo.txt";
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
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
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
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
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
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
![]() |
Similar Threads
- Todo program (C)
- can someone plz help me with this? (Visual Basic 4 / 5 / 6)
- TODO: <Product Name>, (Viruses, Spyware and other Nasties)
- TODO: <Product Name>, Aurora Popup and an Automatic dialer problems... (Viruses, Spyware and other Nasties)
- CD-ROm isnt working correctly (Storage)
- MS Visual++ 6.0 - Error C2146, C2501 ... (C++)
- GDI (C)
Other Threads in the C++ Forum
- Previous Thread: Value returning functions with one or more value parameters
- Next Thread: crack a cipher with string arrays
Views: 659 | Replies: 13
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





