byte oriented and wide oriented

byte oriented and wide oriented

Byte orientation refers to forms of data processing in which digital data are processed bytewise. For example, communication is called byte oriented or character oriented when the transmitted information is grouped into bytes.

The latter term is deprecated, since the notion of character has changed. An ASCII character fits to one byte (octet) in terms of the amount of information. With the internationalization of computer software, wide characters became necessary, to handle texts in different languages. In particular, Unicode characters can be 8, 16, or 32 bits long, i.e., 1, 2, or 4 bytes.

Byte oriented transmission makes use of byte-oriented protocols, that may involve transmission of additional bits as terminators, means of error recovery, etc.

Alternative approaches are bit oriented and word oriented.

—— wikipedia

上面是wiki 对byte oriented 和 wide oriented给出的解释。。。为byte oriented 和 wide oriented纠结好久

这和标准库函数函数fwide有关系:

#include <stdio.h>
#include <wchar.h>
int fwide(FILE *fp ,int mode);


Returns:

positive if stream is wide oriented,

negative if stream is byte oriented,

or 0 if stream has no orientation

The fwide function  performs  different  tasks,  depending  on  the  value  of  the mode
argument.
•If the mode argument  is  negative, fwide will  try  to  make  the  specified  stream
byte oriented.
•If the mode argument  is  positive, fwide will  try  to  make  the  specified  stream
wide oriented.
•If the mode argument  is  zero, fwide will  not  try  to  se
t  the  orientation,  but  will

still return a value identifying the stream’s orientation.

google了很多,也没给出具体是啥效果作用。。。

自己上!

Byte oriented 和 wide oriented是用来限制单次读写stream 的字节数的

如果不按照规则读写,则会发生读写失败

fwide(file_pointer,positive_number);

这样就可以把当前的file_pointer指向的流oriented 为widecharacter那么这个时候对这个流的每次读写操作都不能是以单个字节

test:

#include"stdio.h"
#include"string.h"

#defineBUFFSIZE 1024
#definePOSITIVE 1
 
intmain()
{
        FILE* file_pointer = NULL;
        char buffer[BUFFSIZE];
 
        int byte = 0;
 
        memset(buffer,0,BUFFSIZE*sizeof(char));
 
        file_pointer =fopen("./test.txt","r+");
 
        if(file_pointer == NULL)
        {
                printf("fopenerror\n");
                return 0;
        }
 
        fwide(file_pointer,POSITIVE);
 
        while((byte =fread(buffer,sizeof(char),BUFFSIZE,file_pointer)) > 0)
        {
               fwrite(buffer,sizeof(char),byte,stdout);
        }
 
        fclose(file_pointer);
        return 0;
}

root@ubuntu:/Ad_Pro_in_Unix/chapter_5#gcc ./fwide_test.c -g -o ./a.out

root@ubuntu:/Ad_Pro_in_Unix/chapter_5#./a.out

root@ubuntu:/Ad_Pro_in_Unix/chapter_5#

可以看到,这样读写就是失败的,不能用单个字节的方式读写这个流


对fwrite和fread做修改,改为每次读取多个字节

#include"stdio.h"
#include"string.h"
 
#defineBUFFSIZE 1024
#definePOSITIVE 1
 
intmain()
{
        FILE* file_pointer = NULL;
        char buffer[BUFFSIZE];
 
        int byte = 0;
 
        memset(buffer,0,BUFFSIZE*sizeof(char));
 
        file_pointer =fopen("./test.txt","r+");
 
        if(file_pointer == NULL)
        {
                printf("fopenerror\n");
                return 0;
        }
 
        fwide(file_pointer,POSITIVE);
 
        while((byte =fread(buffer,4*sizeof(char),BUFFSIZE,file_pointer)) > 0)
        {
               fwrite(buffer,4*sizeof(char),byte,stdout);
        }
 
        printf("\n");
        fclose(file_pointer);
        return 0;
}

root@ubuntu:/Ad_Pro_in_Unix/chapter_5# ./a.out

hello world!

wakaka

file standard library IO

root@ubuntu:/Ad_Pro_in_Unix/chapter_5# vim ./fwide_test.c

 

读写正常!


byte oriented的时候,stream的读写操作只能以单个字节进行,而wide oriented的时候只能以多字节形式进行。

如有理解不当的地方,还望高手明示。


你可能感兴趣的:(linux,unix,streams)