-1

How would I remove a specific line from a file in C++?

Please assume that reading the entire file into memory, modifying and writing back out is not an option, because the file is too big to fit into memory. Is there a way to do this 'in place'? Or is changing the size of the file not possible?

Shaun Budhram
  • 3,690
  • 4
  • 30
  • 41
  • see [How to truncate a file from the end?](http://stackoverflow.com/q/9727287/33499), but you have to manually read and overwrite all data after the line you want to remove. – wimh Feb 22 '15 at 09:19
  • Agree with @Wimmel, unless you want to remove the last line from the file. You will have no real option but to read the file and find the line you want to remove then re-write the file. THere may be something you can do this more memory efficiently in the boost library. – Jamie Reid Feb 22 '15 at 10:22
  • There is no current file-system that allows files to be modified "in place" - nor one that I know of that understands "lines" (the filesystem itself just see files as streams of bytes or blocks of bytes). So one way or another, you need to read the entire file, and write back all but the one line you want to remove. You can do this in portions if you really need to (e.g. a file bigger than fits in memory), but it's still "read the whole thing, write back most of it" - and if you're unlucky, MOST of the file needs to be read, if it's line 3 of 1000000000 lines... – Mats Petersson Feb 22 '15 at 10:27

1 Answers1

0

You can use hdf5 format/library. transform your data to hdf5 and read chunks of data using hdf5 library for c
HDF5 Page

Alireza Soori
  • 645
  • 9
  • 25