How to read extended ASCII characters from stdin?

Hi,
I want to read extended ASCII characters from keyboard using c language on unix/linux. How to read extended characters from keyboard or by copy-paste in terminal irrespective of locale set in the system. I want to read the input characters from keyboard, store it in an array or some local variable and calculate exactly how many bytes of data read. Using wchar_t and it's library functions is not helping correctly.

Can anyone please give sample C code to process such request?

Thanks for your valuable replies.
Sanzee

Show us the C code you have tried (in CODE tags).

In the below code I tried to read the entered text in a buffer and showing the length of data read from stdin.

#include <stdio.h>
#include <wchar.h>
#include <stdlib.h>
#include <locale.h>

#define BUFSIZE  256

wchar_t* readFromStdin(const wchar_t *);

wchar_t m_buf[BUFSIZE];

int main()
{
        const wchar_t *pwch;
        char *pbuf = NULL;
        setlocale(LC_ALL, "");
        printf("Enter the word having extended characters:");
        pwch = readFromStdin(L"Word: ");
        printf("The Wide char string is: %S\n", pwch);
        wcstombs(pbuf, pwch, wcslen(pwch)+1); // I have to convert the wchar_t data to char* for further processing

return 0;
}

wchar_t *readFromStdin(const wchar_t *ptr) // Though this function is not doing what its name suggests
{
        unsigned int size;
        scanf("%ls",m_buf);
        size = wcslen(m_buf);

        printf("Size of buf = %d\n", size);
        return (m_buf);

}

Whether this code will properly read any word having extended ascii characters and be able to show the desired length of the word.?

Thanks,

A few comments that may help you:

  1. The argument you're passing to readFromString() is never referenced.
  2. You can't use scanf() to read a wide character string; use wscanf() instead. (Or preferably, to prevent input buffer overflows, use fgetws() .)
  3. You can't print a wide character string using printf() ; use wprintf() instead.
  4. What are you trying to determine with the variable size ? You're currently calculating the number of wide characters read (not counting the terminating wide null character).
  5. Why bother calling wcstombs() ? You exit before you do anything with the the multi-byte string it produces? (Of course, it is likely to core dump since you're telling it to store bytes in the array pointed to by a NULL pointer.)
  6. The 1st argument to wcstombs() needs to be a pointer to an array of type char where the multi-byte string produced by converting the wide character string named by the 2nd argument will be placed. The 3rd argument to wcstombs() needs to be the size of that array(in bytes); not the number of wide characters contained in the wide character string pointed to by the 2nd argument.