阅读文本文件导入到线C中的数组 [英] Reading text file into an array of lines in C

查看:154
本文介绍了阅读文本文件导入到线C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用C我想在一个文本文件的内容,以这样一种方式,当一切都说过和做过一个字符串数组的第n个字符串重新presenting文本文件的第n行有阅读。该文件的行可以任意长。

Using C I would like to read in the contents of a text file in such a way as to have when all is said and done an array of strings with the nth string representing the nth line of the text file. The lines of the file can be arbitrarily long.

什么是实现这一点的一种优雅的方式?我知道一些巧妙的技巧,直接读取文本文件到一个适当大小的缓冲区,但它分解成线条使得它棘手(至少据我可以告诉)。

What's an elegant way of accomplishing this? I know of some neat tricks to read a text file directly into a single appropriately sized buffer, but breaking it down into lines makes it trickier (at least as far as I can tell).

非常感谢!

推荐答案

它分解成线是指把文本和0更换所有的EOL(由EOL我的意思是\\ n和\\ r)的字符。
这样你实际上可以重复使用您的缓冲区并在每行的开始存放到一个单独的char *数组(全部由只做2次)。

Breaking it down into lines means parsing the text and replacing all the EOL (by EOL I mean \n and \r) characters with 0. In this way you can actually reuse your buffer and store just the beginning of each line into a separate char * array (all by doing only 2 passes).

在这种方式,你可以为整个文件的大小+ 2解析这可能会提高性能做一读。

In this way you could do one read for the whole file size+2 parses which probably would improve performance.

这篇关于阅读文本文件导入到线C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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