简体   繁体   English

用C ++覆盖CSV文件中的一行

[英]Overwriting a line in CSV file with C++

I'm writing a payroll program in c++ and need to be able to read lines in a file, do calculations, and then overwrite the read lines in the file. 我正在用C ++写工资程序,需要能够读取文件中的行,进行计算,然后覆盖文件中的读取行。 IS there a function/way i can simply overwrite specific lines, insert new lines, add onto the end of an existing file? 有没有可以简单地覆盖特定行,插入新行,添加到现有文件末尾的功能/方式?

There are no C++ functionality to "insert" or "remove" text in a text-file. 没有C ++功能可以在文本文件中“插入”或“删除”文本。 The only way to do that is to read the existing text in, and write out the modified text. 唯一的方法是读入现有文本,然后写出修改后的文本。

If the new text fits in the same space as the old one, all you need to do is to overwrite the existing text - and of course, you can always add extra spaces before/after a comma in a .CSV file, without it becoming part of the "field". 如果新文本与旧文本位于相同的空间,那么您要做的就是覆盖现有文本-当然,您始终可以在.CSV文件中的逗号前后添加额外的空格,而不会成为逗号“字段”的一部分。 But if the new data is longer, it definitely won't work to "overwrite in place". 但是,如果新数据更长,则“原地覆盖”绝对不起作用。

Adding to the end is relatively easy by using the ios_base::ate modifier. 通过使用ios_base::ate修饰符,添加到末尾相对容易。 But inserting in middle still involves basically reading until you find the relevant place, and then, if the new text is longer, you have to read all the following lines before you can write the new one(s) out. 但是,插入中间仍然要进行基本的阅读,直到找到相关的位置为止,然后,如果新文本较长,则必须先阅读以下所有行,然后才能写出新文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM