-2

Is there a way to replace a string within a file with another word in C? I realized that the only way (maybe) possible is to rewrite the first file on a temp file with the appropriate changes. The problem is that by doing this I am forced to do both a reading and a writing of the same file (it is not so optimized). Is there a way to make string changes without creating a new file? And if yes, how could i do it?

FLxDany
  • 11
  • 4
  • 2
    You can only do it directly if the strings are the same size. There's no way to insert or delete directly in a file. – Barmar Feb 10 '18 at 08:23
  • 1
    'by doing this I am forced to do both a reading and a writing of the same file' no. You read the input file, write the output file, delete the input file, rename the output file. – Martin James Feb 10 '18 at 08:42

2 Answers2

3

If you replace a string aa by some string bbb of different byte length (not the same as the UTF-8 character length, see utf8everywhere) you need to have a temporary file, so you temporarily need twice the disk space. BTW, you might need to specify what exactly a word is for you (think of how words are ending, or are separated; what about combining characters?).

If both original and replacement strings have the same byte length (e.g. aa replaced by cc but not by çà), you could do the replacement in place.

If the file size is small (less than a few gigabytes), you could afford reading it entirely in heap memory (read about C dynamic memory allocation, use malloc and free), remove the original file, and write (e.g. with fwrite or fprintf) the new one from memory.

On current laptops or desktops, most files are small enough to fit in memory (e.g. because they are smaller than a gigabyte), but not all. You could have a file of a terabyte, on some large enough disk (provided your disk partition and file system allows that).

Read more about C stdio input output functions. There is no way to insert or remove bytes "in the middle of" or "inside" a file, only at its end.

If the file is textual (not binary), you might read it line by line (using getline if you have it, or else carefully using fgets and handling line overflow appropriately) and process every line in a loop. You could then assume that each line fits in memory (e.g. is less than a gigabyte).

If you are required to be able to process a huge file containing a single terabyte line, you need to think more about how to do that (e.g. read about finite state machines and/or do your processing in chunks).

If such replacements are a common operation that you want to do efficiently, you might think of some different (and more "efficient") way of keeping your data on the disk, perhaps using indexed files à la gdbm, or databases à la sqlite.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Yes but you need to include the string header...eg

#include <iostream>
#include <string>

int main()
{
  string proverb {"A nod is as good as a wink to a blind horse"};
  string sentence {"It's bath time!"};    // lol
  proverb.replace(38, 5, sentence, 5, 3);
 ......etc

this replaces horse with bat or

proverb.replace(0, 43, sentence, 0, 15);

to replace the string...play around with it theirs probably other ways

poorgoat
  • 37
  • 6