与fseek相关的字符串长度问题 [英] string length questions related to fseek

查看:62
本文介绍了与fseek相关的字符串长度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




下面的函数返回一个文件的行数。

char变量__Line的长度是256,我认为它会允许我存储最多255个字母的字符串,因为第256个字母总是0。

我的问题这是为什么当fseek尝试

如果__Line的长度为256时从文件读取255个字符时出现分段错误?并且

它最多可以读取254个字符吗?


非常感谢,


Andre


int getNumberLines(const char * _fileName)

{

FILE * __File;

char __Line [256];

int __NumberLines = 0;


if((__ File = fopen(_fileName," r"))== NULL)

返回(-1);


while(fgets(_ _ _ _,254,__ File))

__NumberLines ++;


fclose(__ File);


return(__NumberLines);

}

Hi,

The function bellow returns the number of lines of a file.
The length of the char variable __Line is 256, and I think it would
allow me to store strings with up to 255 letters because the 256th
character would always be "0".
My question is why there is a segmentation fault when fseek tries to
read 255 characters from the file if the length of __Line is 256? And
it works fine when it reads up to 254 characters?

Thanks a lot,

Andre

int getNumberLines( const char * _fileName )
{
FILE * __File;
char __Line[ 256 ];
int __NumberLines = 0;

if( ( __File = fopen( _fileName, "r" ) ) == NULL )
return( -1 );

while( fgets( __Line, 254, __File ) )
__NumberLines++;

fclose ( __File );

return( __NumberLines );
}

推荐答案

si******@yahoo.com (sieg1974)写道:
si******@yahoo.com (sieg1974) writes:
下面的函数返回文件的行数。


....假设所有行都不超过253

个字符。

char变量的长度__Line是256,我认为它允许我存储最多255个字母的字符串,因为第256个字符总是0。


''\0''实际上是的。

我的问题是当fseek试图读取255时出现分段错误的原因如果__Line的长度是256,文件中的字符?
它最多可读取254个字符时工作正常吗?


你的代码没有调用fseek()。

int getNumberLines(const char * _fileName)
{
FILE * __File;
char __Line [256];
int __NumberLines = 0;
The function bellow returns the number of lines of a file.
....assuming that none of the lines are longer than 253
characters.
The length of the char variable __Line is 256, and I think it would
allow me to store strings with up to 255 letters because the 256th
character would always be "0".
''\0'' actually but yes.
My question is why there is a segmentation fault when fseek tries to
read 255 characters from the file if the length of __Line is 256? And
it works fine when it reads up to 254 characters?
Your code doesn''t make any calls to fseek().
int getNumberLines( const char * _fileName )
{
FILE * __File;
char __Line[ 256 ];
int __NumberLines = 0;




以_开头的标识符主要是保留的,而你

不应该使用它们。以__开头的标识符肯定是

保留,你绝对不应该使用它们。


其余的代码看起来还不错,虽然样式很奇怪。我不知道
看不到段错的原因。
-

一个称职的C程序员知道如何正确编写C程序,

a C专家知道与Dan Pop争论,而C专家

专家知道不要打扰。



Identifiers beginning with _ are mostly reserved, and you
shouldn''t use them. Identifiers beginning with __ are definitely
reserved and you should definitely not use them.

The rest of the code looks okay, although the style is odd. I
don''t see cause for a segfault.
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.


sieg1974写道:
sieg1974 wrote:


下面的函数返回一个文件的行数。
char变量__Line的长度是256 ,我认为这将允许我存储最多255个字母的字符串,因为第256个字符总是0。
我的问题是为什么当出现分段错误时如果__Line的长度是256,fseek会尝试从文件中读取255个字符?并且
它最多可以读取254个字符吗?

非常感谢,



int getNumberLines(const char * _fileName)
{
FILE * __File;
char __Line [256];
int __NumberLines = 0;

if((__ File = fopen( _fileName," r"))== NULL)
return(-1);

while(fgets(__Line,254,__ File))
if(__Line [strlen] (__Line)-1] ==''\ n'')__NumberLines ++;

fclose(__ File);

return(__NumberLines);
}
Hi,

The function bellow returns the number of lines of a file.
The length of the char variable __Line is 256, and I think it would
allow me to store strings with up to 255 letters because the 256th
character would always be "0".
My question is why there is a segmentation fault when fseek tries to
read 255 characters from the file if the length of __Line is 256? And
it works fine when it reads up to 254 characters?

Thanks a lot,

Andre

int getNumberLines( const char * _fileName )
{
FILE * __File;
char __Line[ 256 ];
int __NumberLines = 0;

if( ( __File = fopen( _fileName, "r" ) ) == NULL )
return( -1 );

while( fgets( __Line, 254, __File ) ) if (__Line[strlen(__Line)-1] == ''\n'') __NumberLines++;

fclose ( __File );

return( __NumberLines );
}




我在你的代码中插入一行来处理长度超过254个字符的行。


但是,为什么要使用fgets()? getc()更容易。


见下文:

int getNumberLines(const char * fileName)

{

FILE * fp;

int c;


int numlines = 0;


if( (fp = fopen(fileName," r"))== NULL)

返回-1;


while((c = getc(fp) )!= EOF)

++ numlines;

fclose(fp);


返回numlines;

}



I inserted a line in your code to handle lines longer than 254 characters.

However, why use fgets()? getc() is easier.

See below:
int getNumberLines(const char *fileName)
{
FILE *fp;
int c;

int numlines = 0;

if ((fp = fopen(fileName,"r")) == NULL)
return -1;

while ((c = getc(fp)) != EOF)
++numlines;
fclose(fp);

return numlines;
}


red floyd< no ***** @ here.dude>写道:
red floyd <no*****@here.dude> writes:
while((c = getc(fp))!= EOF)
++ numlines;
while ((c = getc(fp)) != EOF)
++numlines;




这是计算字符数量的一种有效方法,但是很难用b $ b计算行数。

-

Ben Pfaff
电子邮件: bl*@cs.stanford.edu

web: http://benpfaff.org



This is an effective way to count characters, but a poor way to
count lines.
--
Ben Pfaff
email: bl*@cs.stanford.edu
web: http://benpfaff.org


这篇关于与fseek相关的字符串长度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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