我用这段代码实时打开一个录音文件,用fseek()和ftell()获取文件大小:
typedef struct _Recording Recording;
struct _Recording
{
FILE *file;
long filesize;
int progress_id;
...
};
void start_recording(Recording *recording, char* filepath)
{
...
recording->file = fopen(filepath, "rb");
recording->progress_id =
g_timeout_add (500, (GSourceFunc) progress_timeout, recording);
}
gboolean progress_timeout (Recording *recording)
{
if (recording->file != NULL)
{
fseek(recording->file, 0, SEEK_END);
recording->filesize = ftell(recording->file);
}
return TRUE;
}
void stop_recording(Recording *recording)
{
...
if (recording->file)
{
fclose (recording->file);
recording->file = NULL;
}
if (recording->progress_id != 0)
{
g_source_remove (recording->progress_id);
recording->progress_id = 0;
}
}
我在一个循环中使用这个函数(500 毫秒)。需要帮助以更快地优化功能。
代码的效率。
更新了循环函数
最佳答案
如果您不需要“最大”(questioned)兼容性,那么 use operating system specific 是有意义的功能类似于 fstat
。打开文件总是有开销,甚至会一直寻找结束 - 操作系统会尝试预测您正在做什么,并可能开始将文件内容缓存到内存中 - 在这种情况下是不必要的。
关于c - 帮助优化函数循环获取文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35851792/