Results 1 to 5 of 5
Hi all,
I am trying to read UTF16 encoded data from stdin(as of now only thru file re-direction). I am writing a C program for this and am on RedhatLinux ...
- 10-19-2009 #1Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Reading UTF16 input from STDIN using fgetws()
Hi all,
I am trying to read UTF16 encoded data from stdin(as of now only thru file re-direction). I am writing a C program for this and am on RedhatLinux AS.
Man pages of fgetws() says,
fgetws() converts the input string from the encoding either specified by "LC_CTYPE" or specified in fopen() to wide character string.
My intention is to read UTF16 input from STDIN.
For this i had tried following block of code but was not succesful.
wchar_t buf[512]={0};
FILE *res=NULL;
res=fdopen(fileno(stdin), "r,ccs=UTF16-LE");
fgetws(buf, countof(buf),res);
printf("%ls", buf);
Here fdopen() is successful and am getting a pointer to stream but fgetws() is reading nothing. I am assuming that passing "ccs=UTF-16LE" is not working here.
# gcc -g fgetw.c
# gdb ./a.out
GNU gdb Red Hat Linux (6.5-16.el5rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/i686/nosegneg/libthread_db.so.1".
(gdb) set args < out.txt
(gdb) break main
Breakpoint 1 at 0x8048488: file fgetw.c, line 8.
(gdb) run
Starting program: /home/icu13/a.out < out.txt
Breakpoint 1, main () at fgetw.c:8
8 wchar_t buf[512]={0};
(gdb) n
9 FILE *res=NULL;
(gdb) n
20 res=fdopen(fileno(stdin),"r,ccs=UTF16-LE");
(gdb) print fileno(stdin)
$1 = 0
(gdb) n
21 fgetws(buf, countof(buf),res);
(gdb) print res
$2 = (FILE *) 0x93ee008
(gdb) n
22 printf("%ls", buf);
(gdb) x/10b buf
0xbfe5a790: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xbfe5a798: 0x00 0x00
(gdb) n
Later i had tried fopen() instead of fdopen() and passing "/dev/stdin" as filename to fopen(). I am successful with this. I am able to read UTF16 data from STDIN.
wchar_t buf[512]={0};
FILE *res=NULL;
res=fopen("/dev/stdin","r,ccs=UTF-16LE");
fgetws(buf, countof(buf),res);
printf("%ls", buf);
But the problem is, I want this code to be ported to other two platforms AIX & HPUX. These platforms doesn't have "/dev/stdin" device file.
Due to above, i have reverted back my decision of using fopen() and want to use fdopen().
So please help me with
1) When fdopen() is used, why fgetws() is not reading? Do i am missing anything? please let me know how to use fdopen().
2) AS it is not related to this forum. Please tell me if anyone knows the location for device file for STDIN in AIX and HPUX platforms. So that i can use fopen().
Thanks in Advance.
regards,
Srikrishna Erra.
- 10-20-2009 #2
countof(...)? Surely you mean sizeof(...)?
Linux user #126863 - see http://linuxcounter.net/
- 10-21-2009 #3Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Hi,
It is sizeof() only ...
Here is my C program.
#include <stdio.h>
#include <wchar.h>
#include <unistd.h>
#include <errno.h>
int main()
{
wchar_t buf[512]={0};
FILE* res=NULL;
res=fdopen(0,"r,ccs=UTF-16LE");
/* res=fopen("/dev/stdin","r,ccs=UTF-16LE"); */
perror("File");
fgetws(buf,sizeof(buf),res);
printf("%s",buf);
return(0);
}
regards,
Srikrishna Erra.
- 10-21-2009 #4
Are you sure res is being set to a valid file pointer? You're supposed to be using an existing stream with this call - take a look here. It might even be that the ccs statement isn't compatible with fdopen(...).
Try checking that res is valid before you use it in the fgetws(...) call.Linux user #126863 - see http://linuxcounter.net/
- 10-21-2009 #5Just Joined!
- Join Date
- Oct 2009
- Posts
- 4
Hi,
Are you sure res is being set to a valid file pointer?
>> Yes. "res" is set to valid pointer.
You're supposed to be using an existing stream with this call - take a look here.
>> fdopen() needs a opened file descriptor and "0" here indicates STDIN for >>which input stream automatically opened. I have also tried
res=fdopen(fileno(stdin),"r,ccs=UTF-16LE");
but no use.
>>It might even be that the ccs statement isn't compatible with fdopen(...).
But man pages says
The mode argument is a character string having one of the following values:
r or rb
Open a file for reading.
w or wb
Open a file for writing.
a or ab
Open a file for writing at end-of-file.
r+ or rb+ or r+b
Open a file for update (reading and writing).
w+ or wb+ or w+b
Open a file for update (reading and writing).
a+ or ab+ or a+b
Open a file for update (reading and writing) at end-of-file.
The meaning of these flags is exactly as specified in fopen(), except that modes beginning with w shall not cause truncation of the file.
So i though fdopen() could accept ccs statement.
Here is the Debug output
************************************************** **************
(gdb) set args < out.txt
(gdb) break main
Breakpoint 1 at 0x80484e8: file fgetw.c, line 8.
(gdb) run
Starting program: /home/icu13/a.out < out.txt
Breakpoint 1, main () at fgetw.c:8
8 wchar_t buf[512]={0};
(gdb) n
9 FILE* res=NULL;
(gdb) n
13 pipe(mypipe);
(gdb) n
14 res=fdopen(fileno(stdin),"r,ccs=UTF-16LE");
(gdb) p res
$1 = (FILE *) 0x0
(gdb) n
16 perror("File");
(gdb) p res
$2 = (FILE *) 0x996d008
(gdb) p *res
$3 = {_flags = -72539000, _IO_read_ptr = 0xb7fdc000 "", _IO_read_end = 0xb7fdc000 "", _IO_read_base = 0xb7fdc000 "",
_IO_write_base = 0xb7fdc000 "", _IO_write_ptr = 0xb7fdc000 "", _IO_write_end = 0xb7fdc000 "",
_IO_buf_base = 0xb7fdc000 "", _IO_buf_end = 0xb7fdd000 "<Ðý·$»\027D", _IO_save_base = 0x0, _IO_backup_base = 0x0,
_IO_save_end = 0x0, _markers = 0x0, _chain = 0x442be560, _fileno = 0, _flags2 = 0, _old_offset = 0, _cur_column = 0,
_vtable_offset = 0 '\0', _shortbuf = "", _lock = 0x996d0a0, _offset = 0, __pad1 = 0x0, __pad2 = 0x996d0ac, __pad3 = 0x0,
__pad4 = 0x0, __pad5 = 0, _mode = 0, _unused2 = '\0' <repeats 39 times>}
(gdb) n
File: Success
17 fgetws(buf,sizeof(buf),res);
(gdb) x/10b buf
0xbf8d7210: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xbf8d7218: 0x00 0x00
(gdb) n
18 printf("%s",buf);
(gdb) p *res
$4 = {_flags = -72538968, _IO_read_ptr = 0xb7fdc000 "ÿþ/", _IO_read_end = 0xb7fdc068 "", _IO_read_base = 0xb7fdc000 "ÿþ/",
_IO_write_base = 0xb7fdc000 "ÿþ/", _IO_write_ptr = 0xb7fdc000 "ÿþ/", _IO_write_end = 0xb7fdc000 "ÿþ/",
_IO_buf_base = 0xb7fdc000 "ÿþ/", _IO_buf_end = 0xb7fdd000 "<Ðý·$»\027D", _IO_save_base = 0x0, _IO_backup_base = 0x0,
_IO_save_end = 0x0, _markers = 0x0, _chain = 0x442be560, _fileno = 0, _flags2 = 0, _old_offset = 0, _cur_column = 0,
_vtable_offset = 0 '\0', _shortbuf = "", _lock = 0x996d0a0, _offset = 104, __pad1 = 0x996d0e8, __pad2 = 0x996d0ac,
__pad3 = 0x0, __pad4 = 0x0, __pad5 = 0, _mode = 1, _unused2 = '\0' <repeats 39 times>}
(gdb) x/10b buf
0xbf8d7210: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xbf8d7218: 0x00 0x00
(gdb)
0xbf8d721a: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0xbf8d7222: 0x00 0x00
(gdb)
************************************************** ***************
Thanks in Advance.
regards,
Srikrishna Erra.


Reply With Quote