w*s 发帖数: 7227 | 1 i typed a lot in chinese but it's gone, :(
anyway, say i have a file has 3 lines,
1: i
2: love
3: coding
now i want to modify the 2nd line,
1: i
2: don't love
3: coding
i can use fgets to read each line, then use fputs to write all 3 lines back.
but it's a waste, can i just write the 2nd line pack ?
the issue is the length of 2nd line get changed.
any way to make it better ?
thanks so much ! | w*s 发帖数: 7227 | 2 certainly i can make each line fixed length | l*********s 发帖数: 5409 | 3 lseek / fputs
【在 w*s 的大作中提到】 : certainly i can make each line fixed length
| w*s 发帖数: 7227 | 4 not getting it yet,
the offset of lseek is number of bytes, not by line number,
so i need to keep calculating myself ?
e.g., in the above example, if i remove the 2nd line, how can lseek help ?
many thanks !
【在 l*********s 的大作中提到】 : lseek / fputs
| t****t 发帖数: 6806 | 5 no, you can not "insert" or "delete" from file. no filesystem provide such
ability. if you want to insert or delete, the following parts must be re-
written.
back.
【在 w*s 的大作中提到】 : i typed a lot in chinese but it's gone, :( : anyway, say i have a file has 3 lines, : 1: i : 2: love : 3: coding : now i want to modify the 2nd line, : 1: i : 2: don't love : 3: coding : i can use fgets to read each line, then use fputs to write all 3 lines back.
| w*s 发帖数: 7227 | 6 how's database handle this ?
if i delete an record, i need to re-write the rest part, that's horrible, :(
【在 t****t 的大作中提到】 : no, you can not "insert" or "delete" from file. no filesystem provide such : ability. if you want to insert or delete, the following parts must be re- : written. : : back.
| t****t 发帖数: 6806 | 7 database has different data structure. filesystem, on the other hand, is
linear, linear as in std::vector. you want to insert one? rewrite the rest.
you want to delete one? rewrite the rest.
【在 w*s 的大作中提到】 : how's database handle this ? : if i delete an record, i need to re-write the rest part, that's horrible, :(
| h**********c 发帖数: 4120 | 8 theoretically, you may play with the inodes in Linux.
Never tried.
You just have three lines, no bigger than one inode. | w*s 发帖数: 7227 | 9 make sense, thx a lot !
.
【在 t****t 的大作中提到】 : database has different data structure. filesystem, on the other hand, is : linear, linear as in std::vector. you want to insert one? rewrite the rest. : you want to delete one? rewrite the rest.
| w*s 发帖数: 7227 | 10 well, this is just sample case, in real case, it's like updating/adding/
deleting registry files, very dynamic.
【在 h**********c 的大作中提到】 : theoretically, you may play with the inodes in Linux. : Never tried. : You just have three lines, no bigger than one inode.
| | | v*****r 发帖数: 1119 | 11 some non-standard GNU library like getline offers line-oriented io function,
but for simple thing as read a line, you still need to take care of buffer
for each line etc, why bother? Just use right tools, awk or perl.
back.
【在 w*s 的大作中提到】 : i typed a lot in chinese but it's gone, :( : anyway, say i have a file has 3 lines, : 1: i : 2: love : 3: coding : now i want to modify the 2nd line, : 1: i : 2: don't love : 3: coding : i can use fgets to read each line, then use fputs to write all 3 lines back.
| b***i 发帖数: 3043 | 12 你需要多少行?需要多快?如果要求不多,这样重写也不算什么。
【在 w*s 的大作中提到】 : well, this is just sample case, in real case, it's like updating/adding/ : deleting registry files, very dynamic.
| w*s 发帖数: 7227 | 13 heavily multithread app,
tens of thousands of updates could happen each second ...
【在 b***i 的大作中提到】 : 你需要多少行?需要多快?如果要求不多,这样重写也不算什么。
| b***i 发帖数: 3043 | 14 先得明确你的需求,假定你需要上千万条的记录。你需要处理随机读写的文件,而不是
顺序文本。
第一个十内容文件可以定为64byte一个单元,每个单元最后4个字节是指针,指向其他
模块单元,你一共可以有4G 个模块。另一个目录文件就存每个行在内容文件的起始单
元比如
0F645343,总长,
93434344,总长,
这个是假定你有过很多存储和删除,所以最开始的可能不是0了。你可以做个标记,比
如内容文件那个指针如果是0,表示没有后续文字了。如果是FFFFFFFF表示该单元被删
除了(释放),可以以后给其他新增的用。
这样,你可以快速更新每一模块。
对你想的任何需求,办法是有的。我这就举个例子。你的需求有任何具体的区别,都可
以经过改动,实现。关键是要明确你的需求。
【在 w*s 的大作中提到】 : heavily multithread app, : tens of thousands of updates could happen each second ...
| p***o 发帖数: 1252 | 15 Get a database. Don't reinvent inferior wheels ...
【在 w*s 的大作中提到】 : heavily multithread app, : tens of thousands of updates could happen each second ...
|
|