The C++ syntax has real molested me. I am completely stuck. For those who has assisted me before and the other programmers in the forum, i have this problem.
I wanted a syntax that will create a textfile database. I would like the database to have about 15 columns/ fields with headings that i have included in the struct below. The rows will be determined by the user and thus for every record user will input information against the column/ field heading for the column. Therefore the information is entered in a table with the following headings for the columns:
LR_UNIT_NUMBER | NAME_OF_PROPRIETOR | NATIONAL_ID_CARDNUMBER |
I would want to use a 2D vector to:
(a) populate and maintain the database at runtime by taking user input and write to the textfile database. //kindly assist how to "cout" and write with fstream object to the textfile database
(b) Read the whole database when a user prompts to read and display the contents of the database.
(c) Receive a string from user to query (by comparing with database) and Find a specific data from the database and return whether the record has been found within the database and the number of times such a record exists in the database.
//I created this small code from what i understand i should do but it cant do (I have not written the function to write to textfile since this initial code failed )
Someone showed to declare the vector using typedef as follows and would be glad to know how to loop through the columns and rows given such a declaration:
Sorry i was away for awhile. I had not seen those responses. I didnt mention that i am preparing a college project and thus i require a simple database which am informed can be prepared using C++ syntax. have never employed SQL syntax.
Is there any way of initializing a 2D vector of struct using user input, then writing its input to textfile which can then be read from by an fstream object ?
Who knows the syntax for 2D vector of structs ? And what about its implementation ? ? And how to push_back into that vextor ? If not possible, can i use the getline () ?
Your struct should have a constructor because it contains chars and ints that won't get well-defined values otherwise.
Now here's the big question: do you need the ability to modify existing records in the database? If so then a simple text file will be difficult because you'll have to write all the subsequent records too. In other words, if the file has 100 records and you want to modify record 15 then you'll have to rewrite records 15-100. This is because the space occupied by record 15 will probably grow or shrink if you change it. Now it looks like you modified your original post so if all those strings used to be fixed-length character arrays then I'd change them back. If you use fixed length strings then you will know the size of each record and you can modify any record without affected the others.
Of course this changes the file slightly: it makes it a binary file that stores text data.
I disagree with this. Think about it: if this was a good idea then we wouldn't have named members in classes at all. Everything would just be of type record.
Thanks Dhyaden. I am waiting for a response. I would like the text file contents not to be overwritten. There is no problem if the variables become fixed, therefore the strings datatype is okay.
That agreed, how will i push_back the struct into the 2D vector and thus populate the 2D vector with user input at runtime ? Then write contents to file ? Then the function to read whole file and display to screen ? How about checking for a particular string or strings from the textfile and compare with a user input string and display "Owner name " and "land number" has been found in the file and exists "number" times ?
I would want a syntax with no lambdas so i may easily understand so i may play around to put more functions.
One of the goals of a database is to not read all the records into memory. So I'd see if you can avoid reading everything into a 2D vector.
To start, go through the operations that you've mentioned and try to write function prototypes for each one. You may have to do this a couple of times to get it right. Once you have the prototypes, start filling in the code. I'll give you some examples from your last post:
how will i push_back the struct into the 2D vector and thus populate the 2D vector with user input at runtime ?
I think you don't want to put it into a 2D vector, but you will want to prompt the user for a record, so make a method for struct node:
1 2
// prompt the user for each field and populate the record with the results
bool getFromUser();
Then write contents to file ?
1 2
// write the record to the stream in binary format
bool write(ostream &);
Then the function to read whole file and display to screen ?
// read the record from the stream
bool read(istream &);
1 2
// Display the record in human-readable form
bool display();
How about checking for a particular string or strings from the textfile and compare with a user input string
You will need code that reads all records from a file. Start by writing this code and displaying each record. Once that works, change the code so it only prints the matching records.