-5

The title pretty much says it all, but still to elaborate:

I would have understood if the language would have restricted me from adding new contents to a file (irrespective of position) because it would lead to fragmentation. But what I do not understand is why it is not possible to:

  1. Erase contents from the last line, similar to backspacing from EOF. [ASCII/BINARY]
  2. Erase contents from middle portion of file [ASCII/BINARY]
  3. Replace text in a file with some other text of same size [ASCII]
  4. Replace data in a file with some other data of same size [Binary]

Does any other language support this?

EDIT: To do this in C++, you need to read the file, perform the modifications on variables, then create a new file. The question was why it is not possible to edit the "original" file instead of creating another file.

László Papp
  • 51,870
  • 39
  • 111
  • 135
Cool_Coder
  • 4,888
  • 16
  • 57
  • 99
  • 7
    you can do all those things. What do you mean by "it is not possible" then? – ShinTakezou Jan 04 '14 at 14:55
  • 1
    1. http://stackoverflow.com/questions/15154263/c-binary-file-truncate-or-resize-in-order-to-modify-its-end, 3 and 4: trivial, 2: doable with a bit of work. Some languages _might_ make 2 easier but I don't know of one. (ASCII/binary has nothing to do with any of this, really.) – Mat Jan 04 '14 at 14:56
  • Were you expecting there to be pre-written or standard library functions to do these tasks for you? – dreamlax Jan 04 '14 at 15:03
  • Re your edit: no you don't need to create a new file. Only 2. requires you to read anything form the file. – Mat Jan 04 '14 at 15:04
  • @ScarletAmaranth c++ is statically-typed. – David G Jan 04 '14 at 15:14
  • @Mat ftrucate() is UNIX stuff. Why there is no equivalent in ? – Cool_Coder Jan 04 '14 at 15:15
  • @0x499602D2 Is that the only thing you see wrong with that comment? – ScarletAmaranth Jan 04 '14 at 15:15
  • No. To be honest it sounds like you're talking about JavaScript. C++ doesn't support prototypal inheritance. – David G Jan 04 '14 at 18:47

2 Answers2

1

Does any other language support this?

C++ supports it. You can seek and you can write. But the fact that this is a very uncommon scenario to replace the same exact byte length and the fact that replacing a bunch of bytes with another bunch of bytes that has a different length is horribly inefficient to a point were writing a new file is faster, lead to the language and functions that exist.

Other languages may handle this differently, but I have yet to learn one that actually does. Because file handling does not change across languages and all language and framework writers came to the same conclusions: files are not the best medium for insertion and deletion of data. Arrays are not the best medium for insertion and deletion, files just being one them.

If you need to insert and delete, use another container (lists come to mind). If you need to persist this container to disk, do so after all manipulation is done.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • The use case is: File is saved with millions of double variables during previous session by user. If he opens the file simply edits a single double variable and saves the file, I have to write millions of variables again in a new file. Instead it would be better to simply replace it, I thought. – Cool_Coder Jan 04 '14 at 15:20
  • @Cool_Coder Ok, so you seek to the place in the file where you want to overwrite a specific double and write the new double to it. Where exactly is the problem? – nvoigt Jan 04 '14 at 15:23
  • I was not aware that this could be done. My bad...:( Using too much of Qt has lead me away from C++ – Cool_Coder Jan 04 '14 at 15:28
1

Looks like you were expecting lowish-level access to files to magically work like some word processor / text editor application. But they don't do this, either! They simply abstract away from the user all the complicated mechanisms involved in editing and re-committing a file to disk.

In writing C++ source code, you are now taking responsibility for implementing those mechanisms. It's not quite as simple as dumping a bunch of backspace characters at EOF. :)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055