这是完全便携和/或智能? [英] Is this fully portable and/or smart?

查看:41
本文介绍了这是完全便携和/或智能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我处理文本

文件的最后一个字符是换行符的检查:


/ *检查换行是否是文本文件的最后一个字符* /

unsigned check_text_file_newline_termination(FILE * test_file){

int end_char;


fseek(test_file,-1L,SEEK_END);


end_char = getc(test_file);


rewind(test_file);


if(end_char ==''\ n'')返回TRUE;

else返回FALSE;

}


问题是:这实际上是保证在

全部符合的情况下正常工作吗? C实施?我对规范的解读说

没有,但当然它在几个系统上运行得很好我已经用它了... b $ b使用它...... />

此外,这是否是进行此项检查的最快方式,

而不是更符合bformant的方式。阅读方案

文件中的每个字符都可以找到文件的结尾?


---

William Ernest Reid


This is how I handle a check that the last character of a text
file is a newline:

/* checks if newline is last character of text file */
unsigned check_text_file_newline_termination(FILE *test_file) {
int end_char;

fseek(test_file,-1L,SEEK_END);

end_char=getc(test_file);

rewind(test_file);

if(end_char==''\n'') return TRUE;
else return FALSE;
}

The question is: is this actually guaranteed to work properly on
all "conforming" C "implementations"? My reading of the spec says
"no", but of course it works just fine on the several systems I''ve
used it on...

Also, would this be the fastest way possible to make this check,
as opposed to a perhaps more "conformant" scheme of reading
every character in the file to find the end of the file?

---
William Ernest Reid

推荐答案

Bill Reid写道:
Bill Reid wrote:

这就是我处理的方式检查文本的最后一个字符

文件是换行符:


/ *检查换行符是否为文本文件的最后一个字符* /

unsigned check_text_file_newline_termination(FILE * test_file){

int end_char;


fseek(test_file,-1L,SEEK_END);


end_char = getc(test_file);


倒带(test_file);


if(end_char ==' '\ n'')返回TRUE;

否则返回FALSE;

}


问题是:这实际上是保证在

全部合规上正常工作C实施?我对规范的阅读说明了

没有,但当然它在几个系统上工作得很好我已经使用了它...
$ b
This is how I handle a check that the last character of a text
file is a newline:

/* checks if newline is last character of text file */
unsigned check_text_file_newline_termination(FILE *test_file) {
int end_char;

fseek(test_file,-1L,SEEK_END);

end_char=getc(test_file);

rewind(test_file);

if(end_char==''\n'') return TRUE;
else return FALSE;
}

The question is: is this actually guaranteed to work properly on
all "conforming" C "implementations"? My reading of the spec says
"no", but of course it works just fine on the several systems I''ve
used it on...



我在这里看到的唯一不合格的东西是TRUE和FALSE。

stdbool.h将宏定义为''true''到1和''false''到0.

The only non-conforming things that I can see here are TRUE and FALSE.
stdbool.h defines the macros ''true'' to 1 and ''false'' to 0.


>

此外,这是进行此项检查的最快方式,

而不是一个更符合的东西。读取方案

文件中的每个字符都找到文件的结尾?
>
Also, would this be the fastest way possible to make this check,
as opposed to a perhaps more "conformant" scheme of reading
every character in the file to find the end of the file?



我可以想象,因为fseek可以在操作系统级别上直接在I / O结构上工作。在最糟糕的情况下,fseek的性能不会比每次读取一个字符的文件更糟糕,所以我建议使用它。

I would imagine that, since fseek can work directly on I/O structures at
the OS level. In the worst case, fseek will not perform worse than
reading the file one character at a time, so I would recommend using it.


>

---

William Ernest Reid
>
---
William Ernest Reid




-

Pietro Cerutti



--
Pietro Cerutti


Bill Reid写道:
Bill Reid wrote:

这就是我处理支票的方式文本的最后一个字符

文件是换行符:
This is how I handle a check that the last character of a text
file is a newline:



为什么要这么麻烦?

Why bother?


/ *检查换行是否是文本文件的最后一个字符* /

unsigned check_text_file_newline_termination(FILE * test_file){

int end_char;


fseek(test_file,-1L,SEEK_END);
/* checks if newline is last character of text file */
unsigned check_text_file_newline_termination(FILE *test_file) {
int end_char;

fseek(test_file,-1L,SEEK_END);



"对于文本流,偏移量应为零,或者偏移量应为早先成功调用返回的值

值与同一文件关联的流上的ftell函数

应该是

SEEK_SET。"


你应该至少检查通话是否成功。如果它不是b $ b,你会回报什么?我建议你将返回

类型更改为int并返回EOF,0或一些正数。

"For a text stream, either offset shall be zero, or offset shall be a
value returned by an earlier successful call to the ftell function
on a stream associated with the same file and whence shall be
SEEK_SET."

And you should at least check whether the call succeeds. If it
doesn''t, what will you return? I suggest you change the return
type to int and return EOF, 0 or some positive number.


end_char = getc(test_file);

倒带(test_file);


if(end_char ==''\ n'')返回TRUE;

else返回FALSE;

}


问题是:这实际上是保证在

all>符合的情况下正常工作C实施?
end_char=getc(test_file);
rewind(test_file);

if(end_char==''\n'') return TRUE;
else return FALSE;
}

The question is: is this actually guaranteed to work properly on
all "conforming" C "implementations"?



编号但即使你读完整个文件,倒带也会失败。


-

Peter

No. But even if you read the whole file, rewind can fail.

--
Peter


" Bill Reid" < ho ******** @ happyhealthy.netwrites:
"Bill Reid" <ho********@happyhealthy.netwrites:

这是我处理文本最后一个字符的检查

文件是换行符:


/ *检查换行符是否为文本文件的最后一个字符* /

unsigned check_text_file_newline_termination(FILE * test_file){

int end_char;


fseek(test_file,-1L,SEEK_END);


end_char = getc(test_file );


倒带(test_file);


if(end_char ==''\ n'')返回TRUE;

else返回FALSE;

}
This is how I handle a check that the last character of a text
file is a newline:

/* checks if newline is last character of text file */
unsigned check_text_file_newline_termination(FILE *test_file) {
int end_char;

fseek(test_file,-1L,SEEK_END);

end_char=getc(test_file);

rewind(test_file);

if(end_char==''\n'') return TRUE;
else return FALSE;
}



布尔值的通常类型(除非你有C99的_Bool)是
int,而不是无符号。这没关系,但是读取你的代码的人都会浪费一点时间,想知道你为什么使用unsigned而不是

int。


大概在某处定义了TRUE和FALSE - 但是你真的不需要它们.b $ b不需要它们。我将if / else替换为:


返回end_char ==''\ n'';


你不喜欢检查fseek()是否成功。并非所有文件都可以搜索到
。 (尝试搜索到stdin的结尾,当它从你的键盘上读取时,你的键盘就会好了。)


倒带(test_file)回到开头文件 -

并不一定是调用函数之前的位置。如果你想要恢复文件的位置,请使用ftell()和fseek()。

The usual type for boolean values (unless you have C99''s _Bool) is
int, not unsigned. It doesn''t matter, but anyone reading your code is
going to waste a little time wondering why you used unsigned rather than
int.

Presumably TRUE and FALSE are defined somewhere -- but you really
don''t need them. I''d replace the if/else with:

return end_char == ''\n'';

You don''t check whether fseek() succeeded. Not all files are
seekable. (Try seeking to the end of stdin, when it''s reading from
your keyboard.)

rewind(test_file) goes back to the beginning of the file -- which
isn''t necessarily where it was before the function was called. If you
want to restore the file''s position, use ftell() and fseek().


问题是:是这实际上保证在

全部合规上正常工作C实施?我对规范的阅读说明了

没有,但当然它在几个系统上工作得很好我已经使用了它...
$ b
The question is: is this actually guaranteed to work properly on
all "conforming" C "implementations"? My reading of the spec says
"no", but of course it works just fine on the several systems I''ve
used it on...



你是对的,标准不能保证。对于文本文件,

fseek()要求偏移为零,或者先前对ftell()调用
和SEEK_SET值的返回值。 (因为你已经看到了b $ b,它恰好在你的系统上工作。)

You''re right, it''s not guaranteed by the standard. For a text file,
fseek() requires either an offset of zero, or an offset returned by an
earlier call to ftell() and a whence value of SEEK_SET. (As you''ve
seen, it happens to work on your system.)


此外,这是否是最快的方法进行这项检查,

而不是一个更符合的检查。读取方案

文件中的每个字符都找到文件的结尾?
Also, would this be the fastest way possible to make this check,
as opposed to a perhaps more "conformant" scheme of reading
every character in the file to find the end of the file?



可能。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

Nokia

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

Probably.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于这是完全便携和/或智能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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