开发者

computer basics problem

开发者 https://www.devze.com 2023-01-25 17:32 出处:网络
hi everybody cananyone tell me answer of this question ? i created a simple txt file. it contain only two words and the words are hello word according to i studied computer uses ascii code to store t

hi everybody can anyone tell me answer of this question ? i created a simple txt file. it contain only two words and the words are hello word according to i studied computer uses ascii code to store the text on disk or memory .In ascii code each letter or symbol is represented by one byte or in simple word开发者_StackOverflow社区s one byte is used to store a symbol. Now the problem is this when ever i saw the size of file it shows 11 byte I understand 9 byte for words one byte for space makes the total of 10 then why it is showing 11 byte size .i tried different things such as changing the name of file saving it with shortest name possible or longest name possible but it did not change the total storage so can any body explain why it is happening? i tried this thing over window or Linux(Ubuntu.centos) system result is same.


pax> echo hello word >outfile.txt

pax> ls -al outfile.txt 
-rw-r--r-- 1 pax   pax   11 2010-11-19 15:34 outfile.txt

pax> od -xcb outfile.txt 
0000000    6568    6c6c    206f    6f77    6472    000a
          h   e   l   l   o       w   o   r   d  \n
        150 145 154 154 157 040 167 157 162 144 012

pax> hd outfile.txt 
00000000  68 65 6c 6c 6f 20 77 6f  72 64 0a                 |hello word.|
0000000b

As per above, you're storing "hello word" and the newline character. That's 11 characters in total. If you don't want the newline, you can use something like the -n option of echo (which doesn't add the newline):

pax> echo -n hello word >outfile.txt

pax> ls -al outfile.txt 
-rw-r--r-- 1 pax   pax   10 2010-11-19 15:36 outfile.txt

pax> od -xcb outfile.txt 
0000000    6568    6c6c    206f    6f77    6472
          h   e   l   l   o       w   o   r   d
        150 145 154 154 157 040 167 157 162 144

pax> hd outfile.txt 
00000000  68 65 6c 6c 6f 20 77 6f  72 64                    |hello word|
0000000a


If you want to see the content of the file you can perform an octal dump of it using the "od" command under linux "od ". Most probably what you will see is a CR (carriage return) and a LN (linefeed). The name of the file has nothing to do with his size.

Luis


Did you a new line in the text file (\n)? Just because this character cannot be seen does not mean it is not there.

0

精彩评论

暂无评论...
验证码 换一张
取 消