简体   繁体   English

在没有EOF时获取文本

[英]Get text while not EOF

Here's my code: 这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N 256

int main(int argc, const char * argv[]) {

    char testo[N];
    int i;
    printf("PER TERMINARE L'INSERIMENTO PREMERE CTRL+Z oppure CTRL+D \n");
    for(i=0;i<N;i++)
    {
        scanf("%c",&testo[i]);
        /* if(testo[i]=='h' && testo[i-1]=='c')
        {
            i--;
            testo[i]='k';
        }
        if(testo[i]==testo[i-1])
        {
            i--;
        } */
        if(testo[i]==EOF)
        {
            break;
        }
    }

    puts(testo);

    return 0;
}

When the code in /* ... */ is compiled, I can't stop the insert of text with EOF, but when the code is built and run as shown here, the EOF works. 编译/* ... */的代码时,我无法停止使用EOF插入文本,但是,如此处所示构建并运行代码时,EOF可以正常工作。

Does anyone have any idea what the problem is? 有谁知道这是什么问题吗?

You're testing for EOF incorrectly. 您正在错误地测试EOF。 With scanf() , you need to look at the return value. 使用scanf() ,您需要查看返回值。 In fact, with almost all input functions, you need to test, if not capture and test, the return value. 实际上,几乎所有输入功能都需要测试返回值(如果没有捕获并测试)。

Superficially, you need: 从表面上看,您需要:

for (i = 0; i < N; i++)
{
    if (scanf("%c", &testo[i]) == EOF)
        break;
    …
}

However, in general, you should check that scanf() made as many successful conversions as you requested, so it is better to write: 但是,通常,您应该检查一下scanf()进行了多少次成功的转换,因此最好编写:

for (i = 0; i < N; i++)
{
    if (scanf("%c", &testo[i]) != 1)
        break;
    …
}

In this example, it really won't matter. 在此示例中,这确实无关紧要。 If you were reading numeric data, though, it would matter. 但是,如果您正在读取数字数据,那将很重要。 The user might type Z instead of a number, and scanf() would return 0, not EOF. 用户可能键入Z而不是数字,并且scanf()将返回0,而不是EOF。

To detect EOF , check the result of scanf() 要检测EOF ,请检查scanf()的结果

if scanf("%c",&testo[i]) == EOF) break;

Note: testo[] may not be null character terminated. 注意: testo[]不能以空字符结尾。 To print as a string, insure it is. 要打印为字符串,请确保已打印。

char testo[N];
int i;

// for(i=0;i<N;i++) {
for(i=0;i<(N-1);i++) {
  if (scanf("%c",&testo[i]) == EOF) break;
}

testo[i] = '\0';  // add
puts(testo);

To stop at end of file, check the return value from scanf : 要在文件末尾停止,请检查scanf的返回值:

scanf returns the number of inputs correctly parsed. scanf返回正确解析的输入数。 In your case, %c reads a byte from the stream correctly as long as end of file has not been reached. 在您的情况下,只要未到达文件末尾, %c从流中正确读取一个字节。 if (scanf("%c",&testo[i]) != 1) break; will do. 会做。

Yet using scanf to read one byte at a time from the input stream is overkill. 但是使用scanf一次从输入流中读取一个字节是过大的。 The idiomatic way to do this in C is using the getchar() or the getc() function. 在C语言中,惯用的方法是使用getchar()getc()函数。 The return value must be stored in an int variable and has special value EOF upon end of file. 返回值必须存储在一个int变量中,并且在文件末尾具有特殊值EOF

You should also make the array 1 byte longer and store a null byte at the end to make it a C string, as expected by puts . 您还应该使数组长1个字节,并在结尾处存储一个空字节,以使其成为C字符串,如puts所期望的puts

Here is a modified version of your program: 这是程序的修改版本:

int main(int argc, const char *argv[]) {
    char testo[N+1];
    int i;

    printf("PER TERMINARE L'INSERIMENTO PREMERE CTRL+Z oppure CTRL+D\n");

    for (i = 0; i < N; i++) {
        int c = getchar();
        if (c == EOF)
            break;
        testo[i] = c;
        /* ... further processing ... */
    }
    testo[i] = '\0';

    puts(testo);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM