如何优化fgets文件读取时间 - C编程 [英] How to optimize fgets file read time - C programming

查看:279
本文介绍了如何优化fgets文件读取时间 - C编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,





我正在使用以下C编程读取记事本文件(包含矩阵值数据)



声明





fgets(cpReadString,6144,fStream)





但由于''6144'的计算,它需要三个



秒到读。我想把时间减少到零秒,有人可以帮我解决这个问题吗



来解决这个问题。





提前致谢。

Hello,


I am reading a notepad file(contains matrix value datas) using following C programming

statement


fgets( cpReadString, 6144, fStream )


but due to ''6144'' count, it is taking three

seconds to read. I want to reduce the time to zero seconds, can somebody help me out

to solve this issue.


Thanks in advance.

推荐答案

fgets()读取明智的字符串从文件直到它读取新的行字符,达到缓冲区大小,或在文件的末尾。但是即使缓冲区大小为6144字节且行数太长,也不应该花费三秒钟。也许时间消耗在代码的其他地方。



为了便于阅读,您可以将完整的文件内容读入已分配的缓冲区并解析此内容以获取数据: br />
fgets() reads char wise from the file until it reads a new line character, the buffer size is reached, or at the end of the file. But even with a buffer size of 6144 bytes and a line that is so long this should not take three seconds. Maybe the time is consumed elsewhere in your code.

To improbe reading, you may read the complete file content into an allocated buffer and parse this to get your data:
long lFileSize;
char *pBuf;
// Get length of file
lFileSize = _filelength(_fileno(fStream));
// Allocate buffer, add one byte for terminating NULL char
pBuf = (char *)malloc(lFileSize + 1);
// Read data from file into buffer
fread(pBuf, 1, lFileSize, fStream);
// Append terminating NULL char
pBuf[lFileSize] = '\0';
// Parse buffer here
//
// Free allocated memory
free(pBuf);


这篇关于如何优化fgets文件读取时间 - C编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆