如何在 CSV 文件顶部插入数据?
如何返回 csv 文件的开头并添加行?
(我使用 fprintf() 从 C 打印到 CSV 文件。在打印数千行(5 列)数据结束时,我想返回到文件顶部并插入一些动态标题数据(基于事情如何打印一切))
谢谢。
How can I go back to the very beginning of a csv file and add rows?
(I'm printing to a CSV file from C using fprintf(). At the end of printing thousands of rows (5 columns) of data, I would like to go back to the top of the file and insert some dynamic header data (based on how things went printing everything). )
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于文件的结构方式,这或多或少是不可能的。为了完成您想要的任务:
或者您可以将 csv 数据保存在 ram 中,并在完成处理并知道标头后将其写入文件。
另一种选择是为标头留出一定数量的字节,这将以最小的空间成本更快地处理大文件。由于空间是在写入开始时在文件中分配的,因此返回并填充它不会出现任何问题。以随机访问(“r+”)方式重新打开文件,默认情况下指向文件的顶部,写入标题,然后关闭。
Due to the way files are structured, this is more or less impossible. In order to accomplish what you want:
Or you can hold the csv data in ram and write it to file after you're finished processing and know the header.
Another option is to set aside a certain number of bytes for the header, which will work much faster for large files at minimal space cost. Since the space is allocated in the file at the start of the write, there aren't any issues going back and filling it in. Reopen the file as random access ("r+"), which points to the top of the file by default, write header, and close.
最简单的方法是将文件的全部内容存储在内存中,直到完成为止,写出标题,然后写出文件的其余部分。
如果内存是一个问题,并且您无法将整个文件安全地存储在内存中,或者只是不想这样做,那么您可以将大量 CSV 数据写入临时文件,然后在完成后写入header out到主文件中,循环将临时文件中的数据复制到主文件中。
如果您想要更奇特,在将主要 CSV 数据写入主文件后,您可以从头开始循环遍历该文件,将要使用标头覆盖的数据读入内存,然后将标头写入数据的顶部,依此类推,将每个块读入内存,用前一个块覆盖它,直到到达末尾并附加最后一个块。通过这种方式,您可以在开头“插入”数据,而将文件的其余部分向下移动。我真的不推荐这样做,因为它只会增加复杂性而没有太多好处,除非有特定原因你不能做一些更简单的事情,比如使用临时文件。
The simplest way would be to simply store the entire contents of the file in memory until you are finished, write out the header, and then write out the rest of the file.
If memory is an issue and you can't safely store the entire file in memory, or just don't want to, then you could write out the bulk of the CSV data to a temporary file, then when you are finished, write the header out to the primary file, and copy the data from the temporary file to the primary file in a loop.
If you wanted to be fancy, after writing the main CSV data out to the primary file, you could loop through the file from the beginning, read into memory the data that you're about to overwrite with the header, then write the header over top of that data, and so forth, read each chunk into memory, overwrite it with the previous one until you reach the end and append the final chunk. In this way you "insert" data at the beginning, my moving the rest of the file down. I really wouldn't recommend this as it will mostly just add complexity without much benefit, unless there is a specific reason you can't do something simpler like using a temporary file.
我认为这是不可能的。最简单的方法可能是将输出写入临时文件,然后创建所需的数据作为动态标头,将它们写入目标文件并附加之前创建的临时文件。
I think that is not possible. Probably the easiest way would be to write the output to a temporary file, then create the data you need as the dynamic header, write them to the target file and append the previously created temporary file.