wow...thanks Kennedy, :)
i will try coding that and will come back if i find some doubts.....
Printable View
wow...thanks Kennedy, :)
i will try coding that and will come back if i find some doubts.....
Hi kennedy,
when i dunno the position(where the string is located in the file) of the string how can i fseek ??? :( and moreover to point to the position of the substring strstr() makes a local copy into buffer !! and also the pointer strstr() returns is not of type FILE* instead it returns char *... so i cant use strstr() if i want to directly write into the file without making another copy....what to do now ????
thanks...
You have to find it first. This means you have to read the file into memory, all, or part of it at a time. Your best bet is to load the whole thing into memory, modify it, write your changes. If it's too big for this, then it's not smart to be trying to rewrite the whole thing at once without using a different file anyway.
Quzah.
As stated previously, you should generally only be trying to do this if the replacement string is the same length as the search string. If that's the case:
1) open the file in binary mode
2) use fread() within a loop, to attempt to read in the same number of characters each time, into a buffer.
3) Search the buffer (careful with '\0')
4) If you find the pattern, you can deduce file position from number of times the loop has run + pos in buffer
5) fseek()
6) fwrite()
7) fflush()
EDIT: 2 Quick notes:
make sure to check return values
chose buffer size carefully, because if you split your phrase on read, you can never match it (unless you always store the last few characters from previous read)
Thanks for the replies....
i didnt get .. sorry... :( how to read the file into memory.... what it means...Quote:
Originally Posted by quzah
what about the maximum limit u are telling.... say for 15K which one u prefer.. and for less than 10K which is best..... ????Quote:
Originally Posted by quzah
Quote:
Originally Posted by compile
buffer is locked in the memory, so you read the portion of the file into memoryCode:unsigned char buffer[BUFF_SIZE];
fread(buffer,1,sizeof(buffer),f);
if file is small enough to allocate one buffer that can store the whole file - it is small.
I say 64K is small on most platforms
300-400K is still small on Win32
file bigger than 500K I'll read in chunks on any platform...
Of course you can implement both ways
Code:size_t len = getfileSize
unsigned char* buffer = malloc(len+1);
if(buffer)
{
//go easy way - read the whole file
}
else
{
//go hard way reading file in chunk
}
Thanks vart, :) i wil do further....