How To Edit File

Hi Everyone,

Well,I have a dat file which following format;
ID Level(Manager or Executive)
Name
Sales(for 12months)

For example;
W00051 M
Lee Ah Moi
1200.00 150.00 1400.20 156.00 200.00 880.00 1500.00 8000.00 800.00 120.00 1600.00 1800.00

My question is how am i going to find the value 880.00 (August) & replace it with a new value?
*Need some hints*

Izit possible for me to open & edit the same file without creating s new one?

Thx in advance!
Since the file is saved in a text format, it is very annoying to try and 'edit' it. Instead you should read in the entire file to data structures, close the file, and then write over it again, clearing what used to be there to replace it with the changed data. If it were binary format you could simply change the relevant bytes, but I think that is outside the scope of your assignment, right?
+1
the file is in dat format.

any hints to my question?

It doesn't matter when nid to create another new file or not.

Currently,I'm stucked at how am i going to replace dat value.

izit to use. seekp?
eastw wrote:
the file is in dat format.
The file extension tells you nothing about the actual format of the file.
eastw wrote:
any hints to my question?
L B wrote:
Since the file is saved in a text format, it is very annoying to try and 'edit' it. Instead you should read in the entire file to data structures, close the file, and then write over it again, clearing what used to be there to replace it with the changed data.
I assume that you would use C++ and std::library

1
2
3
4
5
6
7
typedef struct level
{
std::string name;
std::vector<float> sales_list;
}levelnode;

std::map<int /*id*/, levelnode*>

1
2
3
4
5
6
7
8
//C equivalent
typedef struct level
{
int id;
char name[255];
float *sales_list[12];
struct level* nextnode;   // make a linked list ? :D
}levelnode;


keep reading your .dat file (which is a text file) and make one record of the map, insert in the map. Read in the full file into the map. close the file.

Now whatever you need to change, take the input from user, lets say id = 10 and sales month = 5 (200.00); Change it to new values; keep doing this until the user asks to close the program;

truncate the file, write the whole map/linked list to the file and exit.

Can you write some code like this ?
Last edited on
Topic archived. No new replies allowed.