Re: Replacing line in a file
From: Joe Estock (jestock_at_nutextonline.com)
Date: 11/17/03
- Next message: Tanguy Fautré: "Re: Compiler warning when casting from a pointer to an integer"
- Previous message: Tanguy Fautré: "Re: stack overflow"
- In reply to: Chad: "Replacing line in a file"
- Next in thread: Thomas Matthews: "Re: Replacing line in a file"
- Reply: Thomas Matthews: "Re: Replacing line in a file"
- Reply: Chris Theis: "Re: Replacing line in a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Tanguy Fautré: "Re: Compiler warning when casting from a pointer to an integer"
- Previous message: Tanguy Fautré: "Re: stack overflow"
- In reply to: Chad: "Replacing line in a file"
- Next in thread: Thomas Matthews: "Re: Replacing line in a file"
- Reply: Thomas Matthews: "Re: Replacing line in a file"
- Reply: Chris Theis: "Re: Replacing line in a file"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|