User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 402,030 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,438 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.

Simple solution to database

Join Date: Jun 2007
Location: Shanghai
Posts: 16
Reputation: kinggarden is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
kinggarden kinggarden is offline Offline
Newbie Poster

Solution Simple solution to database

  #1  
Jun 11th, 2007
Hi everybody:

I'm trying to do the practices on the top of forum. I started from

beginner's level. This time I will show you my solution to implement

a simple database in C++. If you could give some advice on the

following aspects or other, it would be so nice of you

1. base datastructure. I used a 2D array to store a number of

pointers to string data. However, it's not easy to resize. I find it's not

easy to add a column.

2. design pattern. My solution really looks like a hardcoded solution

only to this question. How to find a better a design pattern than could

be reused easily?

3.algothirms.

4. multithreads and process protection

5....

I hope my post is not too long to make you boring.

my codes:

  1.  
  2.  
  3. #include "stdafx.h"
  4. #include "stdio.h"
  5. #include "stdlib.h"
  6. #include "string.h"
  7. #include "io.h"
  8. const char *filename = "d:/database.txt";
  9. const int ROWS = 10;
  10. const int COLOUMS = 3;
  11. class Data
  12. {
  13. public:
  14. Data() :
  15. m_nTotalColumns(COLOUMS),
  16. m_nTotalRows(ROWS),
  17. m_nCurCol(0),
  18. m_nCurRow(0),
  19. m_ppcData(NULL),
  20. m_fp(NULL)
  21. {
  22. }
  23.  
  24. virtual ~Data() {}
  25.  
  26. //create a dynamic 2 dimension char * array which points to a single data string
  27. bool Init() {
  28. m_ppcData = (char **)new char *[ROWS][COLOUMS];
  29. if(NULL == m_ppcData) {
  30. return false;
  31. }
  32. memset((void *)m_ppcData, NULL, ROWS * COLOUMS * sizeof(char *));
  33. return ReadFile();
  34. }
  35.  
  36. //insert a record with all values of fields
  37. void Insert(const char *name, const char *Gender, const char *old) {
  38. if (NULL == name || NULL == Gender || NULL == old) {
  39. return;
  40. }
  41. //array is full
  42. if(++m_nCurRow >= m_nTotalRows) {
  43. return;
  44. }
  45. StrCpy(m_ppcData[m_nCurRow * m_nTotalColumns], name);
  46. StrCpy(m_ppcData[m_nCurRow * m_nTotalColumns + 1], Gender);
  47. StrCpy(m_ppcData[m_nCurRow * m_nTotalColumns + 2], old);
  48. }
  49.  
  50. //updata a special data
  51. void Update(const unsigned int row, const unsigned int column, const char *data) {
  52. if(row > m_nCurRow || column >= m_nTotalColumns || NULL == data) {
  53. return;
  54. }
  55. StrCpy(m_ppcData[row * m_nTotalColumns + column], data);
  56. }
  57.  
  58. //select a special data
  59. char *Select(const unsigned int row, const unsigned int column) {
  60. if(row > m_nCurRow || column >= m_nTotalColumns) {
  61. return NULL;
  62. }
  63. return m_ppcData[row * m_nTotalColumns + column];
  64. }
  65.  
  66. //delete a row
  67. void DeleteRecord(const unsigned int row) {
  68. if(row > m_nCurRow) {
  69. return;
  70. }
  71. unsigned int i;
  72. unsigned int j;
  73. for(i = 0; i < m_nTotalColumns; i++) {
  74. delete [] m_ppcData[row * m_nTotalColumns + i];
  75. m_ppcData[row * m_nTotalColumns + i] = NULL;
  76. }
  77. //if the deleted row is not the last, i will fflush the data to the top
  78. if(row < m_nCurRow) {
  79. for(i = row; i < m_nCurRow; i++) {
  80. for(j = 0; j < m_nTotalColumns; j++) {
  81. m_ppcData[i * m_nTotalColumns + j] = m_ppcData[(i + 1) * m_nTotalColumns + j];
  82. }
  83. }
  84. }
  85. memset((void *)(m_ppcData + m_nCurRow * m_nTotalColumns), NULL, COLOUMS * sizeof(char *));
  86. m_nCurRow--;
  87. }
  88.  
  89. void display() {
  90. printf("**************\n");
  91. for(int i = 0; i < ROWS * COLOUMS; ++i) {
  92. if (NULL != m_ppcData && NULL != m_ppcData[i]) {
  93. printf("%s\n ", m_ppcData[i]);
  94. }
  95. }
  96. }
  97.  
  98. void Save() {
  99. WriteFile();
  100. }
  101. protected:
  102. const char* StrCpy(char *&dest, const char *src) {
  103. if(NULL == src) {
  104. return NULL;
  105. }
  106. //avoid copy self
  107. if(src == dest) {
  108. return dest;
  109. }
  110. //release original memory
  111. if(NULL != dest) {
  112. delete [ ]dest;
  113. dest = NULL;
  114. }
  115. int len = strlen(src);
  116. dest = new char[len + 1];
  117. if(NULL == dest) {
  118. return NULL;
  119. }
  120. return strcpy(dest, src);
  121. }
  122. //write data into file
  123. bool WriteFile() {
  124. m_fp = fopen(filename, "w+");
  125. if(NULL == m_fp) {
  126. return false;
  127. }
  128. for(int i = 0; i < ROWS * COLOUMS; ++i) {
  129. if (NULL != m_ppcData && NULL != m_ppcData[i]) {
  130. fputs(m_ppcData[i], m_fp);
  131. fputs(" ", m_fp);
  132. }
  133. }
  134. fclose(m_fp);
  135. return true;
  136. }
  137. //Read data From file
  138. //load all the data from the disk, do I need to do so?
  139. bool ReadFile() {
  140. m_fp = fopen(filename, "r+");
  141. if(NULL == m_fp) {
  142. return false;
  143. }
  144. long length = _filelength(_fileno(m_fp)) + 1;
  145. char *tmp = new char[length];
  146. if(NULL == tmp) {
  147. return false;
  148. }
  149. fgets(tmp, length, m_fp);
  150. int i = 0;
  151. char *pre = tmp;
  152. char *t = tmp;
  153. unsigned int pos;
  154. bool bIsEOF = false;
  155.  
  156. for(i = 0, pos = 0; (i < length) && (pos < m_nTotalRows * m_nTotalColumns); ++i) {
  157. if(*t == '\0' || *t == ' ') {
  158. //if we come to the end of the data, break
  159. if(*t == '\0') {
  160. StrCpy(m_ppcData[pos], pre);
  161. break;
  162. }
  163. else {
  164. *t = '\0';
  165. StrCpy(m_ppcData[pos], pre);
  166. //deal with multi spaces
  167. while (*++t == ' ') {
  168. }
  169. pre = t;
  170. pos++;
  171. }
  172. }
  173. &nref="http://bbs.fdc.com.cn/boardlist.asp?boardid=376" target=_blank>清江花园 [url="http://bbs.fdc.com.cn/boardlist.asp?boardid=277"]秦园居[/url] </DIV> <DIV class=tabcontent id=tcontent5> p; delete [] tmp;
  174. fclose(m_fp);
  175. return true;
  176. }
  177.  
  178. private:
  179. const unsigned int m_nTotalColumns;
  180. const unsigned int m_nTotalRows;
  181. unsigned int m_nCurCol;
  182. unsigned int m_nCurRow;
  183. FILE *m_fp;
  184. char **m_ppcData;
  185. };
  186.  
  187. int main(int argc, char* argv[])
  188. {
  189. Data d;
  190. if (d.Init()) {
  191. d.Update(2, 2, "100");
  192. d.Insert("Rock", "male", "24");
  193. d.display();
  194. char * p = d.Select(3, 2);
  195. printf("p = %s\n", p);
  196. d.DeleteRecord(1);
  197. d.display();
  198. d.Save();
  199. }
  200. return 0;
  201. }
  202.  
  203.  

You have to create a file called "d:/database.txt" on your disk, the contens look like this"name Gender old Rock male 100".

Thankyou very much for watching.
Rock Mu

I will keep walking!

Shanghai, China
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 10:28 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC