0

I want to replace each character in file with another one by one "walk" through the file.

It is like using fgetc(), but I also need to replace that char. My file is big, which is the best way to do this? Any advice?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hurrem
  • 193
  • 2
  • 4
  • 15
  • Create a temporary file. Write replaced characters to a memory buffer, write said buffer to the temp file periodically. When you're done, remove the old file and rename the new one. – DCoder Jan 28 '13 at 13:52
  • the main reason is that I don\t want to you use anything like: find and replace, I don't want to search the document... because, anyways I have to replace every character in file, why should I search? If I will search, then I wil have to "walk" through the document for every letter I'm searching, that's gonna be n times loop, where n is the number of elements of alphabet... So, I'm looking for way to solve this problem by one "walk", only one.. is it possible? – Hurrem Jan 28 '13 at 20:05

2 Answers2

0

If you're a Linux user look into the use of sed. There are alternatives for windows that allow you to do it as well but you'll need to download them (they're not default)

It works like this: say you have a sentence "Hello World". Run the command:

sed s/l/p/g sentence.txt

You get the output: "Heppo Worpd" (replacing all l's with p's).

Grambot
  • 4,370
  • 5
  • 28
  • 43
0

fopen mode c+ should do what you want. See http://www.php.net/manual/en/function.fopen.php

Adder
  • 5,708
  • 1
  • 28
  • 56