Is this the most efficient way to get and remove first line in file?

前端 未结 12 2250
天命终不由人
天命终不由人 2020-12-03 03:53

I have a script which, each time is called, gets the first line of a file. Each line is known to be exactly of the same length (32 alphanumeric chars) and terminates with \"

12条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 04:07

    You could store positional info into the file itself. For example, the first 8 bytes of the file could store an integer. This integer is the byte offset of the first real line in the file.

    So, you never delete lines anymore. Instead, deleting a line means altering the start position. fseek() to it and then read lines as normal.

    The file will grow big eventually. You could periodically clean up the orphaned lines to reduce the file size.

    But seriously, just use a database and don't do stuff like this.

提交回复
热议问题