File I/O (Reading from a Random-Access File)

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 17
Reputation: VersEtreOuNe is an unknown quantity at this point 
Solved Threads: 0
VersEtreOuNe VersEtreOuNe is offline Offline
Newbie Poster

File I/O (Reading from a Random-Access File)

 
0
  #1
Feb 10th, 2008
What I need to do i something like this:

ClientData.h
  1. #ifndef CLIENTDATA_H
  2. #define CLIENTDATA_H
  3.  
  4. #include <string>
  5. using std::string;
  6.  
  7. class ClientData
  8. {
  9. public:
  10. ClientData(int = 0, string = "", string = "", double = 0.0);
  11.  
  12. void SetAccountNumber(int);
  13. int GetAccountNumber() const;
  14.  
  15. void SetLastName(string);
  16. string GetLastName() const;
  17.  
  18. void SetFirstName (string);
  19. string GetFirstName() const;
  20.  
  21. void SetBalance (double);
  22. double GetBalance() const;
  23.  
  24. private:
  25. int _accountNumber;
  26. char _lastName[15];
  27. char _firstName[10];
  28. double _balance;
  29. };
  30.  
  31. #endif

ClientData.cpp
  1. #include <string>
  2. using std::string;
  3.  
  4. #include "ClientData.h"
  5.  
  6. ClientData::ClientData(int accountNumberValue, string lastNameValue, string firstNameValue, double balanceValue)
  7. {
  8. SetAccountNumber(accountNumberValue);
  9. SetLastName(lastNameValue);
  10. SetFirstName(firstNameValue);
  11. SetBalance(balanceValue);
  12. }
  13.  
  14. int ClientData::GetAccountNumber() const
  15. {
  16. return _accountNumber;
  17. }
  18.  
  19. void ClientData::SetAccountNumber(int accountNumberValue)
  20. {
  21. _accountNumber = accountNumberValue;
  22. }
  23.  
  24.  
  25. string ClientData::GetLastName() const
  26. {
  27. return _lastName;
  28. }
  29.  
  30. void ClientData::SetLastName(string lastNameString)
  31. {
  32. // copy at most 15 characters from string to lastName
  33. const char* lastNameValue = lastNameString.data();
  34.  
  35. int length = lastNameString.size();
  36.  
  37. length = (length < 15 ? length : 14);
  38.  
  39. strncpy_s(_lastName, lastNameValue, length);
  40.  
  41. _lastName[length] = '\0';
  42. }
  43.  
  44. string ClientData::GetFirstName() const
  45. {
  46. return _firstName;
  47. }
  48.  
  49. void ClientData::SetFirstName(string firstNameString)
  50. {
  51. const char* firstNameValue = firstNameString.data();
  52.  
  53. int length = firstNameString.size();
  54.  
  55. length = (length < 10 ? length : 9);
  56.  
  57. strncpy_s(_firstName, firstNameValue, length);
  58.  
  59. _firstName[length] = '\0'; // append null character to firstName
  60. }
  61.  
  62. double ClientData::GetBalance() const
  63. {
  64. return _balance;
  65. }
  66.  
  67. void ClientData::SetBalance(double balanceValue)
  68. {
  69. _balance = balanceValue;
  70. }


main.cpp
  1. #include <iostream>
  2. using std::cout;
  3. using std::cin;
  4. using std::cerr;
  5. using std::ios;
  6. using std::endl;
  7. using std::left;
  8. using std::right;
  9. using std::showpoint;
  10. using std::fixed;
  11.  
  12. #include <fstream>
  13. using std::fstream;
  14. using std::ostream;
  15. using std::ifstream;
  16.  
  17. #include <iomanip>
  18. using std::setw;
  19. using std::setprecision;
  20.  
  21. #include "ClientData.h"
  22.  
  23. void OutputLine(ostream&, const ClientData& );
  24.  
  25. int main ()
  26. {
  27. //-----------------------------------------------------------
  28. // Reading from a Random-Access File Sequentially
  29. //-----------------------------------------------------------
  30.  
  31. ifstream inCredit("credit.dat", ios::in);
  32.  
  33. if (!inCredit)
  34. {
  35. cerr << "File could not be opened." << endl;
  36. exit(1);
  37. }
  38.  
  39. cout << left << setw(10) << "Account" << setw(16) << "Last Name" << setw(11) << "First Name" << left << setw(10) << right << "Balance" << endl;
  40.  
  41. ClientData client;
  42.  
  43. inCredit.read(reinterpret_cast<char*> (&client), sizeof(ClientData));
  44.  
  45. while (inCredit && !inCredit.eof())
  46. {
  47. //display record
  48. if (client.GetAccountNumber() != 0)
  49. OutputLine(cout, client);
  50.  
  51. //read next from file
  52. inCredit.read(reinterpret_cast<char*> (&client), sizeof(ClientData));
  53. }
  54.  
  55.  
  56. cin.ignore();
  57. cin.get();
  58. return 0;
  59. }
  60.  
  61. void OutputLine(ostream& output, const ClientData& record)
  62. {
  63. output << left << setw(10) << record.GetAccountNumber()
  64. << setw(16) << record.GetLastName()
  65. << setw(11) << record.GetFirstName()
  66. << setw(10) << setprecision(2) << right << fixed
  67. << showpoint << record.GetBalance() << endl;
  68. }



This program reads records from a "credits.dat" file and prints all the records..

But
What I need to do is search for records and print 1 at a time.
for example i should enter the account number of a record and it should print all the information about that record. Keeping in mind that the archive "credits.dat" has many records...

I've been struggling trying to figure out a way to do this.
Any help would be much appreciated....
Last edited by VersEtreOuNe; Feb 10th, 2008 at 7:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File I/O (Reading from a Random-Access File)

 
0
  #2
Feb 10th, 2008
If you want a random-access file where you can go to any record without reading all the intermediate records then the file must be in binary form with fixed-length records. Your class ClientData makes that pretty easy to implement because of its fixed-length data objects.

You have to open the files in binary mode using ios::binary flag ofstream out("filename",ios:binary); then use the ofstream's write() function to write out one record. So lets say you need to rewrite the 10th record
  1. #include <fstream>
  2. using namespace std;
  3. int main()
  4. {
  5. ClassData obj;
  6. ofstream out("filename", ios::out | ios::binary);
  7. // seek to the 10th record
  8. out.seekp(10 * sizeof(ClassData));
  9. // write the 10th record
  10. out.write(&obj, sizeof(ClassData));
  11. }

Similar code as above to read the 10th record (or any record in the file)
Last edited by Ancient Dragon; Feb 10th, 2008 at 8:20 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,116
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: File I/O (Reading from a Random-Access File)

 
0
  #3
Feb 11th, 2008
Only output the record if client.GetAccountNumber() is the account number entered. Similar to your output while loop.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: VersEtreOuNe is an unknown quantity at this point 
Solved Threads: 0
VersEtreOuNe VersEtreOuNe is offline Offline
Newbie Poster

Re: File I/O (Reading from a Random-Access File)

 
0
  #4
Feb 11th, 2008
I didn't make that other program i used it as an example for this one I'm making:

Circle.h
  1. #ifndef CIRCLE_H
  2. #define CIRCLE_H
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. class Circle
  9. {
  10. friend ostream& operator <<(ostream& output, const Circle& aCircle);
  11. friend istream& operator >>(istream& input, Circle& aCircle);
  12.  
  13. public:
  14. Circle();
  15. Circle(double radius, int id);
  16. Circle(const Circle& aCircle);
  17.  
  18. void SetCircleRadius(double radius) { _circleRadius = radius; }
  19. void SetCircleId(int id) { _circleId = id; }
  20.  
  21. double GetCircleRadius() const { return _circleRadius; }
  22. int GetCircleId() const { return _circleId; }
  23.  
  24. friend void AddNewCircle(fstream&);
  25.  
  26. friend void AccessCircle(fstream&);
  27.  
  28. private:
  29. double _circleRadius;
  30. int _circleId;
  31. };
  32.  
  33. #endif

Circle.cpp
  1. #include "Circle.h"
  2.  
  3. Circle::Circle()
  4. {
  5. _circleId = 0;
  6. _circleRadius = 0.0;
  7. }
  8.  
  9. Circle::Circle(double radius, int id)
  10. {
  11. _circleId = id;
  12. _circleRadius = radius;
  13. }
  14.  
  15. Circle::Circle(const Circle& aCircle)
  16. {
  17. _circleId = aCircle._circleId;
  18. _circleRadius = aCircle._circleRadius;
  19. }
  20.  
  21. ostream& operator <<(ostream& output, const Circle& aCircle)
  22. {
  23. output << aCircle._circleId << endl;
  24. output << aCircle._circleRadius << endl;
  25.  
  26. return (output);
  27. }
  28.  
  29. istream& operator >>(istream& input, Circle& aCircle)
  30. {
  31. int quantity;
  32. cout << "How many circles do you want to add ";
  33. input >> quantity;
  34.  
  35. for (int i = 0; i < quantity; ++i)
  36. {
  37. cout << "\nEnter the ID of Circle #" << (i + 1) << " : ";
  38. input >> aCircle._circleId;
  39.  
  40. cout << "Enter Radius of Circle #" << (i + 1) << " : ";
  41. input >> aCircle._circleRadius;
  42.  
  43. cout << endl;
  44. }
  45.  
  46. return (input);
  47. }
  48.  
  49. void AddNewCircle(fstream& insertInFile)
  50. {
  51. Circle aCircle;
  52.  
  53. cout << "Enter an Id for the circle: ";
  54. cin >> aCircle._circleId;
  55.  
  56. cout << "\nEnter the circle's radius: ";
  57. cin >> aCircle._circleRadius;
  58.  
  59. insertInFile.open("circle.dat", ios::out | ios::binary);
  60.  
  61. insertInFile.write(reinterpret_cast<char*> (&aCircle), sizeof(Circle));
  62. }
  63.  
  64.  
  65. void AccessCircle(fstream& extractFromFile)
  66. {
  67. Circle aCircle;
  68.  
  69. cout << "Enter an Id for the circle: ";
  70. cin >> aCircle._circleId;
  71.  
  72. extractFromFile.open("circle.dat", ios::in | ios::binary);
  73.  
  74. extractFromFile.read(reinterpret_cast<char*> (&aCircle), sizeof(Circle));
  75.  
  76. }

main.cpp
  1. #include <iostream>
  2. #include <fstream>
  3. #include <map>
  4. using namespace std;
  5.  
  6. #include "Circle.h"
  7.  
  8. int main ()
  9. {
  10. Circle aCircle;
  11. fstream file;
  12. int option;
  13.  
  14. file.open("Circle.dat", ios::out|ios::out|ios::binary);
  15.  
  16. if (file.fail())
  17. {
  18. cout << "\n\nError: failed to open file.\n\n";
  19. }
  20. else
  21. {
  22. file.write((char*)& aCircle, sizeof(aCircle));
  23. }
  24.  
  25. do
  26. {
  27. cout << "Menu \n"
  28. << " (1) Add Circle(s) \n"
  29. << " (2) Find a Circle by ID \n"
  30. << " (3) Exit \n"
  31. << "Your Selection -> ";
  32. cin >> option;
  33.  
  34. switch (option)
  35. {
  36. case 1:
  37. AddNewCircle(file);
  38. break;
  39.  
  40.  
  41. case 2:
  42. AccessCircle(file);
  43. cout << "\n\nId radius" << endl;
  44. cout << aCircle.GetCircleId() << " " << aCircle.GetCircleRadius() << "\n\n";
  45. break;
  46.  
  47.  
  48. case 3:
  49. file.close();
  50. break;
  51.  
  52.  
  53. default:
  54. cerr << "\nERROR: Wrong Option menu.\n\n";
  55.  
  56. }
  57.  
  58. } while (option != 3);
  59.  
  60. return EXIT_SUCCESS;
  61. }

The problem is that when i try to find the circles radius from the file by id it always prints 0 for id and 0 for radius...

???
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,116
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: File I/O (Reading from a Random-Access File)

 
0
  #5
Feb 11th, 2008
In AccessCircle you never tested for errors on the open nor read. They are probably failing because you have the file open for writing. You also don't close the file after reading.

All IO needs and OPEN and CLOSE. And always test to make sure the OPEN, CLOSE, READ and WRITE worked.

You should not keep the file open for write all the time. Just open it when you need to write then close it. Be sure to open it for append (ios::app I believe)
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 17
Reputation: VersEtreOuNe is an unknown quantity at this point 
Solved Threads: 0
VersEtreOuNe VersEtreOuNe is offline Offline
Newbie Poster

Re: File I/O (Reading from a Random-Access File)

 
0
  #6
Feb 12th, 2008
I made some modifications, but it still not working properly. Any help, tips would much appreciated !

Circle.h
  1. #ifndef CIRCLE_H
  2. #define CIRCLE_H
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7.  
  8. class Circle
  9. {
  10. friend ostream& operator <<(ostream& output, const Circle& aCircle);
  11. friend istream& operator >>(istream& input, Circle& aCircle);
  12.  
  13. public:
  14. Circle();
  15. Circle(double radius, int id);
  16. Circle(const Circle& aCircle);
  17.  
  18. void SetCircleRadius(double radius) { _circleRadius = radius; }
  19. void SetCircleId(int id) { _circleId = id; }
  20.  
  21. double GetCircleRadius() const { return _circleRadius; }
  22. int GetCircleId() const { return _circleId; }
  23.  
  24. private:
  25. double _circleRadius;
  26. int _circleId;
  27. };
  28.  
  29. #endif

Circle.cpp
  1. #include "Circle.h"
  2.  
  3. Circle::Circle()
  4. {
  5. _circleId = 0;
  6. _circleRadius = 0.0;
  7. }
  8.  
  9. Circle::Circle(double radius, int id)
  10. {
  11. _circleId = id;
  12. _circleRadius = radius;
  13. }
  14.  
  15. Circle::Circle(const Circle& aCircle)
  16. {
  17. _circleId = aCircle._circleId;
  18. _circleRadius = aCircle._circleRadius;
  19. }
  20.  
  21. ostream& operator <<(ostream& output, const Circle& aCircle)
  22. {
  23. output << "\n ------------" << endl;
  24. output << " Id radius" << endl;
  25. output << " ------------" << endl;
  26. output << " " << aCircle._circleId << " " << aCircle._circleRadius << endl;
  27. output << " ------------" << "\n\n";
  28.  
  29. return (output);
  30. }
  31.  
  32. istream& operator >>(istream& input, Circle& aCircle)
  33. {
  34. int quantity;
  35. cout << "\nHow many circles do you want to add ";
  36. input >> quantity;
  37.  
  38. for (int i = 0; i < quantity; ++i)
  39. {
  40. cout << "\n\nEnter the ID of Circle #" << (i + 1) << " : ";
  41. input >> aCircle._circleId;
  42.  
  43. cout << "Enter Radius of Circle #" << (i + 1) << " : ";
  44. input >> aCircle._circleRadius;
  45.  
  46. cout << endl;
  47. }
  48.  
  49. return (input);
  50. }

main.cpp
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. #include "Circle.h"
  7.  
  8. int main ()
  9. {
  10. Circle aCircle;
  11. fstream file;
  12. int option;
  13.  
  14. do
  15. {
  16. cout << "Menu \n"
  17. << " (1) Add Circle(s) \n"
  18. << " (2) Find a Circle by ID \n"
  19. << " (3) Exit \n"
  20. << "Your Selection -> ";
  21. cin >> option;
  22.  
  23. switch (option)
  24. {
  25. case 1:
  26. file.close();
  27. file.clear();
  28.  
  29. file.open("Circle.dat", ios::out | ios::app | ios::binary);
  30.  
  31. if (!file)
  32. {
  33. cerr << "\n\nFailed to open file.\n\n";
  34. system("PAUSE");
  35. exit(1);
  36. }
  37. else
  38. {
  39. cin >> aCircle;
  40. file.write(reinterpret_cast<char*> (&aCircle), sizeof(Circle));
  41. }
  42. break;
  43.  
  44. case 2:
  45. file.close();
  46. file.clear();
  47.  
  48.  
  49. cout << "Enter id: ";
  50. int id;
  51. cin >> id;
  52. aCircle.SetCircleId(id);
  53.  
  54. file.open("Circle.dat", ios::in | ios::app | ios::binary);
  55.  
  56. if (!file)
  57. {
  58. cerr << "\n\nFailed to open file.\n\n";
  59. system("PAUSE");
  60. exit(1);
  61. }
  62. else
  63. {
  64. file.seekg(id * sizeof(Circle), ios::beg);
  65. file.read(reinterpret_cast<char *> (&aCircle), sizeof(Circle));
  66.  
  67. cout << aCircle;
  68. }
  69. break;
  70.  
  71.  
  72. case 3:
  73. file.close();
  74. cout << "\n\nG o o D B y E\n\n";
  75. exit(0);
  76. break;
  77.  
  78.  
  79. default:
  80. cerr << "\nERROR: Wrong Option menu. Please try again.\n\n";
  81.  
  82. }
  83.  
  84. } while (option != 3);
  85.  
  86. return EXIT_SUCCESS;
  87. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,116
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is online now Online
Posting Sensei

Re: File I/O (Reading from a Random-Access File)

 
0
  #7
Feb 12th, 2008
Originally Posted by VersEtreOuNe View Post
I made some modifications, but it still not working properly. Any help, tips would much appreciated !
OK. read the post titled Read Me: Read This Before Posting
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC