C 中的临时文件
我正在编写一个输出文件的程序。该文件有两部分内容。然而,第二部分是在第一部分之前计算的。我正在考虑创建一个临时文件,并将数据写入其中。然后创建一个永久文件,然后将临时文件内容转储到永久文件中并删除该文件。我看到一些帖子说这不起作用,并且可能会在不同的编译器之间产生一些问题或其他问题。
数据是一堆字符
。每 32 个字符必须出现在不同的行上。我可以将它存储在链表或其他东西中,但我不想为此编写链表。
有人有任何建议或替代方法吗?
I am writing a program which outputs a file. This file has two parts of the content. The second part however, is computed before the first. I was thinking of creating a temporary file, writing the data to it. And then creating a permanent file and then dumping the temp file content into the permanent one and deleting that file. I saw some posts that this does not work, and it might produce some problems among different compilers or something.
The data is a bunch of chars
. Every 32 chars have to appear on a different line. I can store it in a linked list or something, but I do not want to have to write a linked list for that.
Does anyone have any suggestions or alternative methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以创建一个临时文件,尽管有些人确实说他们对此有问题,但我个人使用它们没有任何问题。使用平台功能获取临时文件是最好的选择。不要假设您可以在 Windows 上写入 c:\ 等,因为这并不总是可行。不要假设文件名,以防文件已被使用等。没有正确使用临时文件才是导致人们出现问题的原因,而不是临时文件不好。
有什么原因可以让您不能将第二部分保留在内存中,直到准备好第一部分?否则,您能否计算出第一部分所需的大小,并将文件的该部分留空以供稍后填写。这将消除对临时文件的需求。
A temporary file can be created, although some people do say they have problems with this, i personally have used them with no issues. Using the platform functions to obtain a temporary file is the best option. Dont assume you can write to c:\ etc on windows as this isnt always possible. Dont assume a filename incase the file is already used etc. Not using temporary files correctly is what causes people problems, rather than temporary files being bad
Is there any reason you cannot just keep the second part in ram until you are ready for the first? Otherwise, can you work out the size needed for the first part and leave that section of the file blank to come back to fill in later on. This would eliminate the needs of the temporary file.
您提出的两种解决方案都可以工作。您可以将中间结果输出到临时文件,然后将该文件附加到包含您要首先呈现的数据集的文件。您还可以将中间数据存储在内存中。正确的数据结构取决于您想要如何组织数据。
正如其他回答者之一指出的那样,文件本质上是特定于平台的。如果您的代码仅在单个平台上运行,那么这就不用担心了。如果您需要支持多个平台,那么如果您使用临时文件解决方案,则可能需要对其中部分或全部平台进行特殊处理。这对您来说是否是一个大问题取决于与在内存中构建和存储数据相比,这会增加多少复杂性。
Both solutions you propose could work. You can output intermediate results to a temporary file, and then later append that file to the file that contains the dataset that you want to present first. You could also store your intermediate data in memory. The right data structure depends on how you want to organize the data.
As one of the other answerers notes, files are inherently platform specific. If your code will only run on a single platform, then this is less of a concern. If you need to support multiple platforms, then you may need to special case some or all of those platforms, if you go with the temporary file solution. Whether this is a deal-breaker for you depends on how much complexity this adds compared to structuring and storing your data in memory.