473,382 Members | 1,651 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

how to display the contents of a binary file at an indexed location?

the rror i have is it does not read contents from the employee.dat file


Expand|Select|Wrap|Line Numbers
  1. #ifndef main_h
  2. #define main_h
  3. struct date{
  4. int day;
  5. int month;
  6. int year;
  7. };
  8. class employees
  9. {
  10. int ID;
  11. char name[30];
  12. date hiredate;
  13. int salary;
  14. public:
  15. void insert();
  16. void deleteemp();
  17. void modify();
  18. void show();
  19. void display(employees,int,fstream);
  20. };
  21. #endif main_h
  22.  
  23. #include<iostream.h>
  24. #include<fstream.h>
  25. #include<conio.h>
  26. #include"main.h"
  27.  
  28. void employees::insert()
  29. {
  30. employees emp;
  31. cout<<"enter the details:";
  32. cout<<"ID";
  33. cin>>ID;
  34. cout<<"name:";
  35. cin>>name;
  36. cout<<"salary:";
  37. cin>>salary;
  38. cout<<"hiredate:";
  39. cin>>hiredate.day>>hiredate.month>>hiredate.year;
  40.  
  41. }
  42. void employees::show()
  43. { cout<<"ID:"<<ID<<endl;
  44. cout<<"name:"<<name<<endl;
  45. cout<<"salary:"<<salary<<endl;
  46. cout<<"date hired on:"<<hiredate.day<<"-"<<hiredate.month<<"-"<<hiredate.year<<endl;
  47. }
  48.  
  49.  
  50. void main()
  51. {
  52. employees e;
  53. char ch;
  54. do{
  55. cout<<"enter your choice!\n1.insert new record\n2.display the existing record\n3.delete a record\n4.modify existing\n";
  56. int choice;
  57. cin>>choice;
  58. switch(choice)
  59. {
  60. case 1:
  61. e.insert();
  62. fstream fio;
  63. fio.open("employee.dat",ios::binary|ios::app|ios::out);
  64. fio.write((char*)& e,sizeof(e));
  65. fio.seekg(0,ios::beg);
  66. fio.close();
  67. break;
  68. case 2:
  69. int n;
  70. cout<<"which employee number do you want details for?";
  71. cin>>n;
  72. fio.open("employee.dat",ios::binary|ios::in);
  73. int y=(n-1)*sizeof(e);
  74. fio.seekg(y);
  75. fio.read((char*)& e,sizeof(e));
  76. break;}
  77. cout<<"continue?";
  78.  
  79. cin>>ch;
  80. }while(ch=='y');
  81. getch();
  82. }
  83.  
May 3 '12 #1
3 1822
weaknessforcats
9,208 Expert Mod 8TB
I think you are running afoul of slack bytes. These padding bytes are inserted by the compiler to enforce int boundary alignment for class and struct members.

A sizeof(employees) is 52 bytes but a sizeof each of the data members is 50.

That means you have to read back in 52 bytes and then fiddle with that to find your data. Very ugly.

I recommend that you write out a buffer you have defined as an array. That gives you a fixed size. In this case the buffer is 50 bytes. Write a function that takes an employees& as an argument and inside that function extract the data members from your object and pack them into the buffer. Then write the buffer.

On the readback, write a function that takes an employees& as an argument plus a second argument for the offset. Read into the buffer and then unpack the buffer into the arguent object.
May 3 '12 #2
could you please write an exact code block for the fix that you have offered. i have not learnt much about file handling...so if you could pls help me...thanks a lot for your help
May 3 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
Maybe you can try it this way:
Expand|Select|Wrap|Line Numbers
  1. class employees
  2.   {
  3.  int ID;
  4.  char name[30];
  5.  unsigned  char pad1;      //pad byte
  6.  unsigned  char pad2;      //pad byte
  7.  date hiredate;
  8.   int salary;
  9.   public:
  10.   void insert();
  11.   void deleteemp();
  12.   void modify();
  13.   void show();
  14.   void display(employees,int,fstream);
  15.   };
Since alignment is on an int boundary, ID is on an int boundary since it is the ifrst member. ID is an int so the next member (name) also is on an int boundary. However, since it is only 30 bytes, the next int boundary would be 32 bytes away. So I added 2 pad bytes after the array to force hiredate to begin 2 bytes later. When I did a sizeof(employees) I still got 52 but this time I have defined 52 bytes in the class. That means no pad bytes stuffed in by the compiler. I believe you can now write as you were indending and your data will be where you expect it.

I didn't test this so let me knw what happened.
May 4 '12 #4

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

Similar topics

2
by: Fred the man | last post by:
Hi, I'm a PHP newbie, and am stuck as to why I can't find a pattern in a Win32 binary file. I'm actually trying to extract the FileVersion information myself since PHP under Unix doesn't seem...
5
by: Victor | last post by:
Hi I need to open an html file from the file system and embed that html file within my main aspx form. I have 2 questions: 1) How do I open the html file? I tried the File.open(Path, mode) didnt...
4
by: Jens Mittag | last post by:
Hi! In my code, I have an array of a structure, which I want to save to a binary file. When the array is just created, everything works fine, but when I change contents of the array, saving...
18
by: David Buchan | last post by:
Hi guys, This may be a dumb question; I'm just getting into C language here. I wrote a program to unpack a binary file and write out the contents to a new file as a list of unsigned integers....
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
5
by: Phil Kelly | last post by:
Hi I need to write the contents of a structure to a binary file - there is one string and 2 integers, but I can't seem to figure out how to write the data correctly. If I am simply writing...
8
by: Shalaka Joshi | last post by:
Hi, I have binary file say, "test.bin". I write "FF" in the file and expect my code to read 255 for me. char * lbuf; int lreadBytes ; long lData; FILE *pFile = fopen ("c:\\testbin","rb");
10
by: rory | last post by:
I can't seem to append a string to the end of a binary file. I'm using the following code: fstream outFile("test.exe", ios::in | ios::out | ios::binary | ios::ate | ios::app)...
5
by: zehra.mb | last post by:
Hi, I had written application for storing employee data in binary file and reading those data from binary file and display it in C language. But I face some issue with writing data to binary file....
22
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.