如何在C中使用EOF [英] How to use EOF in C

查看:62
本文介绍了如何在C中使用EOF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我想学习如何在C中使用EOF。请帮助我。

Hi friends,
I want to learn how to use EOF in C.Please help me.

推荐答案

7月11日上午10:41,Ramesh< chary ... @ gmail.comwrote:
On Jul 11, 10:41 am, Ramesh <chary...@gmail.comwrote:

朋友们,

我想学习如何在C中使用EOF。请帮助我。
Hi friends,
I want to learn how to use EOF in C.Please help me.



C为您提供表示文件结尾的EOF。以下代码可以

帮助你。

int get_line(char line [],int max)

{

int nch = 0;

int ch;

max = max - 1; // initally max指向\0

//所以我们需要在\0

之前获取char((ch = getchar( ))!= EOF)// getchar返回char来自

{

if(ch ==''\ n'')

休息;


if(nch< max)

{

line [nch] = ch?;

nch = nch + 1;

}

}


if(ch == EOF&& nch == 0)

返回EOF;


line [nch] =''\ 0'';

返回nch;

}


#include< stdio.h>

extern int get_line(char [],int);

main()

{

char l [200];


while(getline( l,200)!= EOF)

printf(" line read\"%s \" \ n",l);


返回0;

}

C provides u with EOF denoting end of file. The following code may
help you.
int get_line(char line[], int max)
{
int nch = 0;
int ch;
max = max - 1; //initally max is pointing to \0
//So we need to get the char before \0

while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == ''\n'')
break;

if(nch < max)
{
line[nch] = ch;
nch = nch + 1;
}
}

if(ch == EOF && nch == 0)
return EOF;

line[nch] = ''\0'';
return nch;
}

#include <stdio.h>
extern int get_line(char [], int);
main()
{
char l[200];

while(getline(l, 200) != EOF)
printf("line read\"%s\"\n", l);

return 0;
}


sumedh写道:
sumedh wrote:

7月11日上午10点41分,Ramesh< chary ... @ gmail.comw死记硬背:
On Jul 11, 10:41 am, Ramesh <chary...@gmail.comwrote:

朋友们,

我想学习如何在C中使用EOF。请帮助我。
Hi friends,
I want to learn how to use EOF in C.Please help me.



C为您提供表示文件结尾的EOF。


C provides u with EOF denoting end of file.



确切地说,EOF是stdio.h中定义的宏,它产生一个

整数常量。当它们遇到文件结束条件或错误时,它被许多C'的I / O

函数用作返回值。

这对功能feof和ferror通常用于查找

中的哪一个。

To be precise, EOF is a macro defined in stdio.h that yields an
integer constant. It is used as the return value by many of C''s I/O
functions when they encounter an end-of-file condition or an error.
The pair of functions feof and ferror are typically used to find out
which of the two it is.


以下代码可能

帮助你。

int get_line(char line [],int max)

{

int nch = 0;

int ch;

max = max - 1; // initally max指向\0

//所以我们需要在\0
之前获取char
The following code may
help you.
int get_line(char line[], int max)
{
int nch = 0;
int ch;
max = max - 1; //initally max is pointing to \0
//So we need to get the char before \0



这种类型的评论仅在C99时支持。当在Usenet的帖子中使用时,它往往会打破

This type of comment is only supported as of C99. Also it tends to
break when used in posts to Usenet.


while((ch = getchar())!= EOF)/ / getchar从
返回char
{

if(ch ==''\ n'')

break;


if(nch< max)

{

line [nch] = ch;

nch = nch + 1;

}

}


if(ch == EOF&& nch == 0)

返回EOF;


line [nch] =''\ 0'';

返回nch;

}


#include< stdio.h>

extern int get_line(char [],int);

main()
while((ch = getchar()) != EOF) //getchar returns char from
{
if(ch == ''\n'')
break;

if(nch < max)
{
line[nch] = ch ;
nch = nch + 1;
}
}

if(ch == EOF && nch == 0)
return EOF;

line[nch] = ''\0'';
return nch;
}

#include <stdio.h>
extern int get_line(char [], int);
main()



声明main以显式返回int。如果你不想打开命令行,你可以使用void表示它没有

取任何参数。

int main(无效){

Declare main to explicitly return an int. And if you don''t intend to
access the command line you can use void to indicate that it does not
take any parameters.

int main(void) {


{

char l [200];


while(getline(l,200)!= EOF)
{
char l[200];

while(getline(l, 200) != EOF)



getline定义在哪里?

Where is getline defined?


printf(" line read \"%s \" \ n",l);


返回0;

}
printf("line read\"%s\"\n", l);

return 0;
}


Ramesh< ch ****** @ gmail.comwrites:
Ramesh <ch******@gmail.comwrites:

我想学习如何在C中使用EOF。请帮助我。
I want to learn how to use EOF in C.Please help me.



任何体面的C教科书都会解释这一点。我推荐Kernighan&

Ritchie'的_The C Programming Language _,2nd Edition,俗称

K& R2。

$ b但是我认为你问的是错误的问题。而不是询问如何使用EOF,你应该尝试学习如何读取和b / b
过程输入。使用EOF只是其中的一部分。


-

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

圣地亚哥超级计算机中心< *< http ://users.sdsc.edu/~kst>

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

- Antony Jay和Jonathan Lynn,是部长

Any decent C textbook will explain this. I recommend Kernighan &
Ritchie''s _The C Programming Language_, 2nd Edition, commonly known as
K&R2.

But I think you''re asking the wrong question. Rather than asking how
to use EOF, you should probably be trying to learn how to read and
process input. The use of EOF is just a part of that.

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


这篇关于如何在C中使用EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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