473,320 Members | 1,876 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,320 software developers and data experts.

Modifying data in file

void modifyData()
{
int Id;
char nm[25];
float pay;


fstream modData("Department1.txt",ios::out || ios::in || ios::binary);
if(!modData)
{
cerr<<"file could not be opened"<<"\n";
}
else
{
cout<<" Enter the ID of department you want to modify :"<<"\n";
cin>>Id;
cout<<"Enter Name of Department:";
cin>>nm;
cout<<"\n";
cout<<"Enter Salary of the Department's Employee :";
cin>>pay;
cout<<"\n";

Department d;

modData.seekg((Id-1)*sizeof(Department));

modData.read((char*)(&d),sizeof(Department));

d.setPayrate(pay);
d.setName(nm);
d.setId(Id);

modData.seekp((Id-1)*sizeof(Department));

modData.write((char*)(&d),sizeof(Department));
}


}

this is the code to modify data in file but its not working well can any1 help me? please
Nov 26 '08 #1
5 1823
Expand|Select|Wrap|Line Numbers
  1. fstream modData("Department1.txt",ios::out || ios::in || ios::binary);
  2.  
you need to use the bitwise inclusive or-operator ( | ) not the relational or-operator( || )

---
amonsch
Nov 26 '08 #2
ooohhhhhhhh thank u soooo sooo much ive been trying from last 2 hours n was unable to debug...thanks a lot
Nov 26 '08 #3
can any1 please guide how can we delete a record from a file
Nov 26 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
That depends upon what you mean by delete.

If delete means the record is gone and the space it occupied is also gone, then you will need to rewrite the file.

If delete means the record is invisible to the user of your read/write functions, then you can fill the deleted record's are with "delete characters". That is, an arrangement of data that means "no data here". The downside here is that you will need to run a reformat utility (which you write) that will copy the file to a temporary location omitting the "delete characters" and then copying the temporary file back to the original file.

A variation on this is keeping the file in memory, applying the "delete characters" as necessary and then when the file is saved to disc, the save function does not copy the "delete characters" to disc.
Nov 26 '08 #5
void delData()
{
int id;

fstream delData("Department1.txt",ios::out | ios::in|ios::binary);
if(!delData)
{
cerr<<"file could not be opened"<<"\n";
}
else
{
cout<<" Enter the ID of department you want to modify :"<<"\n";
cin>>id;

Department d;

delData.seekg((id-1)*sizeof(Department));

delData.read((char*)(&d),sizeof(Department));


delData.seekg(0,ios::end);

int size=delData.tellg()/sizeof(Department);


int iid=d.getID();
if(iid!=0)
{
Department d1;
delData.seekp((id-1)*sizeof(Department));

delData.write((char*)(&d1),sizeof(Department));

size--;

}
else
cout<<" Id "<<id<<" is already empty "<<"\n";


Department td[10];

for(int i=0 ; i<=size ; i++)
{
int iD=1;
delData.seekg(0);
td[i].setId(iD);
int gid=td[i].getID();
if(gid>0)
{
delData.read((char*)(&td[i]),sizeof(Department));
iD++;
}
else
continue;
}
}
}



still its not working....
it is still printing like
1
2
4
5
Nov 26 '08 #6

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

Similar topics

33
by: Jason Heyes | last post by:
I would like to modify the contents of a file, replacing all occurances of one string with another. I wrote these functions: bool read_file(std::string name, std::string &s); bool...
0
by: Steven T. Hatton | last post by:
I tried to create my own manipulator that would both set the width of the subsequent output field, and cast an unsigned char to unsigned in. I came up with the following rather ugly hack. Notice...
7
by: Együd Csaba | last post by:
Hi, I've a problem with some of my stored procs. My config is: RH7.1, Postgres 7.3.2 I had converted a few fields of a few tables from one type to another and after this I made all the...
1
by: Max Khitrov | last post by:
Hello everyone, I'm working on a VS .NET add-in that will allow developers to use Subversion software from within the IDE (much like Source Safe). Ideally, I would like for my plug-in to be able...
2
by: Yavuz Bogazci | last post by:
Hi, i am reading the LastAccessTime, LastWriteTime and DateofCreation of a file. The second and third are working well, but each time i want to read the LastAccessTime, its set to "now". How can...
6
by: MackS | last post by:
Hello everyone I am faced with the following problem. For the first time I've asked myself "might this actually be easier to code in C rather than in python?", and I am not looking at device...
13
by: Robin Becker | last post by:
When young I was warned repeatedly by more knowledgeable folk that self modifying code was dangerous. Is the following idiom dangerous or unpythonic? def func(a): global func, data data =...
24
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...
3
by: tracethepath | last post by:
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. class example { public: ...
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

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.