Re: Replacing line in a file

From: Joe Estock (jestock_at_nutextonline.com)
Date: 11/17/03


Date: Sun, 16 Nov 2003 19:07:28 -0600

Perhaps the simple way would be to read the file in line by line and compare
it to the string you want to replace it with and if it matches, seek back to
the beginning of the line, write out the string, and if you have no more
occurences of it then close the file, otherwise repeate the same operation.

e.g.,

myfile.txt
  0|0123456
  0|0123457
  0|0123458

file *fp = fopen("myfile.txt", "a+");
char buffer[512];
char replace[] = "myreplacement";

fread(&buffer, fp);

if(!strcmp(buffer, replace))
{
  fseek((strlen(buffer) - strlen(buffer) * 2), SEEK_CUR);
  fwrite(&replace, fp);
}
fclose(fp);

"Chad" <chad@mizmoz.com> wrote in message
news:bb0f9985.0311161650.1e8847b5@posting.google.com...
> Hello, im struggleing,Im trying to replace a line in a file.
>
> 1|010000
> 1|010001
> 1|010002
> 0|010003 <-- replace this with 1|10003
> 0|010004
> 0|010005
> ... all of the strings are the same length.
>
>
> I had been doing it by creating a temp file but it seems a bit of a
> waste of resources. the file is nearly 2mb.
>
> Any help would be greatly appreciated, thanks.



Relevant Pages

  • Re: 32-bit programs on Windows x64
    ... tiny individual sequences. ... to compare its input string against every possible ... compare one string against one string, ...
    (microsoft.public.vc.mfc)
  • Re: 32-bit programs on Windows x64
    ... THE AMOUNT OF RAM ON THE MACHINE IS COMPLETELY, ... nothing in that article that implies any concurrency. ... to compare its input string against every possible valid ...
    (microsoft.public.vc.mfc)
  • Re: 32-bit programs on Windows x64
    ... THE AMOUNT OF RAM ON THE MACHINE IS COMPLETELY, ... nothing in that article that implies any concurrency. ... to compare its input string against every possible valid ...
    (microsoft.public.vc.mfc)
  • Re: 32-bit programs on Windows x64
    ... machine has "parallelism" ... to compare its input string against every possible valid ...
    (microsoft.public.vc.mfc)
  • Re: 32-bit programs on Windows x64
    ... THE AMOUNT OF RAM ON THE MACHINE IS COMPLETELY, ... to compare its input string against every possible valid ... Note that the L2 cache is ...
    (microsoft.public.vc.mfc)