有关fgets和新行的问题 [英] Question regarding fgets and new lines
问题描述
我需要在逗号分隔的文件中读取,为此我将使用fgets来使用
。我正在 http://www.cplusplus.com/ref/和
我注意到文件说:
"从流中读取字符并将它们存储在字符串中直到(num -1)
已读取
个字符或已达到换行符或EOF字符,
以先到者为准。
我的问题是,如果它停止在一个新行字符(LF?)那么
如何读取一个包含多个新行字符的文件?
另一个问题。语法是:
char * fgets(char * string,int num,FILE * stream);
但你必须分配一个此前字符串的大小。你会只用
使用fgets中使用的相同数量吗?所以char stringexample [num]?
I need to read in a comma separated file, and for this I was going to
use fgets. I was reading about it at http://www.cplusplus.com/ref/ and
I noticed that the document said:
"Reads characters from stream and stores them in string until (num -1)
characters have been read or a newline or EOF character is reached,
whichever comes first."
My question is that if it stops at a new line character (LF?) then how
does one read a file with multiple new line characters?
Another question. The syntax is:
char * fgets (char * string , int num , FILE * stream);
but you have to allot a size for the string before this. Would you just
use the same num as used in the fgets? So char stringexample[num] ?
推荐答案
me ********** @ yahoo.ca 写道:
我需要用逗号阅读分开的文件,为此我打算使用fgets来支付
。我正在 http://www.cplusplus.com/ref/和
我注意到文件说:
"从流中读取字符并将它们存储在字符串中直到(num -1)
已读取
个字符或已达到换行符或EOF字符,
以先到者为准。
我的问题是,如果它停止在一个新的行字符(LF?)然后如何读取一个包含多个新行字符的文件?b
$ b
I need to read in a comma separated file, and for this I was going to
use fgets. I was reading about it at http://www.cplusplus.com/ref/ and
I noticed that the document said:
"Reads characters from stream and stores them in string until (num -1)
characters have been read or a newline or EOF character is reached,
whichever comes first."
My question is that if it stops at a new line character (LF?) then how
does one read a file with multiple new line characters?
你多次调用它,直到你读完整个文件。
You call it multiple times, until you''ve read the entire file.
另一个问题。语法是:
char * fgets(char * string,int num,FILE * stream);
但你必须分配一个此前字符串的大小。你会只用
使用fgets中使用的相同数量吗?所以char stringexample [num]?
Another question. The syntax is:
char * fgets (char * string , int num , FILE * stream);
but you have to allot a size for the string before this. Would you just
use the same num as used in the fgets? So char stringexample[num] ?
是的。你基本上是在告诉fgets:好吧,我已经设置了这么大的空间
一边让你读书,给我那么多角色(减去1为
NUL终结者)或第一行,以先到者为准。
-
Clark S. Cox III
cl ******* @ gmail.com
我********** @ yahoo.ca 写道:
我需要在逗号分隔的文件中读取,为此我要使用fgets来使用
。
I need to read in a comma separated file, and for this I was going to
use fgets.
你可能最好一次解析一个这样的文件。
You may be better off parsing such files one character at a time.
我正在阅读它在 http://www.cplusplus.com/ref/ 和
我注意到文件说:
"从流中读取字符并将它们存储在字符串中直到(num -1)
已经读过字符或者到达换行符或者EOF字符,
以先到者为准。
我的问题是如果它停在一个新行字符(LF?)然后如何读取带有多个换行符的文件
?
I was reading about it at http://www.cplusplus.com/ref/ and
I noticed that the document said:
"Reads characters from stream and stores them in string until (num -1)
characters have been read or a newline or EOF character is reached,
whichever comes first."
My question is that if it stops at a new line character (LF?) then how
does one read a file with multiple new line characters?
通过对fgets()进行多次调用。
问题就像Excel这样的情况允许在
个人
字段记录。这些字段是带引号,带有双引号
(&);和
嵌入式双引号作为两个双引号转义。因此,我的
评论说你可能最好用一个简单的状态机解析
一次一个字符。
By making multiple calls to fgets().
The problem though is cases like Excel which allow newlines in
individual
field records. Such fields are ''quoted'' with a leading double quote
("), and
an embedded double quote is escaped as two double quotes. Hence my
comment that you may be better off with a simple state machine parsing
one character at a time.
另一个问题。语法是:
char * fgets(char * string,int num,FILE * stream);
但你必须分配一个此前字符串的大小。你会只用
使用fgets中使用的相同数量吗?所以char stringexample [num]?
Another question. The syntax is:
char * fgets (char * string , int num , FILE * stream);
but you have to allot a size for the string before this. Would you just
use the same num as used in the fgets? So char stringexample[num] ?
是的。样品使用是......
char line [256];
while(fgets(line,sizeof line,stdin))
$
/ * ... * /
}
虽然更严肃的程序会推出自己的fgets()那个
动态
为一条线分配存储空间,而不是固定
缓冲区的大小。
[这样的程序仍然需要注意那些将通过stdin抽出一个大的免费二进制文件的白痴。]
-
彼得
Yes. Sample use is...
char line[256];
while (fgets(line, sizeof line, stdin))
{
/* ... */
}
Though more serious programs will roll their own fgets() that
dynamically
allocates storage for a line, rather than fixing the size of the
buffer.
[Such programs still need to be mindful of the idiots that will pump a
large \n free binary file through stdin.]
--
Peter
Peter Nilsson写道:
Peter Nilsson wrote:
您最好一次解析一个字符这样的文件。
You may be better off parsing such files one character at a time.
我想可能使用fgetc?
I guess maybe using fgetc?
这篇关于有关fgets和新行的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!