便携式EOL? [英] Portable EOL?

查看:75
本文介绍了便携式EOL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了下面的程序来创建和填充html

文件。我在vi中查看创建的文件时遇到了问题。

vi告诉我该文件没有行结束。


这是代码:


int

main(int argc,char ** argv){


FILE * fd;

int x;


char buff [8196];

x = 0;


bzero(buff,sizeof(buff));


(void)strlcpy(buff,"< HTML>",sizeof(buff)) ;


while(x <1024){

(void)strlcat(buff," A" sizeof(buff));

x ++;

}

(void)strlcat(buff,"< / HTML>" ;, sizeof(buff));


fd = fopen(" test5.html"," w +");

if(fd == NULL)

errx(-1,未能打开);

(无效)fprintf(fd,"%s",buff);

(void)fclose (fd);

退出(EXIT_SUCCESS);

}


现在,如果我改变这个,vi不会警告noeol s行:


(无效)fprintf(fd,"%s",buff);


to


(void)fprintf(fd,"%s \ n",buff);


我感到困惑的是我认为流自动终止了/>
在unix环境中带有CR(0a)的文件。困扰我的是

,理论上上面会使代码不那么便携。我是否可以忽略某些东西?

I have written the program below to just create and populate an html
file. I am running into a problem when viewing the created file in vi.
I am told by vi that the file does not have an end of line.

Here''s the code:

int
main(int argc, char **argv) {

FILE *fd;
int x;

char buff[8196];
x = 0;

bzero(buff, sizeof(buff));

(void)strlcpy(buff,"<HTML>", sizeof(buff));

while (x < 1024) {
(void)strlcat(buff,"A",sizeof(buff));
x++;
}
(void)strlcat(buff, "</HTML>", sizeof(buff));

fd = fopen("test5.html", "w+");
if (fd == NULL)
errx(-1, "failed to open");
(void)fprintf(fd, "%s", buff);
(void)fclose(fd);
exit(EXIT_SUCCESS);
}

Now, vi will not warn about noeol if I change this line:

(void)fprintf(fd, "%s", buff);

to

(void)fprintf(fd, "%s\n", buff);

Where I am confused is that I thought streams automatically terminated
the file with an CR (0a) in a unix environment. What bothers me is
that the above would in theory make the code less portable. Am I
overlooking something?

推荐答案

" bw ***** @ yahoo.com" ; < bw ***** @ yahoo.comwrites:
"bw*****@yahoo.com" <bw*****@yahoo.comwrites:

现在,如果我更改此行,vi将不会发出关于noeol的警告:

(无效)fprintf(fd,"%s",buff);





(无效)fprintf (fd,%s \ n,buff);


我感到困惑的是,我认为流自动终止了

unix环境中的CR(0a)。
Now, vi will not warn about noeol if I change this line:

(void)fprintf(fd, "%s", buff);

to

(void)fprintf(fd, "%s\n", buff);

Where I am confused is that I thought streams automatically terminated
the file with an CR (0a) in a unix environment.



不,这是错的。文本流中的最后一行需要

用新行字符明确终止。

-

程序员有不知道你的代码的许多细节的权利

并且仍然做出合理的改变。

--Kernighan和Plauger,_软件工具_

No, that''s just wrong. The last line in a text stream needs to
be explicitly terminated with a new-line character.
--
"Programmers have the right to be ignorant of many details of your code
and still make reasonable changes."
--Kernighan and Plauger, _Software Tools_


2006年8月19日11:08:32 -0700,bw ***** @ yahoo.com < bw ***** @ yahoo.com>

写道:
On 19 Aug 2006 11:08:32 -0700, "bw*****@yahoo.com" <bw*****@yahoo.com>
wrote:

>我已经编写了下面的程序来创建并填充一个html
文件。我在vi中查看创建的文件时遇到了问题。

vi告诉我该文件没有行尾。

这里是代码:

主要(int argc,char ** argv){

FILE * fd;
int x;

char buff [8196];
x = 0;

bzero(buff,sizeof(buff));

(void)strlcpy(buff) ,"< HTML>",sizeof(buff));
>I have written the program below to just create and populate an html
file. I am running into a problem when viewing the created file in vi.
I am told by vi that the file does not have an end of line.

Here''s the code:

int
main(int argc, char **argv) {

FILE *fd;
int x;

char buff[8196];
x = 0;

bzero(buff, sizeof(buff));

(void)strlcpy(buff,"<HTML>", sizeof(buff));



为什么是非标准功能?你怎么做不能用strcpy或strncpy做
?由于源字符串只有7个字符,所以你真的想要处理8,196。

Why a non-standard function? What does it do that you could not do
with strcpy or strncpy? Since the source string is only seven
characters, do you really want to process 8,196.


>
while(x < 1024){
(void)strlcat(buff,A,sizeof(buff));
x ++;
}
(void)strlcat(buff," ;< / HTML>" ;,sizeof(buff));
>
while (x < 1024) {
(void)strlcat(buff,"A",sizeof(buff));
x++;
}
(void)strlcat(buff, "</HTML>", sizeof(buff));



此时,buff包含一个六字符标题,1024个副本

字母A,一个七字符预告片,一个sting终止''\'''和

大约7000多个不感兴趣的字符。你从来没有在这个数组中放置

a''\ n''。

At this point, buff contains a six character header, 1024 copies of
the letter A, a seven character trailer, a sting terminating ''\0'', and
some 7000+ characters of no interest. At no point did you ever place
a ''\n'' in this array.


>
fd = fopen(test5.html,w +);
if(fd == NULL)
errx(-1,未能打开);
( void)fprintf(fd,"%s",buff);
>
fd = fopen("test5.html", "w+");
if (fd == NULL)
errx(-1, "failed to open");
(void)fprintf(fd, "%s", buff);



您的文件现在包含上述字符,最多但不是

,包括''\ 0''。

Your file now contains the characters described above, up to but not
including the ''\0''.


> (void)fclose(fd);
退出(EXIT_SUCCESS);
}
现在,如果我更改此行,vi将不会发出关于noeol的警告:

(void)fprintf(fd,%s,buff);


(void)fprintf(fd,"%s \ n" ,buff);
> (void)fclose(fd);
exit(EXIT_SUCCESS);
}

Now, vi will not warn about noeol if I change this line:

(void)fprintf(fd, "%s", buff);

to

(void)fprintf(fd, "%s\n", buff);



您的格式字符串现在包含一个额外的字符,在您的数据之后立即将

写入文件。

Your format string now includes an additional character which will get
written to the file immediately after your data.


>
我感到困惑的是,我认为流在unix环境中自动终止了带有CR(0a)的文件。困扰我的是
>
Where I am confused is that I thought streams automatically terminated
the file with an CR (0a) in a unix environment. What bothers me is



如果这是真的,那么多次调用fprintf就无法使用

从单独的部分构建一条线。

If that were true, then multiple calls to fprintf could not be used to
build up a line from separate pieces.


>以上理论上会使代码不那么便携。我是否忽视了什么?
>that the above would in theory make the code less portable. Am I
overlooking something?



比什么便携? \ n是C'的可移植方式,表示此时输出应包含行尾指示符的



运行时库将执行你的系统必需的魔法。

这对于unix来说可能是0a,对于windows来说可能是0a0d(或者是0d0a)。对于我的IBM大型机来说,
将是完全不同的,取决于RECFM是U,V还是F.取决于
。重点是它是可移植的。

代码并不需要关心,编译器也不在乎,如果在

全部。

删除del for电子邮件

Less portable than what? The \n is C''s portable way of indicating
that the output should contain an end of line indicator at this point.
The run time library will perform the magic necessary for your system.
This may be an 0a for unix or 0a0d (or is it 0d0a) for windows. It
will be something completely different for my IBM mainframe depending
on whether the RECFM is U, V, or F. The point is it is portable. The
code doesn''t have to care and the compiler doesn''t care much, if at
all.
Remove del for email




Ben Pfaff写道:

Ben Pfaff wrote:

不,那是'错了。文本流中的最后一行需要使用换行符明确终止

No, that''s just wrong. The last line in a text stream needs to
be explicitly terminated with a new-line character.



谢谢。这就是我错的地方。


除了

便携性之外,还有什么理由使用流与unix I / O?我意识到,如果我使用unix I / OI必须自己做

缓冲,

但这不是什么大问题。

哦,8196缓冲区是从unix I / O重新写入

流来保留的。

strlcat远远优于strcat和strncat。

Thanks. That is where I am wrong.

Are there any good reasons to use streams versus unix I/O besides
portability? I realize that if I use unix I/O I have to do my own
buffering,
but that''s not a big deal.

Oh, the 8196 buffer was left over from re-writing this from unix I/O to
streams.
And strlcat is vastly superior to strcat and strncat.


这篇关于便携式EOL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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