writing to a pre existent file

i have a file that i filled with null to size 128MB and than i write at certain offset(otFBL=655424) from its begining numbers from 1 till 129407 as integers(having size of 4 bytes) and checked them it was very right. and then i try to edit this content one time in the same program and another time in a program by it self but only when i try to make an ofstream without even writing any thing igot scratch and the size of the file become 0 bytes.iam using Dev-c++ 4.9.9.2
here is the code of trying in another program:
the read instructions that i made here are just examples to be sure that the file was filled before right and as i filled that file with integers(4bytes) then the offset between a number and the following is 4bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;


int main(int argc, char *argv[])
{const int otFBL=655424;
int q;
ifstream in("VFS99",ios::in | ios::binary);
if(!in){cout<<"cannot open file";}
//ofstream out("VFS99", ios::out | ios::binary);
//if(!out){cout<<"cannot open file ";}
in.seekg(otFBL, ios::beg);
cout<<"here is the get pointer after seekg"<<in.tellg()<<endl;
in.read((char *)&q, sizeof q);
cout<<"transllated into "<<q<<endl;
in.read((char *)&q, sizeof q);
cout<<"transllated into "<<q<<endl;
in.seekg(otFBL+4*129406, ios::beg);
in.read((char *)&q, sizeof q);
cout<<"transllated into "<<q<<endl;
in.close();
 system("PAUSE");
    return EXIT_SUCCESS;
}

here is the output
here is the get pointer after seekg655424
transllated into 1
transllated into 2
transllated into 129407
Press any key to continue . . .



but when i remove the single line comments marks from the ofstream(("VFS99", ios::out | ios::binary)
without even writing what ihave wanted .
here is what i got:
here is the get pointer after seekg655424
transllated into 4072496
transllated into 4072496
transllated into 4072496
Press any key to continue . . .


Last edited on
I'm not sure if it's safe associating two filestreams with the same file at the same time. I would suggest you either call ofstream out("VFS99", ios::out | ios::binary); after in.close or use fstream inout("VFS99", fstream::in | fstream::out); to replace the input filestream and the output filestream if you want to be able to read and write at the same time.
Last edited on
i have tried this before and it didnot work well either
Topic archived. No new replies allowed.