Archived

This topic is now archived and is closed to further replies.

rholding2001

data base's and c++

Recommended Posts

ok what i want to do is call a database''s (db in future) field in c++. ive made a file inaccess that displays a first and a second name, i want in c++ to execute a command that cout''s field 1 (firstName) or infact the entire name. how do you do this. ive seen some tuts on the net but they are like 100 lines of code.....surely its not that hard to do. is access the right way to go about it? how would you easily write a set of names in notepad say and call them from c++. thanks for any help.

Share this post


Link to post
Share on other sites
I've never actually gotten into the whole database stuff, but it shouldn't be a problem to use access. The thing is, I believe that the functions needed to access the access database take a lot of initialising to work. I've heard that it could be a pain to get databases to work, but I can't say for certain. Google may be your best friend if no one else here knows much about it.

To load names from a text file, you can use the string class, and I think you need the iostream header, which should be included in Visual C++ if that's what you're using.

You can then use the other file io functions to load the text file as pure text. Now, you can do the following: (this is pseudo code, I hate to say it but I don't remeber the EXACT syntax to use the file type.)

string stuff;
FILE fp; (I think this has to be a pointer, not sure though)

fp = fopen("blah.txt", rt); (Check the arguments on this function, I don't remember if this was all)
fp>>stuff;

Then stuff would have a value of the first jumble of letters in blah.txt, up until the first white space or new line.
Repeat to get to the next chunk of letters:
fp>>stuff>>stuff;


Now stuff has a value of the next chunk of stuff. Remeber to use fclose(I think) on fp, or it'll be a memory leak.

So if you had a text file like this:

junk foo
garbage

Then, the first example would put junk into string. The second would put foo into it, and if you did fp>>stuff>>stuff>>stuff, it would have garbage in it. (I think, it may not do this on white space, I don't remember).

I wrote a BSP entities parser once, and used this method to get the data out of it... I'm pretty sure this is a horrible way to do it, but it works. Remeber to check the documentation on these functions, and the FILE type, because I'm dead certain that code won't compile as is.

Good luck though.

[edited by - Zealot on May 20, 2004 1:11:47 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by rholding2001
ive seen some tuts on the net but they are like 100 lines of code.....surely its not that hard to do.



Even a really simple example can easilly get 100 lines of code with all initializing stuff. ´

And to answer your question, there''s no way to do this in plain c++, you need some database library. I haven''t hower accessed a database from plain c++, if you don''t count borland c++ builder, with it''s built in libraries. So I can''t really recommend a good one.

Share this post


Link to post
Share on other sites
thanks Zealot

yeah immediatley after i posted i went looking for a text open file and yes it is easy to do

and your pseudo code is pretty much the same as the real one.

the difficulty with this is i need to create my own search algo''s that will determine the end of a field

so for example a data entry for a student could be john;computing;123456789 , these would be set up as different variables in a mulitidimensional array

array[name][course][enroll] - but this will obviosly go into parallel arrays as we are dealing with both chars(strings) and ints. so. yeah.

if there is anyone out there that has the simplest way to get read different parts of a text file and have them in their own fields according to the database then im all ears to suggestions.

Share this post


Link to post
Share on other sites
if there is anyone out there that has the simplest way to get read different parts of a text file and have them in their own fields according to the database then im all ears to suggestions.

boost::tokenizer - look it up.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites