473,671 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem in modifying data in a binary file

15 New Member
hello everybody...
i am trying to make a project on binary files and that needs a function to modify data in a binary file. I have made the following class.

Expand|Select|Wrap|Line Numbers
  1. class example
  2. {
  3.    public:   
  4.  
  5.    char name[10], number[5];
  6.    void getdetails();
  7.    void showdetails();
  8.  
  9.    example()
  10.    {
  11.       name[0]='\0', number[0]='\0';
  12.    }
  13.  
  14.    char* getname()
  15.    {
  16.       return name;
  17.    }
  18.  
  19. };
  20.  
and the definition of mod_data() ( a function to modify data of the class ) is as follows:

Expand|Select|Wrap|Line Numbers
  1.  
  2. void mod_data(example &abc)
  3. {
  4.    char name[10], number;
  5.  
  6.    cout<<"NAME:  ";
  7.    gets(name);
  8.    if(strlen(name)!=0)
  9.      strcpy(abc.name, name);
  10.  
  11.    cout<<"NUMBER:  ";
  12.    gets(number);
  13.    if(strlen(number)!=0)
  14.      strcpy(abc.number, number);
  15. }
  16.  
to modify the record in the binary file, function modify is used

Expand|Select|Wrap|Line Numbers
  1.  
  2. void modify()
  3. {
  4.    int recc = 0;
  5.    int flag=0;
  6.    char name[25];
  7.    example abc;
  8.    clrscr();
  9.    fstream fil ("EXAMPLE.dat",ios::in);   
  10.    cout<<"\n  Enter The Name Of The Person Whose Details To Be Modified:  ";
  11.    gets(name);
  12.    cout<<endl;
  13.  
  14.    while (!fil.eof())
  15.    { 
  16.       fil.read ((char*)&abc,sizeof(abc));
  17.       recc++;
  18.       if (stricmp(abc.getname(), name)==0)
  19.       {
  20.           flag=1;
  21.           break;
  22.       }
  23.    }
  24.  
  25.    fil.close();
  26.    if(flag==1)
  27.    {
  28.    fil.open ("EXAMPLE.dat",ios::out|ios::ate);
  29.    mod_data(abc);
  30.    fil.seekp((recc-1)*sizeof(abc), ios::beg);
  31.    fil.write((char*)&abc,sizeof(abc));
  32.    fil.close();
  33.    }
  34.  
  35.     else
  36.     {
  37.      cout<<"\n  PERSON NOT FOUND";
  38.      getch();
  39.     }
  40. }
  41.  
but the program is not working properly.
in my original class there are more than 15 data members. And what i want to do is if the user presses enter in a particular field the record for that field which was present before remains as it is i.e. that field is not modified. But the function works properly for only the first few data members and not for the last ones.The last data members when displayed appear blank or garbage data is displayed. Please help me out.
Oct 27 '07 #1
3 3391
weaknessforcats
9,208 Recognized Expert Moderator Expert
I suspect the format of the data on the disc does not match your sizeof calculations. Often, the compiler aligns data on integer or double boundaries.

Rather than read/write data using sizeof, I recommend you write a Serialize() method that writes each data member to disc indivually in a pattern you can depend upon. Later, you read the data back using the same pattern you used for writing.

You say the real class has 15 members an if any of them are other than char, then you could have this problem.
Oct 27 '07 #2
tracethepath
15 New Member
I suspect the format of the data on the disc does not match your sizeof calculations. Often, the compiler aligns data on integer or double boundaries.

Rather than read/write data using sizeof, I recommend you write a Serialize() method that writes each data member to disc indivually in a pattern you can depend upon. Later, you read the data back using the same pattern you used for writing.

You say the real class has 15 members an if any of them are other than char, then you could have this problem.
what is this " Serialise()"??
i have not read this yet...
can you explain a bit further?
how to call it n do read and write operations using it?

and all my class members are of char type...
by the way my problem is solved now...i made a tempory file and went on writing the data until the data to be modified occured modified it and copied rest of the data to the temp file...and then removed original file and renamed the temp...
Oct 28 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You need to define the format of your disc records.

Your class has a name array of 15 and a number array of 5. plus 13 other members.

I suggest using a flat file approach where each record has exactly the same layout.

Each record has 15 bytes for the name followed by 20 bytes for the number.

When you write, you write exactly 125 bytes for all names and 20 bytes for all numbers. Then you read back in using this exact format.

This process is called serialization.

You could use a script approach where everythign is written as text. In this case you write 15 characters for the name and then convert the number into a string of char. That string would need to hold the max value 9999 to -9999.

On the read, you convert the chars back into an int.

The advantage here is that you can edit your file with a text editor and that makes it easier to get data from other programs.
Oct 28 '07 #4

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

Similar topics

5
6554
by: Michael G. Schneider | last post by:
I know that using Word Automation inside an ASP page is no good idea. Anything I want to do in the current project is: open document, change some text, save and close document. Basically changing some variables, consting of a name embraced by special chars, to some value. As for example: change "" to "Michael". Does anybody know whether there is a way for achieving this with basic "file input / output". Can I regard a Word document as...
6
519
by: | last post by:
I am rewriting a C++ application in C#. This file has a combination of Text and Binary data. I used CFile before to read the text. If I hit a certain string that denotes the following data is binary, I used the current position in the file and another stream to read to the binary data. All text data is ended with a carriage return / line feed while the binary is actually an image file listed byte by byte. Preceding the binary data...
7
6152
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h> int main(void) { char ch;
11
6623
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
3
2122
by: John R. Delaney | last post by:
I am running in debugging mode after a clean C++ compilation under .NET 2003. In a BIG loop (controlled many levels up in the call stack), I open a file with fopen using the "a" option. Then I write 23 doubles to it with fwrite, one call for each double. Then I close the file using fclose. After three times around the loop in the debugger, I stop the program (using "Stop debugging"). That is writing 552 bytes. The resulting file's properties...
39
3081
by: Marcin Zmyslowski | last post by:
Hello all! I have the following problem with MS Access 2003 permissions. I have two users. One is admin and the second one is user who has full permissions to enter modify and read data. I have one table and one queries which is based on this table. This query is good working for these two users.
7
3430
by: sturlamolden | last post by:
This question has been asked many times, and last time I was accused of spreading FUD. So now I will rather propose a solution. The reason for the problem is as follows: The binary installer for Python built by te python.org team is compiled with Microsoft Visual Studio 2003. It is linked with the C runtime msvcrt71.dll. The copyright to msvcrt71.dll is owned by Microsoft, and it is not an integral part of the Windows operating system....
15
4137
by: =?ISO-8859-1?Q?J=F8rn?= Dahl-Stamnes | last post by:
Hello folks, I need some help/advice FAST. I have problems with addslashes on my web-servers. After uploading a file, I read the uploaded file, use addslashes on the read data and then insert it into a blob field in a MySQL database. The problem is that this works fine on my internal test web-server (running under RedHat 7.3). But on my production web-server (running Fedora Core 4)
24
2929
by: allpervasive | last post by:
hi all, this is reddy, a beginner to c lang,,here i have some problems in reading and modifying the contents of a file,, hope you can help to solve this problem. Here i attach the file to be modified and the program code. In the attached file below i just want to change the value of data(only float value) after the line 1 P V T 1 15 till 2 G TT, from positive to negative and vice versa, and wire the date in other file. can someone help...
0
8388
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8593
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8663
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6218
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
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 we have to send another system
2
1799
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.