0

What is the use of the functionfwide?

int fwide(FILE *stream, int mode);

As per the man page it shows: "set and determine the orientation of a FILE stream". What is meant by orientation of file stream?

As per my thought, while reading the character from file using some functions like fgetc, it takes one byte to store the retrieved data. So, using fwide function, we can able to change the size of ascii character to the equivalent size of international character set(UCS). Is it right ? If it is right, How to change it ?

Bentoy13
  • 4,886
  • 1
  • 20
  • 33
mohangraj
  • 9,842
  • 19
  • 59
  • 94
  • 1
    Using wide characters doesn't automatically imply UCS encoding. I also recommend you read [the **NOTES** section of the manual page](http://man7.org/linux/man-pages/man3/fwide.3.html#NOTES), as the practical use of `fwide` seems quite limited. – Some programmer dude Sep 22 '15 at 09:55

3 Answers3

1

Here orientation is meant by character type orientation in file. That is stream is byte-character or wide-character (i.e char or wchar_t).

If mode>0 it attempts to change to wide-character (wchar_t) orientation.

if mode<0, byte-character (char) orientation.

And if mode==0 then it doesn't attempt to change orientation and will determine current orientation of file .

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • 1
    In C terms, `char` vs `wchar_t`. – John Zwinck Sep 22 '15 at 09:57
  • @JohnZwinck mentioned that . Thanks !! – ameyCU Sep 22 '15 at 09:58
  • So, by default, the character take 1 byte. If it is wide character it will take more than one byte. So, How to change the mode of reading from file from normal character to wide character. – mohangraj Sep 22 '15 at 09:59
  • If mode is set to 0, On what basis it will read the character from file? – mohangraj Sep 22 '15 at 10:06
  • @mohan if `mode` is `0` it will determine current orientation of file . Function does not only set orientation but also determine it. – ameyCU Sep 22 '15 at 10:08
  • using fgetwc function, it gets worked. But, the size of the character returned by that function is remain same. It doesn't get incremented. why? The value of 'a' remains 97 – mohangraj Sep 22 '15 at 10:30
  • @mohan How data is stored in file ? char ? – ameyCU Sep 22 '15 at 10:34
  • Yes. But, during retrival, we are changed to wide character. Is it right? or it is applicable for some character except english alphabets – mohangraj Sep 22 '15 at 10:36
  • @mohan wchar_t was introduced to store new characters but also keeping ascii codes . But if a wchar_t variable has `a` then its size should be 2 bytes .What size do you get ? – ameyCU Sep 22 '15 at 10:37
  • @mohan _The value of 'a' remains 97_ yes because it also stores old ascii codes . All differs is size . – ameyCU Sep 22 '15 at 10:42
  • So, is threre any character which occupy more than one byte – mohangraj Sep 22 '15 at 11:04
  • @mohan Yes there are .http://stackoverflow.com/questions/10017328/unicode-stored-in-c-char – ameyCU Sep 22 '15 at 11:31
1

int fwide(FILE *stream, int mode) is a function to set (also, get) the stream's orientation. By orientation, either of the three things can be true --

  • if the mode is 0, fwide determines the current orientation of the stream.

  • if the mode is positive, the stream's wide-character oriented.

  • if the mode is negative, the stream's byte oriented.

This holds for getting, setting of the stream

Your understanding is not entirely off track, just somewhat. When you want to read data in wchar_t format, then you set it to wide-character orientation, otherwise the usual ASCII suffices.

To simplify further, when you want to read the characters which are beyond the capacity of simple "char" data type, you change the orientation to wchar_t.

I have encountered cases where byte orientation was working for "other-language" characters as below.

Error = 错误: Severity = 严重性: State = 状态:

No idea about how that worked - but yeah, using this function is pretty simple, except for an already 'on' stream. I tried to set-use-reset a stream and the reset part failed. I guess better approach would be to play more with it.

Siddharth
  • 321
  • 1
  • 5
0

You cannot change the width of a file once it has been set. fopen(3) and freopen(3) support additional characters in their mode argument that describe the character set of the file:

fd = fopen("input.txt", "r,ccs=CP437");

If no such argument is given then the width of the first function used to access it (e.g. fgetc(3) vs. fgetwc(3)) will lock in the width of the file.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358