LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-30-2006, 07:07 PM   #1
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 673

Rep: Reputation: 57
Need a little help with C++


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!!!

nomb
 
Old 10-31-2006, 09:33 AM   #2
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
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?

Darn, now I am doing your homework...
 
Old 10-31-2006, 06:38 PM   #3
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 673

Original Poster
Rep: Reputation: 57


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... :'(

http://www.codeguru.com/code/legacy/...ms/md5_src.zip

---Edit 3---

Here is my menu:

Code:
using namespace std;

void newbackup();
void updatebackup();
void restorebackup();

int main() {

int input;
cout<<"1. New Backup\n";
cout<<"2. Update Backup\n";
cout<<"3. Restore Backup\n";
cout<<"4. Quit\n";
cout<<" \n";
cout<<"Selection: ";

cin>> input;

switch ( input ) {
	case 1:
	newbackup();
	break;
case 2:
updatebackup();
break;
case 3:
restorebackup();
break;
case 4:
break;
default:
cout << "Error, bad input, quitting\n";
break;
}
cin.get();
}
And the error:

Code:
/tmp/cc7zTtPp.o: In function `__static_initialization_and_destruction_0(int, int)':
incbackup.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc7zTtPp.o: In function `__tcf_0':
incbackup.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc7zTtPp.o: In function `main':
incbackup.cpp:(.text+0x8e): undefined reference to `std::cout'
incbackup.cpp:(.text+0x93): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0xa2): undefined reference to `std::cout'
incbackup.cpp:(.text+0xa7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0xb6): undefined reference to `std::cout'
incbackup.cpp:(.text+0xbb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0xca): undefined reference to `std::cout'
incbackup.cpp:(.text+0xcf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0xde): undefined reference to `std::cout'
incbackup.cpp:(.text+0xe3): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0xf2): undefined reference to `std::cout'
incbackup.cpp:(.text+0xf7): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0x105): undefined reference to `std::cin'
incbackup.cpp:(.text+0x10a): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(int&)'
incbackup.cpp:(.text+0x137): undefined reference to `newbackup()'
incbackup.cpp:(.text+0x13e): undefined reference to `updatebackup()'
incbackup.cpp:(.text+0x145): undefined reference to `restorebackup()'
incbackup.cpp:(.text+0x156): undefined reference to `std::cout'
incbackup.cpp:(.text+0x15b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
incbackup.cpp:(.text+0x162): undefined reference to `std::cin'
incbackup.cpp:(.text+0x167): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
/tmp/cc7zTtPp.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

Last edited by nomb; 10-31-2006 at 07:48 PM.
 
Old 11-01-2006, 05:15 AM   #4
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
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.

Baik

Last edited by baikonur; 11-01-2006 at 05:43 AM.
 
Old 11-01-2006, 07:03 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
you'll have to use a package that includes support for C++.
normally that would be g++

use g++ to compile your code (and add the headers and function definitions) and the errors should go away.
 
Old 11-01-2006, 07:12 PM   #6
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 673

Original Poster
Rep: Reputation: 57
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.
 
Old 11-01-2006, 07:52 PM   #7
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 1,997

Rep: Reputation: 107Reputation: 107
http://www.parashift.com/c++-faq-lite/

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.
 
Old 11-01-2006, 09:02 PM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,858
Blog Entries: 1

Rep: Reputation: Disabled
nomb -

Please try this: it worked for me, maybe there's something simple wrong with your "g++" command:
Code:
#include <iostream>

using namespace std;

int
main (int argc, char *argv[])
{
  cout << "1. New Backup\n";
  cout << "2. Update Backup\n";
  cout << "3. Restore Backup\n";
  cout << "4. Quit\n";
  cout << " \n";
  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:
      cout << "Hasta la bye bye!\n";
      return 0;
    default:
      cout << "Error, bad input, quitting\n";
  }

  return 0;
}
Quote:
SAMPLE BUILD:
g++ -g -Wall -pedantic x.cpp
<= Unless you specify "-o EXENAME", it will write to "a.out"
Quote:
SAMPLE EXECUTE:
./a.out
1. New Backup
2. Update Backup
3. Restore Backup
4. Quit

Selection: 1
Newbackup selected...

Last edited by paulsm4; 11-01-2006 at 09:04 PM.
 
Old 11-02-2006, 01:46 PM   #9
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 673

Original Poster
Rep: Reputation: 57
Hmm, ya got that to work perfectly.

Guess my problem is whith trying to do differant sub routines. Guess I need to figure our to do that. I'm gonna look at that resource above.
 
Old 11-02-2006, 03:38 PM   #10
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 673

Original Poster
Rep: Reputation: 57
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.

Thanks,

nomb

Last edited by nomb; 11-02-2006 at 03:45 PM.
 
Old 11-02-2006, 03:58 PM   #11
Denes
Member
 
Registered: Mar 2004
Distribution: CentOS 4.3/4.5
Posts: 72

Rep: Reputation: 15
This loop will only accept valid input:

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.

char Path[256];
cin.getline(Path, 256);
 
Old 11-03-2006, 01:49 AM   #12
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
Quote:
I think it is 32768 for Linux but I am not sure.
I get the idea he's on Windows anyway:
Quote:
system("CLS");
 
Old 11-03-2006, 04:29 PM   #13
nomb
Member
 
Registered: Jan 2006
Distribution: Debian Testing
Posts: 673

Original Poster
Rep: Reputation: 57
I was at work writing that code but it is being made for linux.

If I did system("Clear"); would that work in linux?

Oh and I found out if I do #include <string> I can get input of longer length. At least thats how I read it.

nomb
 
Old 11-04-2006, 03:36 AM   #14
baikonur
Member
 
Registered: Oct 2005
Location: germany
Distribution: debian
Posts: 255
Blog Entries: 5

Rep: Reputation: 30
Quote:
If I did system("Clear"); would that work in linux?
Try.
(when you're home)
 
Old 11-04-2006, 08:32 AM   #15
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> 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.
 
  


Reply

Tags
c++, md5


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 09:04 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration