473,465 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Doing File operations

15 New Member
i am new to this...
i was making a menu driven program on file operations..
to add, delete, modify, search and display a particular record and list al the records...
i have not yet written function modify...
but the rest of the program shuld work properly but its not...
its not doing searching by name properly and that deleting records...
heres d code:
Expand|Select|Wrap|Line Numbers
  1.  class example
  2. {
  3. public:
  4.  
  5. char name[10];
  6. int number;
  7.  
  8. void getdetails()
  9. {
  10. cout<<"Enter the name"<
  11. gets(name);
  12. cout<<"Enter the number"<
  13. cin>>number;
  14. }
  15.  
  16. void showdetails()
  17. {
  18. cout<<"NAME: "<<<<"NUMBER: "<<
  19. }
  20.  
  21. };
  22.  
  23. void add()
  24. {
  25. example abc;
  26. abc.getdetails();
  27. ofstream file1("EXAMPLE.dat", ios::binary|ios::app);
  28. file1.write((char*)&abc, sizeof(abc));
  29. file1.close();
  30. }
  31.  
  32. void del()
  33. {
  34. int choice;
  35. char name[10];
  36. int number;
  37. example xyz;
  38. cout<<"Do you wish to search and delete according to name(1) or 
  39.  
  40. number(2)"<
  41. cin>>choice;
  42.  
  43. switch(choice)
  44.  
  45. {
  46. case 1:
  47. cout<<"Enter the name"<
  48. gets(name);
  49.  
  50. fstream file3, file2;
  51. file3.open("EXAMPLE.dat", ios::binary);
  52. file2.open("temp.dat", ios::binary);
  53.  
  54. while(file3)
  55. {
  56. file3.read((char*)&xyz, sizeof(xyz));
  57. if(!(abc.name==name))
  58. file2.write((char*)&xyz, sizeof(xyz));
  59. }
  60.  
  61. file3.close();
  62. file2.close();
  63. remove("EXAMPLE.dat");
  64. rename("temp.dat", "EXAMPLE.dat");
  65. break;
  66.  
  67. case 2:
  68. cout<<"Enter the number"<
  69. cin>>number;
  70.  
  71. fstream file4, file5;
  72. file4.open("EXAMPLE.dat", ios::binary);
  73. file5.open("temp.dat", ios::binary);
  74.  
  75. while(file4)
  76. {
  77. file4.read((char*)&xyz, sizeof(xyz));
  78. if(!(abc.number==number))
  79. file5.write((char*)&xyz, sizeof(xyz));
  80. }
  81.  
  82. file4.close();
  83. file5.close();
  84. remove("EXAMPLE.dat");
  85. rename("temp.dat", "EXAMPLE.dat");
  86. break;
  87.  
  88. default:
  89. cout<<"wrong choice"<
  90. break;
  91. }
  92. getch();
  93. }
  94.  
  95. void modify()
  96. {
  97.  
  98. }
  99.  
  100. void particular()
  101. {
  102. int choice;
  103. char name[10];
  104. int number;
  105. example abc;
  106. cout<<"Do you wish to search and display according to name(1) or 
  107.  
  108. number(2)"<
  109. cin>>choice;
  110.  
  111. switch(choice)
  112.  
  113. {
  114. case 1:
  115. cout<<"Enter the name"<
  116. gets(name);
  117. ifstream file6("EXAMPLE.dat", ios::binary);
  118.  
  119. while(file6)
  120. {
  121. file6.read((char*)&abc, sizeof(abc));
  122. if(abc.name==name)
  123. abc.showdetails();
  124. }
  125.  
  126. file6.close();
  127. break;
  128.  
  129. case 2:
  130. cout<<"Enter the number"<
  131. cin>>number;
  132. ifstream file7("EXAMPLE.dat", ios::binary);
  133.  
  134. while(file7)
  135. {
  136. file7.read((char*)&abc, sizeof(abc));
  137. if(abc.number==number)
  138. abc.showdetails();
  139. }
  140.  
  141. file7.close();
  142. break;
  143.  
  144. default:
  145. cout<<"wrong choice"<
  146. break;
  147. }
  148. getch();
  149. }
  150.  
  151. void listall()
  152. {
  153. example abc;
  154. ifstream file8("EXAMPLE.dat", ios::binary);
  155.  
  156. while(file8)
  157. {
  158. file8.read((char*)&abc, sizeof(abc));
  159. abc.showdetails();
  160. cout<
  161. }
  162.  
  163. file8.close();
  164. getch();
  165. }
  166.  
  167. void main()
  168. {
  169.  
  170. char ch='y';
  171. while(ch=='y')
  172. {
  173.  
  174. clrscr();
  175. int choice;
  176. cout<<"MENU:"<
  177. cout<<"1. Add a record"<
  178. cout<<"2. Delete a record"<
  179. cout<<"3. Modify a record"<
  180. cout<<"4. Search and display a particuar record"<
  181. cout<<"5. List all the records"<
  182. cout<<"6. Quit"<
  183. cout<
  184. cout<<"enter your choice"<
  185. cin>>choice;
  186.  
  187. switch(choice)
  188.  
  189. {
  190. case 1:
  191. add();
  192. break;
  193.  
  194. case 2:
  195. del();
  196. break;
  197.  
  198. case 3:
  199. modify();
  200. break;
  201.  
  202. case 4:
  203. particular();
  204. break;
  205.  
  206. case 5:
  207. listall();
  208. break;
  209.  
  210. case 6:
  211. exit(0);
  212. }
  213.  
  214. }
  215.  
  216. getch();
  217. }
please help me out....
Jun 29 '07 #1
3 1553
tracethepath
15 New Member
i am new to this...
i was making a menu driven program on file operations..
to add, delete, modify, search and display a particular record and list al the records...
i have not yet written function modify...
but the rest of the program shuld work properly but its not...
its not doing searching by name properly and that deleting records...
heres d code:
Expand|Select|Wrap|Line Numbers
  1.  class example
  2. {
  3. public:
  4.  
  5. char name[10];
  6. int number;
  7.  
  8. void getdetails()
  9. {
  10. cout<<"Enter the name"<
  11. gets(name);
  12. cout<<"Enter the number"<
  13. cin>>number;
  14. }
  15.  
  16. void showdetails()
  17. {
  18. cout<<"NAME: "<<<<"NUMBER: "<<
  19. }
  20.  
  21. };
  22.  
  23. void add()
  24. {
  25. example abc;
  26. abc.getdetails();
  27. ofstream file1("EXAMPLE.dat", ios::binary|ios::app);
  28. file1.write((char*)&abc, sizeof(abc));
  29. file1.close();
  30. }
  31.  
  32. void del()
  33. {
  34. int choice;
  35. char name[10];
  36. int number;
  37. example xyz;
  38. cout<<"Do you wish to search and delete according to name(1) or 
  39.  
  40. number(2)"<
  41. cin>>choice;
  42.  
  43. switch(choice)
  44.  
  45. {
  46. case 1:
  47. cout<<"Enter the name"<
  48. gets(name);
  49.  
  50. fstream file3, file2;
  51. file3.open("EXAMPLE.dat", ios::binary);
  52. file2.open("temp.dat", ios::binary);
  53.  
  54. while(file3)
  55. {
  56. file3.read((char*)&xyz, sizeof(xyz));
  57. if(!(abc.name==name))
  58. file2.write((char*)&xyz, sizeof(xyz));
  59. }
  60.  
  61. file3.close();
  62. file2.close();
  63. remove("EXAMPLE.dat");
  64. rename("temp.dat", "EXAMPLE.dat");
  65. break;
  66.  
  67. case 2:
  68. cout<<"Enter the number"<
  69. cin>>number;
  70.  
  71. fstream file4, file5;
  72. file4.open("EXAMPLE.dat", ios::binary);
  73. file5.open("temp.dat", ios::binary);
  74.  
  75. while(file4)
  76. {
  77. file4.read((char*)&xyz, sizeof(xyz));
  78. if(!(abc.number==number))
  79. file5.write((char*)&xyz, sizeof(xyz));
  80. }
  81.  
  82. file4.close();
  83. file5.close();
  84. remove("EXAMPLE.dat");
  85. rename("temp.dat", "EXAMPLE.dat");
  86. break;
  87.  
  88. default:
  89. cout<<"wrong choice"<
  90. break;
  91. }
  92. getch();
  93. }
  94.  
  95. void modify()
  96. {
  97.  
  98. }
  99.  
  100. void particular()
  101. {
  102. int choice;
  103. char name[10];
  104. int number;
  105. example abc;
  106. cout<<"Do you wish to search and display according to name(1) or 
  107.  
  108. number(2)"<
  109. cin>>choice;
  110.  
  111. switch(choice)
  112.  
  113. {
  114. case 1:
  115. cout<<"Enter the name"<
  116. gets(name);
  117. ifstream file6("EXAMPLE.dat", ios::binary);
  118.  
  119. while(file6)
  120. {
  121. file6.read((char*)&abc, sizeof(abc));
  122. if(abc.name==name)
  123. abc.showdetails();
  124. }
  125.  
  126. file6.close();
  127. break;
  128.  
  129. case 2:
  130. cout<<"Enter the number"<
  131. cin>>number;
  132. ifstream file7("EXAMPLE.dat", ios::binary);
  133.  
  134. while(file7)
  135. {
  136. file7.read((char*)&abc, sizeof(abc));
  137. if(abc.number==number)
  138. abc.showdetails();
  139. }
  140.  
  141. file7.close();
  142. break;
  143.  
  144. default:
  145. cout<<"wrong choice"<
  146. break;
  147. }
  148. getch();
  149. }
  150.  
  151. void listall()
  152. {
  153. example abc;
  154. ifstream file8("EXAMPLE.dat", ios::binary);
  155.  
  156. while(file8)
  157. {
  158. file8.read((char*)&abc, sizeof(abc));
  159. abc.showdetails();
  160. cout<
  161. }
  162.  
  163. file8.close();
  164. getch();
  165. }
  166.  
  167. void main()
  168. {
  169.  
  170. char ch='y';
  171. while(ch=='y')
  172. {
  173.  
  174. clrscr();
  175. int choice;
  176. cout<<"MENU:"<
  177. cout<<"1. Add a record"<
  178. cout<<"2. Delete a record"<
  179. cout<<"3. Modify a record"<
  180. cout<<"4. Search and display a particuar record"<
  181. cout<<"5. List all the records"<
  182. cout<<"6. Quit"<
  183. cout<
  184. cout<<"enter your choice"<
  185. cin>>choice;
  186.  
  187. switch(choice)
  188.  
  189. {
  190. case 1:
  191. add();
  192. break;
  193.  
  194. case 2:
  195. del();
  196. break;
  197.  
  198. case 3:
  199. modify();
  200. break;
  201.  
  202. case 4:
  203. particular();
  204. break;
  205.  
  206. case 5:
  207. listall();
  208. break;
  209.  
  210. case 6:
  211. exit(0);
  212. }
  213.  
  214. }
  215.  
  216. getch();
  217. }
please help me out....
pls... help me..
Jun 29 '07 #2
niskin
109 New Member
Which aspect do you want us to help you with? Do you know which lines are not compiling, or if it is compiling what is wrong?
Jun 29 '07 #3
tracethepath
15 New Member
Which aspect do you want us to help you with? Do you know which lines are not compiling, or if it is compiling what is wrong?
the function void delete is not working properly...It deletes all the data...and the function add, it adds the last entry twice..thanx 4 ur concern...
Jun 30 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Benny | last post by:
Hi, I would like to know, if I need to do some floting point operations (mainly multiplication and division) on each roll of a table, should I read the data out from the DB and do the...
3
by: Peter | last post by:
Hello, Two newbie questions: 1) I have a javascript file with a function in it. From this function I want to access a variable in another javascript file -which is not inside a function. I...
1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
3
by: Scott Brady Drummonds | last post by:
Hello, all, My most recent assignment has me working on a medium- to large-sized Windows-based C++ software project. My background is entirely on UNIX systems, where it appears that most of my...
27
by: Greg Smith | last post by:
Hello, I have been given a programming task that falls into the "impossible" category with my current skill set. I am hoping somebody out there knows how to do this and can save my b-t. I...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
1
by: Ben Kim | last post by:
Hello all, We are connecting to a remote server using FTP and sending up 100's of files daily to this site. I just received a call from the operations manager of this site and they are...
0
by: tabassumpatil | last post by:
Please send the c code for: 1.STACK OPERATIONS : Transfer the names stored in stack s1 to stack s2 and print the contents of stack s2. 2.QUEUE OPERATIONS : Write a program to implement...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.