简体   繁体   English

从打开的文件中读取

[英]Read from an open file

I'm working on a project that encrypts data at rest, then the encrypted data is decrypted into a temporary file in order to write the unencrypted data to an XML stream. 我正在研究一个对静态数据进行加密的项目,然后将加密的数据解密为一个临时文件,以便将未加密的数据写入XML流。 The encryption and decryption work, but I am having trouble reading the data from the file. 加密和解密工作正常,但是我无法从文件中读取数据。 I believe this is due to the file still being open, but I can't close the file since it gets deleted on close. 我认为这是由于文件仍处于打开状态,但是由于关闭时文件被删除,因此我无法关闭文件。 I'm wondering if there's a way to read from this file? 我想知道是否有一种方法可以读取此文件?

As a last resort I could rewrite the code to just use a large encrypted buffer instead of a file, but I'd like to figure out how to read the data from the open file. 作为最后的选择,我可以重写代码以仅使用大的加密缓冲区而不是文件,但是我想弄清楚如何从打开的文件中读取数据。

EDIT: I should've said earlier that I have the decryption logic in a C++ class and that I port the functions I need to C with extern "C". 编辑:我应该早先说过,我在C ++类中具有解密逻辑,并且我将需要的函数移植到外部“ C”中。 The function doing the decryption is in C++, which allowed me to get a HANDLE from the file descriptor and then use FlushFileBuffers(HANDLE) to flush the buffer. 进行解密的函数在C ++中,这使我可以从文件描述符中获取HANDLE,然后使用FlushFileBuffers(HANDLE)刷新缓冲区。

Input and output can be buffered both at the C library level and at the kernel level; 输入和输出可以在C库级别和内核级别进行缓冲。 writes from one process are not necessarily immediately visible to another process until the buffer has been flushed. 在刷新缓冲区之前,一个进程的写操作不一定立即对另一进程可见。 If you're using the C library's standard IO features, you can use fflush in the writer process to make sure its output is available to the reader process. 如果使用的是C库的标准IO功能,则可以在fflush器进程中使用fflush ,以确保其输出可用于读取器进程。

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

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