English 中文(简体)
在投入的中间点,植被与低地轨道基金发生什么?
原标题:What happens when fgets encounters EOF in the middle of an input?
  • 时间:2012-01-14 10:50:16
  •  标签:
  • c

男子页说:

fgets()的回报是成功的,UNL在出现错误或档案结束时,没有读过任何特性。

我写了一小篇C案,呼吁用心子检验其行为。 我特别想看到,在一些特性得到投入之后,《观察员部队》发生了什么。 我将钥匙组合(Ctrl+D)用于ash。 我不得不两次施压Ctrl+D,以恢复植被。 在我两次催促Ctrl+D之前,它印刷了投入的特性。 Pressing Ctrl+ 某几类物品一经输入,根本无效果。 如果我在此之后对一些特性提出意见,这些特性就储存在已经通过的各阵列中。 为什么要这样作?

最佳回答

The behavior you describe has nothing to do with fgets. The shell does not close the input stream until you press ctrl-D the 2nd time.

问题回答

You should find that if the input ends after at least some characters were read but before a newline is encountered that fgets returns non-null (a pointer to the supplied buffer) and the supplied buffer won t contain a newline but will be null terminated.

这正是<代码>fgets的文件所说的。

E.g.

#include <stdio.h>

int main(void)
{
    char buffer[200];

    char* ret = fgets(buffer, sizeof buffer, stdin);

    printf("exit code = %p
", (void*)ret);

    if (ret != 0)
    {
        printf("read code = %s<--END
", buffer);
    }

    return 0;
}

产出:

$ printf "no newline here->" | ./a.out
exit code = 0x7fff6ab096c0
read code = no newline here-><--END

$ printf "newline here->
more text
" | ./a.out
exit code = 0x7fff6f59e330
read code = newline here->
<--END

$ printf "" | ./a.out
exit code = (nil)

它只是从光盘上储存的文字档案中读取,在最后一行的末尾不会出现新的线性。 http://www. open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf”rel=“nofollow” C 标准>>) 说明其做了哪些工作,包括:

char *fgets(char * restrict s, int n, FILE * restrict stream);

The fgets function reads at most one less than the number of characters specified by n from the stream pointed to by stream into the array pointed to by s. No additional characters are read after a new-line character (which is retained) or after end-of-file. A null character is written immediately after the last character read into the array.

<>代码>fgets(>>>,如果输入线太长,或如果在文件终结之前没有新线,可向您提供一条没有新线的插座。

当你从磁盘中读取时,如果你到达卷宗的末尾,就会出现笔迹。 当你从钥匙板等互动装置读取时,C标准没有具体规定如何启动档案结束状态。 关于不相像的系统,它是由在一线开始时打字Control-D,或者在一线中打字Control-D两次(尽管控制特性可以重新配置)。

能否做到这一点,取决于执行情况。

C99 7.19p2 says:

A text stream is an ordered sequence of characters composed into lines, each line consisting of zero or more characters plus a terminating new-line character. Whether the last line requires a terminating new-line character is implementation-defined.

类似混合的系统一般不需要新的线性。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签