文件中的“^ @”是什么意思?

时间:2022-10-06 16:53:53

Code:

码:

int fd;
fd = open("fruit", O_WRONLY);
write(fd, "apple", sizeof("apple"));
close(fd);

I compile it with

我用它编译它

$ gcc test.c -o test

and run as

并运行

$ ./test   

Then I open the fruit file, and I see the following in the file:

然后我打开水果文件,我在文件中看到以下内容:

apple^@

What does the ^@ mean?

^ @是什么意思?

2 个解决方案

#1


40  

It is the null character code '\0'. Certain editors like vi display it as ^@.

它是空字符代码'\ 0'。某些编辑器如vi将其显示为^ @。

sizeof("apple") would return 6 because it includes the null character used to terminate strings.

sizeof(“apple”)将返回6,因为它包含用于终止字符串的空字符。

#2


38  

The ^@ is the way an ASCII NUL is commonly represented in printable form. That is the same as the @ character with some bits removed:

^ @是ASCII NUL通常以可打印形式表示的方式。这与删除了一些位的@字符相同:

@ = 0100
^@ = 0

and it is the same as '\0' (the string terminator in C). Because it is the string terminator, you would not see it from printf or its related functions, but you can readily create it using the block-oriented write. For example, you could have written

它与'\ 0'(C中的字符串终止符)相同。因为它是字符串终止符,所以您不会从printf或其相关函数中看到它,但您可以使用面向块的写入来轻松创建它。例如,你可以写

write(fd,"apple\0orange",sizeof("apple\0orange"));

and seen

并看到了

apple^@orange^@

because every double-quoted literal in C has a trailing string terminator which is counted in its size. If you had meant to write the string without its terminator, you could have done this:

因为C中的每个双引号文字都有一个尾随的字符串终止符,它的大小是计算的。如果您打算在没有终结符的情况下编写字符串,那么您可以这样做:

const char *s = "apple";
write(fd,s,strlen(s));

thereby eliminating two problems in the example: (a) incorrect length and (b) possibly using inconsistent string content and length by ensuring both are the same item. See Sizeof string literal for some comments on (a).

从而消除了示例中的两个问题:(a)不正确的长度和(b)通过确保两者是相同的项目可能使用不一致的字符串内容和长度。有关(a)的一些注释,请参见字符串文字的大小。

NUL is one of the 32 ASCII control characters whose values range from 0 to 31, called C0 controls. All of these ASCII control characters are typically shown in this way (for a printable form), using the character corresponding to adding 64 (0100) to the control character's value.

NUL是32个ASCII控制字符之一,其值范围从0到31,称为C0控制。所有这些ASCII控制字符通常以这种方式显示(对于可打印的形式),使用对应于将64(0100)添加到控制字符值的字符。

ASCII DEL is 127 (0177). Displaying it as ^? is a special case which is more recent than the other ASCII control characters. For instance, X/Open terminfo (curses) does not define a printable form for this character, although it does for the others. Unlike the other ASCII control characters, DEL is formed by OR'ing all (seven) bits into the character.

ASCII DEL为127(0177)。将其显示为^?是一个特殊情况,它比其他ASCII控制字符更新。例如,X / Open terminfo(curses)没有为这个角色定义可打印的形式,尽管它适用于其他角色。与其他ASCII控制字符不同,DEL是通过对所有(七)位进行OR运算形成的。

ASCII is a 7-bit code, of course. Many other codes were developed; ASCII corresponds to the POSIX portable character set, so it is frequently encountered.

当然,ASCII是一个7位代码。制定了许多其他法规; ASCII对应于POSIX可移植字符集,因此经常遇到它。

It is easy to find tables of ASCII characters with a web search. Most of these tables (or their accompanying discussion) veer off into misinformation. Here is a link to a reasonably factual page, entitled ASCII Character Set. It states

通过Web搜索很容易找到ASCII字符表。大多数这些表格(或其随附的讨论)都会转向错误信息。这是一个名为ASCII字符集的合理事实页面的链接。它指出

The Control key subtracts 64 from the value of the keys that it modifies.

Control键从其修改的键值中减去64。

However, the statement is only correct if the key is one of those from the set @, A, B, etc. If you apply that to other keys, the results are perhaps interesting but not useful. Rather, in a C program you would do a logical masking, e.g.,

但是,如果键是来自集合@,A,B等的键之一,则该语句才是正确的。如果将其应用于其他键,结果可能很有意思但无用。相反,在C程序中,您将进行逻辑屏蔽,例如,

ch = ch & 037;

to obtain a character in the range 0 to 31.

获取0到31范围内的字符。

#1


40  

It is the null character code '\0'. Certain editors like vi display it as ^@.

它是空字符代码'\ 0'。某些编辑器如vi将其显示为^ @。

sizeof("apple") would return 6 because it includes the null character used to terminate strings.

sizeof(“apple”)将返回6,因为它包含用于终止字符串的空字符。

#2


38  

The ^@ is the way an ASCII NUL is commonly represented in printable form. That is the same as the @ character with some bits removed:

^ @是ASCII NUL通常以可打印形式表示的方式。这与删除了一些位的@字符相同:

@ = 0100
^@ = 0

and it is the same as '\0' (the string terminator in C). Because it is the string terminator, you would not see it from printf or its related functions, but you can readily create it using the block-oriented write. For example, you could have written

它与'\ 0'(C中的字符串终止符)相同。因为它是字符串终止符,所以您不会从printf或其相关函数中看到它,但您可以使用面向块的写入来轻松创建它。例如,你可以写

write(fd,"apple\0orange",sizeof("apple\0orange"));

and seen

并看到了

apple^@orange^@

because every double-quoted literal in C has a trailing string terminator which is counted in its size. If you had meant to write the string without its terminator, you could have done this:

因为C中的每个双引号文字都有一个尾随的字符串终止符,它的大小是计算的。如果您打算在没有终结符的情况下编写字符串,那么您可以这样做:

const char *s = "apple";
write(fd,s,strlen(s));

thereby eliminating two problems in the example: (a) incorrect length and (b) possibly using inconsistent string content and length by ensuring both are the same item. See Sizeof string literal for some comments on (a).

从而消除了示例中的两个问题:(a)不正确的长度和(b)通过确保两者是相同的项目可能使用不一致的字符串内容和长度。有关(a)的一些注释,请参见字符串文字的大小。

NUL is one of the 32 ASCII control characters whose values range from 0 to 31, called C0 controls. All of these ASCII control characters are typically shown in this way (for a printable form), using the character corresponding to adding 64 (0100) to the control character's value.

NUL是32个ASCII控制字符之一,其值范围从0到31,称为C0控制。所有这些ASCII控制字符通常以这种方式显示(对于可打印的形式),使用对应于将64(0100)添加到控制字符值的字符。

ASCII DEL is 127 (0177). Displaying it as ^? is a special case which is more recent than the other ASCII control characters. For instance, X/Open terminfo (curses) does not define a printable form for this character, although it does for the others. Unlike the other ASCII control characters, DEL is formed by OR'ing all (seven) bits into the character.

ASCII DEL为127(0177)。将其显示为^?是一个特殊情况,它比其他ASCII控制字符更新。例如,X / Open terminfo(curses)没有为这个角色定义可打印的形式,尽管它适用于其他角色。与其他ASCII控制字符不同,DEL是通过对所有(七)位进行OR运算形成的。

ASCII is a 7-bit code, of course. Many other codes were developed; ASCII corresponds to the POSIX portable character set, so it is frequently encountered.

当然,ASCII是一个7位代码。制定了许多其他法规; ASCII对应于POSIX可移植字符集,因此经常遇到它。

It is easy to find tables of ASCII characters with a web search. Most of these tables (or their accompanying discussion) veer off into misinformation. Here is a link to a reasonably factual page, entitled ASCII Character Set. It states

通过Web搜索很容易找到ASCII字符表。大多数这些表格(或其随附的讨论)都会转向错误信息。这是一个名为ASCII字符集的合理事实页面的链接。它指出

The Control key subtracts 64 from the value of the keys that it modifies.

Control键从其修改的键值中减去64。

However, the statement is only correct if the key is one of those from the set @, A, B, etc. If you apply that to other keys, the results are perhaps interesting but not useful. Rather, in a C program you would do a logical masking, e.g.,

但是,如果键是来自集合@,A,B等的键之一,则该语句才是正确的。如果将其应用于其他键,结果可能很有意思但无用。相反,在C程序中,您将进行逻辑屏蔽,例如,

ch = ch & 037;

to obtain a character in the range 0 to 31.

获取0到31范围内的字符。