ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Hey guys I'm writing (well stumbling through) a pretty complex program for me. I'm not the most knowledgeable person when it comes to C++. The part I'm stuck on is I need to write a section of C++ code that will go into a directoy I store in a variable, go through all the files in that directory, create md5 sums for each file, and then store the file name and the md5 sum into a database I need to figure out how to create. I'm about to go crazy... If anyone can help that would be awsome... Thanks all!!!
Well, it would surely help if you posted the code you have written already.
People could get the idea that you want them to do your homework... ;-)
Anyway, why C? (if you don't know it very well)
Use bash or any other script language, in combination with the gnu program md5sum.
Which database do you want to use? Is there even any need for a database?
How many recordsets do you think this will create?
I wish it was my homework. A friend was saying how he couldn't find any good linux backup programs and said he had a really good idea for one. Well, he basically had an idea for an incrimental backup. Backup a directory and create md5sums for everyfile, then when you go to back up that directory again it checks the md5sums and only replaces the files that have changed. I have a few ideas I want to throw in as well but I want atleast to figure out a basic program. I like to program and made few cool programs in VB. But being I don't use windows anymore I had to pick a new language to learn and have always wanted to learn c/c++. That and the fact I figured almost every linux box would have gcc on it made me decide to go to c++.
This is a learn as you go program for me to help me learn C++ and C. sure I can make the hello world programs but I'm hopeing to get a lot more out of this.
I have written bash scripts that do create a db for the files and can generate md5sums but I was hopeing to stay in C or C++.
I have found only one thing for md5 in C which I have know idea what to do with it. But in all honesty if you have any helpful suggestions I would be really grateful. I have a sourceforge page but nothing is really on it cause I don't have a working program yet...
Thanks,
nomb
---Edit---
I was hopeing to use an mysql database so I could learn that too. I've had some experience with it on my myth box but not really enough to satisfy me.
---Edit 2---
I attached something that I think will allow me to implement it. But I don't really know how to use it or call it or pass anything to it...
I cant even get my menu compiled... :'(
ok.
If your friend opens his eyes a little and looks around he will find a linux backup program that does just what he wants it to do: rsync. It can do incremental (bitwise!) and differential backups, you can even run it through a ssh tunnel over the internet and there are many many more cool features. I use it for all company backup tasks and I'm completely happy with it.
Quote:
the fact I figured almost every linux box would have gcc on it made me decide to go to c++.
That should not be a reason. I've come across Linux boxes that didn't have a c compiler installed. On the other hand, it really isn't a big deal to install the packages you need to run something.
I understand this is a learning project for you. But - and please don't take this as an insult - if you don't know from scratch, this is not the right point for you to start.
I have to admit, it's been 2 years now for me since I last wrote in C, I was still a windows user then, and I've forgotten basically everything. One thing I remember, though, is that you have to include the right header files. You'll at least need
Code:
#include <iostream>
for your menu.
Another thing I think I had trouble with, when I tried to write C++ code on Linux: You can't compile C++ code (and cout/cin are C++ constructs) with the standard gcc compiler, you'll have to use a package that includes support for C++. That may or may not be the case on your box and with your packages.
Also, declaring a function is not enough, you'll also have to define it.
Read a tutorial! http://www.cplusplus.com/doc/tutorial/ is one that google just came up with. I'm sure there are more and better ones. STFW ...
In every programming language there is a way to execute external programs. I don't know the C function, but you will find out if you do some effort.
MySQL is a good choice (C++ is not, like I said before)... install a apache server with php-support and phpmyadmin, then play around with it. Or, if you want to make it tough on yourself, just install mysql and learn it on the command line.
g++ is the part of gcc that compiles c++ programs.
Yes I know there are incremental programs out there as I already stated but I like to further my knowledge on anything I can. This is by no means my first c++ program just one of the more complicated ones. I do know how to invoke shell commands from c++ and c but I wanted to try and do all this from within c++.
iostream is already include I just forgot to copy that line so I do apologize.
Really good reading. In particular, look in the sections about anonymous namespaces and the "using" keyword.
I think I need the commandline you are using as well, to see whether some weird option is causing strangeness with the linker. When I just do "make foobar" where foobar.cpp is the file with the source, it compiles fine except for the declared but undefined functions.
Ok guys I'm getting things to work nicely but I have a few other questions.
First, here is my code so far:
Code:
#include <iostream>
using namespace std;
void goodbye(void);
int menu();
int badinput();
int main ()
{
menu();
return 0;
}
void goodbye (void)
{
system("CLS");
cout << "Thank you for using IncBackup.\n";
cout << "Programed and designed by Nathan McBride.\n";
cout << endl;
cout << "Questions? Comments?\n";
cout << endl;
cout << "Feel free to email me.\n";
cout << "nomb85@adelphia.net\n";
cout << endl;
cout << "Hit any key to exit...";
system("PAUSE");
}
int menu ()
{
system("CLS");
cout << "1. New Backup\n";
cout << "2. Update Backup\n";
cout << "3. Restore Backup\n";
cout << "4. Quit\n";
cout << endl;
cout << "Selection: ";
int input;
cin >> input;
switch ( input )
{
case 1:
cout << "Newbackup selected...\n";
break;
case 2:
cout << "updatebackup selected...\n";
break;
case 3:
cout << "restorebackup selected...\n";
break;
case 4:
goodbye();
return 0;
default:
badinput();
return 0;
}
}
int badinput()
{
system("CLS");
cout << "I'm sorry that is not a valid choice.\n";
cout << "Please try again.\n";
system("PAUSE");
menu();
}
The problems I am having now is if someone enters "a" for the input value obviously it
wont work because it isn't an integer. Which is fine. But it gets stuck in the bad input loop for
some reason.
Second I was going to use char variable type which doesn't work with the menu it turns out so that
is fine. My other questions is when I ask for a path to backup. eg. /home/nomb/mythbox/videos/
how do I decalre a variable without knowing how long it needs to be.
If i decalre char path[50]; anything over 50 characters long wont work.
int input;
while (1){
cout << "Selection: ";
cin >> input;
if (!cin.fail()) {
break;
}
cin.clear();
cin.ignore(200, '\n');
cout << "Invalid Input" << endl;
};
You can use a char value if you replace your case 1 statements with case '1' and so on.
There is a maximum length a path can be for each OS. I think it is 32768 for Linux but I am not sure. There should be a define somewhere in the headers for this. You can use cin.getline to restrict input to a maximum size so for example if you want to use a max path of 255 (+1 for terminator) it would be.
> If I did system("Clear"); would that work in linux?
as long as you have a command on your system and in your path named Clear ('clear' on most distros) that clears the screen.. normally you should not worry about clearing the screen.. its not portable, not safe, and most of all its annoying.. the user can clear the screen if they want to clear the screen.. have a look at many of the apps you use from the console and pay attention to how many actually clear the screen when used.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.