从文件中读取一行 [英] Reading a line from a file

查看:57
本文介绍了从文件中读取一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




相当新的C.从文件中读取一行(\\\
终止)的最佳方法是什么?我看过fscanf,但不确定

使用哪种格式说明符。 (也许是%s)。


谢谢

科林

解决方案

科林JN Breame< c。********** @ durham.ac.uk>潦草地写道:


相当新的C.从文件中读取一行(\\\
终止)的最佳方法是什么?我看过fscanf,但不确定
使用哪种格式说明符。 (也许是%s)。




如果你知道这条线的长度,那么fgets()就好了

这份工作比fscanf()。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)---- ---------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

战争!咦!天哪,你们呀!到底有什么好处呢?我们询问了Quimby市长。

- Kent Brockman


On Sun,2003年12月7日18:44:51 +0000,Joona I Palaste写道:

Colin JN Breame< c。********** @ durham.ac.uk>潦草地写道:


C的相当新的东西。读取一行的最佳方法是什么(\\\
终止)
来自档案?我看过fscanf,但不确定使用哪种格式
说明符。 (也许是%s)。



如果你知道这条线的长度,那么对于
工作而言,fgets()要比fscanf()好得多。



这行是可变长度的。


Colin JN Breame写道:

在Sun, 2003年12月7日18:44:51 +0000,Joona I Palaste写道:

Colin JN Breame< c。********** @ durham.ac.uk>潦草地写了下面的内容:


C的相当新的东西。读取线路的最佳方法是什么(\\ \\ n终止)
从一个文件?我看过fscanf,但不确定使用哪种格式
说明符。 (也许是%s)。



如果你知道这条线的长度,那么对于
工作而言,fgets()要比fscanf()好得多。



该行是可变长度。




C中的数组不是可变长度。 (也就是说,他们不会自动改变大小。有一个名为可变长度阵列的C99功能,但是他们实际上也没有改变长度。)因此,你有一些

选项:


1)创建一个足够大的数组。为你最长的一行。

2)创建一个动态数组并尽可能多地读取该行的数据。

该数组。如果这就是整条线,你就完成了。如果它不是

整行,则重新分配数组以使其更大并阅读更多。冲洗并

重复,直到读完整行(或者你的内存耗尽,

先到先得)。


就个人而言,我建议使用2,并将功能包装在

a单独的功能中:


char * line = my_getline(some_file);


/ *在这里用线做* /


免费(线);


-Kevin

-

我的电子邮件地址有效,但会定期更改。

要联系我,请使用最近发布的地址。


Hi,

Fairly new to C. What is the best way to read a line (\n terminated) from
a file? Ive looked at fscanf but was not sure which format specifier to
use. (%s perhaps).

Thanks
Colin

解决方案

Colin JN Breame <c.**********@durham.ac.uk> scribbled the following:

Hi, Fairly new to C. What is the best way to read a line (\n terminated) from
a file? Ive looked at fscanf but was not sure which format specifier to
use. (%s perhaps).



If you know how long the line will be, then fgets() is much better for
the job than fscanf().

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"War! Huh! Good God, y''all! What is it good for? We asked Mayor Quimby."
- Kent Brockman


On Sun, 07 Dec 2003 18:44:51 +0000, Joona I Palaste wrote:

Colin JN Breame <c.**********@durham.ac.uk> scribbled the following:

Hi,


Fairly new to C. What is the best way to read a line (\n terminated)
from a file? Ive looked at fscanf but was not sure which format
specifier to use. (%s perhaps).



If you know how long the line will be, then fgets() is much better for the
job than fscanf().


The line is variable length.


Colin JN Breame wrote:

On Sun, 07 Dec 2003 18:44:51 +0000, Joona I Palaste wrote:

Colin JN Breame <c.**********@durham.ac.uk> scribbled the following:

Hi,


Fairly new to C. What is the best way to read a line (\n terminated)
from a file? Ive looked at fscanf but was not sure which format
specifier to use. (%s perhaps).



If you know how long the line will be, then fgets() is much better for the
job than fscanf().



The line is variable length.



Arrays in C are not variable length. (That is, they won''t automatically
change size. There is a C99 feature called Variable Length Arrays, but
they never actually change length either.) Therefore, you have a few
options:

1) Create an array that''s "large enough" for your longest line.
2) Create a dynamic array and read as much of the line as you can into
that array. If that''s the whole line, you are done. If it''s not the
whole line, realloc the array to make it larger and read more. Rinse and
repeat until the entire line is read (or you run out of memory,
whichever comes first).

Personally, I''d recommend using 2, and wrapping the functionality up in
a separate function:

char *line = my_getline(some_file);

/* do stuff with line here */

free(line);

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.


这篇关于从文件中读取一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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