change content of a file

Hi all
I need to change some part of a file in c++.(Borland c++).
I explain what I need here:


#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>

int main () {
FILE * pFile;
long lSize;
short int * outbuffer;
size_t result;

short int buffer[5] = { 1 , 20 , 55 ,3,36 };
pFile = fopen ( "c:/myfile.txt" , "w+t" );
fwrite (buffer , sizeof(short int) , sizeof(buffer) , pFile );
fclose (pFile);
pFile = fopen ( "c:/myfile.txt" , "r+t" );
if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
outbuffer = (short int *) malloc (sizeof(short int )*lSize);
if (outbuffer == NULL) {fputs ("Memory error",stderr); exit (2);}
result = fread (outbuffer,1,lSize,pFile);
if (result != lSize) {fputs ("Reading error",stderr); exit (3);}
fclose (pFile);
free (outbuffer);
cin.get();
return 0;
}


the above code create a file that contains these numbers : 1 , 20 , 55 ,3,36
now I want to change 20 to 35 .
How can I do it ?


Ku, how do you think posting the same topic multiple times in a single forum is going to help you? If anything, it is more likely going to get you ignored.

In any case, I suggest you do not use Borland; it is very old, and thus has little to no support for the newer standards of C/C++.

On your problem: one way that comes to mind is to read the file into memory, find the "20" and re-write the file with a "35" instead. This is only one method and there are almost certainly better ones.
I have to use Borland. also the file is so big I cannot read it to memory.

Then only read a piece of it to memory, write that piece, then read another, etc, until you run out of file.
Topic archived. No new replies allowed.