Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 13th, 2006, 12:24 PM   #1
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 4 n00b is on a distinguished road
Send a message via MSN to n00b
file managing problem

i wrote a program which takes student data and inputs them into both the main and index file. it let's you enter the data manually (no rand), prints all the data, let's you search the database by a student's number.

i guess something must've went wrong, because when i tell it to print the data, it prints only some numbers and simbols. also, when i try to search for a student's by it's number, it won't find him/her in the database. please check this out if you can find any errors. thanks


lang Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstdlib>
  4. #include <string.h>
  5. using namespace std;
  6.  
  7. struct tstudent{ // main file
  8. int nr_entry;
  9. int student_number;
  10. char name[35];
  11. int year;
  12. };
  13.  
  14. struct tindex{ // index file
  15. int student_number;
  16. };
  17. fstream dat,ind; // file objects
  18.  
  19. void input(char *character_array) // function to avoid newline
  20. {
  21. cin.getline(character_array,150);
  22. if(cin.gcount()==1)
  23. cin.getline(character_array,150);
  24. }
  25.  
  26.  
  27. void manual_input(){
  28. tstudent layer;
  29. tindex index;
  30. int counter;
  31. // 1. number of file entries
  32. if(!dat){
  33. counter=0;
  34. dat.open("student.dat",ios::out|ios::binary);
  35. }
  36. else{ // file exists
  37. dat.seekg(0,ios::end);
  38. counter=dat.tellg()/sizeof(tstudent);
  39. dat.close();
  40. dat.open("student.dat",ios::out|ios::app|ios::binary);
  41. ind.open("student.ind",ios::out|ios::app|ios::binary);
  42. }
  43. // 2. adding a new entry into the file
  44. counter++;
  45. cout << "Entry number " << counter << endl;
  46. layer.nr_entry=counter;
  47. cout << "Student's number:"; cin >> layer.student_number;
  48. cout << "First and last name:";input(layer.name);
  49. cout << "Year of college:";cin >> layer.year;
  50. dat.write((char *)&layer,sizeof(tstudent));
  51. index.student_number=layer.student_number;
  52.  
  53. ind.write((char *)&index,sizeof(tindex));
  54. dat.close();
  55. ind.close();
  56. };//manual_input() END
  57.  
  58. void output_entries(){
  59. tstudent layer;
  60. tindex index;
  61. dat.open ("student.dat",ios::in|ios::binary);
  62. ind.open ("student.ind",ios::in|ios::binary);
  63. while (1){
  64. ind.read ((char *)&index,sizeof(tindex));
  65. if (ind.eof())break;
  66. dat.seekg(index.student_number);
  67. dat.read ((char *)&layer, sizeof(tstudent));
  68. cout << "Entry number " << layer.nr_entry << endl;
  69. cout << "Student's number:" <<layer.student_number << endl;
  70. cout << "First and last name:"<<layer.name<<endl;
  71. cout << "Year of college:"<<layer.year<<endl;
  72. }//while
  73. dat.close();
  74. ind.close();
  75. };//output_entries() END
  76.  
  77.  
  78. void entry_search(int nr){
  79. tstudent layer;
  80. tindex index;
  81. bool found=false;
  82. int counter=0;
  83. dat.open ("student.dat",ios::in|ios::binary);
  84. ind.open ("student.ind",ios::in|ios::binary);
  85. while (1){
  86. ind.read ((char *)&index, sizeof(tindex));
  87. counter++;
  88. if (ind.eof())break;
  89. if (index.student_number==nr){
  90. dat.seekg(index.student_number);
  91. dat.read ((char *)&layer, sizeof(tstudent));
  92. found=true;
  93. cout << "Entry number: " << layer.nr_entry << endl;
  94. cout << "Student's number:"<<layer.student_number << endl;
  95. cout << "First and last name:"<<layer.name<<endl;
  96. cout << "Year of college:"<<layer.year<<endl;
  97. break;
  98. }//if
  99. }//while
  100. dat.close();
  101. ind.close();
  102. if (!found) cout << "The entry wasn't found!" << endl;
  103. cout <<"Number of entries read:"<<counter<<endl;
  104. };
  105.  
  106. int main(){
  107. int choice;
  108. int nr;
  109. do{cout << endl;
  110. cout << "1. adding a new entry into main and index (manually)" << endl;
  111. cout << "2. print all the entered data" << endl;
  112. cout << "3. Search the entries" << endl;
  113. cout << "4. Exit the program" << endl;
  114. cin >> choice;
  115. switch (choice){
  116. case 1:manual_input();break;
  117. case 2:output_entries();break;
  118. case 3:cout << "Student's number:"; cin >> nr;
  119. entry_search(nr);break;
  120. case 4: exit(0);
  121. }//switch
  122. } while (choice!=9);
  123. return 1;
  124. }

Last edited by n00b; Nov 13th, 2006 at 12:31 PM. Reason: spelling mistakes
n00b is offline   Reply With Quote
Old Nov 13th, 2006, 11:13 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 894
Rep Power: 4 The Dark is on a distinguished road
You are writing the student number to the index file, then using that to seek in the data file. As the student number is typed in manually, it is highly unlikely that it contains the offset of the student info in the data file.
When you seek into the data file, you need to use the number of entries that you have read in the index file. This is stored in your variable counter, except you seem to be incrementing it before you use it.
The Dark is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
converting string to float beginnerCCC C 22 Oct 3rd, 2006 12:59 AM
OnlineTextEditor.Com! Sane Show Off Your Open Source Projects 43 Jun 16th, 2006 9:55 AM
Problem with file and array Marek C 9 Dec 28th, 2005 11:03 AM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 10:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 5:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:44 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC