ofstream:seekp failed

W

wobudui

Hi everyboday, I have some trouble in dealing with the file stream.
My souce code Listed hear:
int main()
{
char buffer[10]={0};
ofstream ofile.open("mydata.in",ios::app);
ofile.seekp(10);
ofile.write(buffer,sizeof(buffer));
ofile.close();
}

My purpose is to write 10 zeros in the file start at position 10. Of
course ,
mydata.in is a file which size is more than 10 charecters.But the it
seem that the seekp don't work.

All comments will be welcomed. Thanks.
 
O

Owen

(e-mail address removed)
Hi everyboday, I have some trouble in dealing with the file stream.
My souce code Listed hear:
int main()
{
char buffer[10]={0};
ofstream ofile.open("mydata.in",ios::app);

ios::app means opening an output file for "APPENDING".
so your code should be just like that:

ofstream ofile;
ofile.open("mydata.in")
....
 
W

wobudui

Thanks for your suggestion. But use ofile.open("mydata.in") cause
mydata.in trunct to zero.
 
W

wobudui

Thanks for your suggestion. But use ofile.open("mydata.in") cause
mydata.in trunct to zero. I want to overwrite a random byte in a file using FILE without rewriting the whole file.
 
J

Jeffrey Baker

Hi everyboday, I have some trouble in dealing with the file stream.
My souce code Listed hear:
int main()
{
char buffer[10]={0};
ofstream ofile.open("mydata.in",ios::app);
ofile.seekp(10);
ofile.write(buffer,sizeof(buffer));
ofile.close();
}

My purpose is to write 10 zeros in the file start at position 10. Of
course ,
mydata.in is a file which size is more than 10 charecters.But the it
seem that the seekp don't work.

All comments will be welcomed. Thanks.

Syntax wise, ofstream ofile.open("xxxxxxx, xx::xx); needs
to be:
ofstream ofile;
ofile.open("xxxxxxx, xx::xx);

ofile.open("mydata.in",ios::app); needs to be ios::eek:ut;
append will put it at the first position.

Here is a piece of code:

//iostream

#include<fstream>

using std::eek:fstream;

using std::ios;

int main()

{

char buffer[20]={0};

ofstream ofile;

ofile.open("mydata.in", ios::eek:ut);

ofile.seekp(10);

ofile << "X ";

ofile.write(buffer,sizeof(buffer));

ofile.close();

return 0;

Regards,
Jeff
 
W

wobudui

thx. my English is very bad.


finally , I find out the problem.
I want to put some char to a exist file.
the way like next is not work.
int main()
{

char buffer[20]={0};

ofstream ofile;

ofile.open("mydata.in", ios::eek:ut | ios::binary);


ofile.write(buffer,sizeof(buffer));

ofile.close();

ofile.open("mydata.in", ios::eek:ut | ios::binary);

ofile.seekp(10);

char * X="ab";
ofile.write(X,sizeof(X));

ofile.close();

return 0;

}

the out file is: 00 00 00 00 00 00 00 00 00 00 61 62 00 00
it is 14 byte!!!!

but I use next way ,it work

int main()
{

char buffer[20]={0};

fstream ofile; //use fstream

ofile.open("mydata.in",ios::in | ios::eek:ut | ios::binary); //add ios::in


ofile.write(buffer,sizeof(buffer));

ofile.close();

ofile.open("mydata.in",ios::in | ios::eek:ut | ios::binary);

ofile.seekp(10);

char * X="ab";
ofile.write(X,sizeof(X));

ofile.close();

return 0;

}

the out file is: 00 00 00 00 00 00 00 00 00 00 61 62 00 00 00 00
00 00 00 00

20 byte ,that is I want.

but I don't know why?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top