如何激发EOF没有用C preceding换行符 [英] How to stimulate EOF without preceding newline in C

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

问题描述

可以说我有下面的C code:

  INT函数getline(焦线[])
{
    INT C,I = 0;
    而((C =的getchar())!= EOF)
           线[I +] = C;
    线[I +] = C;
    返回我;
}>>输入:007
>> ^ Z
>>输出:

如果我们仔细观察,我给上面的输出方式,我是pressing 输入刺激EOF之前。由此,串的长度是4不是3-(不含EOF)。

当我做我的练习,我真的面临一些麻烦与额外的\\ n。

我如何刺激EOF不换行?是否有可能呢?

 >>输入:007 ^ Z
>> ^ Z
>>输出:长度= 6


解决方案

我的意见转换成回答:


  

在哪个平台?在Unix和衍生工具,则可以键入EOF字符两次 - 通常是<大骨节病>控制-D 而非<大骨节病>控制-Z ,虽然。这也可以在Windows上工作;我不知道,但它是值得一试。


(A响应意见认为,平台是Windows)。


  

在Unix上,控制-D 使上线的数据提供给
  程序。第一个<大骨节病>控制-D 给它你已经输入的内容;该
  第二个给了它零字节读取,这是EOF的指示。


Lets say I have the below C code:

int getLine (char line[])
{
    int c, i=0;
    while( (c=getchar()) != EOF )
           line[i++]=c;
    line[i++] = c;
    return i;
}

>> Enter: 007
>> ^Z
>> Output: 

If we closely observe the way I give output above, I am pressing Enter before stimulating EOF. This means, the length of string is 4 not 3 (excluding EOF).

When I am doing my exercises, I am really facing some trouble with that extra \n.

How do I stimulate EOF without newline? Is it possible at all?

>> Enter: 007^Z
>> ^Z
>> Output: length=6

解决方案

Converting my comment into an answer:

On which platform? On Unix and derivatives, you would type the EOF 'character' twice — usually control-D rather than control-Z, though. That may also work on Windows; I don't know, but it is worth a try.

(A response comment affirms that the platform is Windows.)

On Unix, control-D makes the data on the line available to the program. The first control-D gives it what you've typed already; the second gives it zero bytes to read, which is the indication of EOF.

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

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