简体   繁体   English

输出中的'y'代表什么?

[英]What does 'y' in the output stand for in C?

I have a problem, I construct a string in a loop and the output of that string to stout displays the string and a character 'y' with two dots above it as the last character. 我有一个问题,我在一个循环中构造一个字符串,该字符串输出到stout显示字符串和一个字符'y',上面有两个点作为最后一个字符。

What is that? 那是什么?

I create the string in this function: 我在这个函数中创建了字符串:

char get_string(char *buf, int ble, FILE *fp, char del)
{
    int i = 0;
    int c;
    char    result;

    memset(buf, 0, BUFLEN);

    do {

        c = fgetc(fp);

        if (c == del) {
            buf[i] = '\0';
            result = c;
            break;
        } else if(c == '\n') {
            buf[i] = '\0';
            result = '\n';
            break;
        } else {
            buf[i] = c;
            i++;
        }

    } while (c != EOF);

    return result;
}

and then use the buf and result as follows in another function: 然后在另一个函数中使用buf和结果如下:

char    pair[BUFLEN];
char    end;

do {

        end = get_string(pair, BUFLEN, fp, ';');
        printf("Result: %s\n",pair);

} while (pair != NULL);

The last iteration of the above Prints out "Result: y" I have no idea why. 上面的最后一次迭代打印出“结果:y”我不知道为什么。

You're using a do - while loop, which means that you're executing the loop body before testing for EOF , so you end up putting EOF in your buffer as well. 你正在使用do - while循环,这意味着你在测试EOF之前正在执行循环体,所以你最终也将EOF放入缓冲区。 The EOF value of -1 gets translated into the character range where it corresponds to ÿ. EOF值-1被转换为与corresponds对应的字符范围。 I'd recommend you to just switch to a more usual while loop because it handles this condition more naturally. 我建议你只需切换到更常用的while循环,因为它更自然地处理这种情况。

ÿ is the glyph for the character which (in Unicode and many ISO-8859-? encodings) has the ordinal value 0xFF. ÿ是字符的字形(在Unicode和许多ISO-8859-?编码中)具有序数值0xFF。 That value, also known in decimal as 255, is also used in some contexts as "the end-of-file character" (aka EOF) -- although there's no standard that defines the character as such (AFAIK), the value -1 is what does get returned in many languages (such as C) when you try to read more from a file that's exhausted ("at end of file"). 该值也以十进制表示为255,在某些上下文中也用作“文件结束字符”(又名EOF) - 尽管没有标准定义字符 (AFAIK),值-1当您尝试从耗尽的文件(“文件末尾”)中读取更多内容时,会以多种语言(例如C)返回。

In practice, therefore, an unexpected ÿ in your output often means that you are erroneously interpreting a byte that's intended to signify "end of something" (a byte encoded with all bits set to one) as if it was part of the text to display. 因此,在实践中,输出中的意外ÿ通常意味着您错误地解释了一个意图表示“结束某事”的字节(一个字节编码为所有位设置为1),就像它是要显示的文本的一部分一样。

'y' with two dots over it is character 0xFF (in latin-1 - the default codepage for the console). 'y'上面有两个点是字符0xFF(在latin-1中 - 控制台的默认代码页)。

0xFF as a 8-bit signed value is -1. 0xFF作为8位有符号值为-1。

Look for places where you're printing -1 as a char (or using -1 as a char and then printing it). 查找您将-1作为char打印的位置(或使用-1作为char然后打印它)。

When you "construct your string in a loop", do you remember to properly terminate it with a '\\0' ? 当你“在一个循环中构造你的字符串”时,你还记得用'\\0'正确地终止它吗?

If the loop assigns characters to a character array, the last array item should be '\\0' . 如果循环将字符分配给字符数组,则最后一个数组项应为'\\0'

Ok, after seeing the code, you are terminating the string. 好的,看到代码后,你正在终止字符串。

EDIT : 编辑

Looks like you are including the EOF character in your string. 看起来你在字符串中包含了EOF字符。 This is one case where the string is not terminated properly. 这是字符串未正确终止的一种情况。 You should check for an EOF in your if-else structure and handle it properly. 您应该在if-else结构中检查EOF并正确处理它。

One other thing I noticed: 我注意到的另一件事:

You are assigning an int c to a char result when returning from your function. 从函数返回时,您正在为char result分配int c The compiler should have warned you, that you are trying to put a larger data type into a smaller data type. 编译器应警告您,您正在尝试将较大的数据类型放入较小的数据类型中。 Depending on what the purpose of the return value is, I would think about changing the return data type to int . 根据返回值的目的,我会考虑将返回数据类型更改为int

Your if -statement, which ends with an else putting the character into the buffer has two flaws: 你的if -statement以一个将字符放入缓冲区的else结尾有两个缺点:

  1. It does not filter out the EOF special "character", which signifies the end of the stream 它不会过滤出EOF特殊“字符”,它表示流的结束
  2. It does not check for buffer overrun by comparing i with the BUFLEN value. 它不会通过将iBUFLEN值进行比较来检查缓冲区溢出。

The first problem is the cause of your ÿ character, when the stream ends, you add the EOF character to the buffer, then the loop terminates. 第一个问题是你的ÿ字符的原因,当流结束时,你将EOF字符添加到缓冲区,然后循环终止。

The fix is to put a clause into your if-else statement to filter that away, like this: 修复是在if-else语句中放入一个子句来过滤掉它,如下所示:

} else if (c != EOF) {
    buf[i] = c;
    i++;
}

The second problem you need to decide how to handle before fixing, but it should be fixed. 在修复之前需要决定如何处理的第二个问题,但应该修复它。

You do not properly null-terminate your string. 您没有正确地终止字符串。 If reading from fp never returns "del" or "\\n" and you reach EOF, there will be no null-terminator. 如果从fp读取永远不会返回“del”或“\\ n”并且您达到EOF,则不会有空终止符。 You need to fix your code. 您需要修复代码。

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

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