How to read extended ASCII characters from stdin?


 
Thread Tools Search this Thread
Top Forums Programming How to read extended ASCII characters from stdin?
# 1  
Old 01-17-2015
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

Last edited by sanzee007; 01-17-2015 at 01:43 PM.. Reason: Typo error
# 2  
Old 01-17-2015
Show us the C code you have tried (in CODE tags).
# 3  
Old 01-19-2015
In the below code I tried to read the entered text in a buffer and showing the length of data read from stdin.

Code:
#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,
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line input, output, and code samples.

Last edited by Don Cragun; 01-19-2015 at 05:24 AM.. Reason: Change ICODE tags to CODE tags for multi-line data.
# 4  
Old 01-19-2015
Quote:
Originally Posted by sanzee007
In the below code I tried to read the entered text in a buffer and showing the length of data read from stdin.

Code:
#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,
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for multi-line input, output, and code samples.
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print byte position of extended ascii character

Hello, I am on AIX. When I encounter extended ascii characters and special characters on a file I need to print.. Byte position, actual character and line number. Is there a simple command that can give me the above result ? Thanks in advance (38 Replies)
Discussion started by: rosebud123
38 Replies

2. Shell Programming and Scripting

Extended ASCII Characters keep on getting reintroduced to text files

I am working with a log file that I am trying to clean up by removing non-English ASCII characters. I am using Bash via Cygwin on Windows. Before I start I set: export LC_ALL=C I clean it up by removing all non-English ASCII characters with the following command; grep -v $''... (4 Replies)
Discussion started by: lewk
4 Replies

3. Shell Programming and Scripting

Removal Extended ASCII using awk

Hi All, I am trying to remove (SELECTIVE - passed as argument) Extended ASCII using Awk based on adhoc basis. Can you please let me know how to do it. I have to implement this using awk only. Thanks & Regads (14 Replies)
Discussion started by: tostay2003
14 Replies

4. Shell Programming and Scripting

Search and Replace Extended Ascii Characters

We are getting extended Ascii characters in the input file and my requirement is to search and replace them with a space. I am using the following command LANG=C sed -e 's// /g' It is doing a good job, but in some cases it is replacing the extended characters with two spaces. So my input... (12 Replies)
Discussion started by: ysvsr1
12 Replies

5. Shell Programming and Scripting

Identify extended ascii characters in a file

Hi, Is there a way to identify the lines in a file having extended ascii characters and display the same? For instance I have a file abc.txt having below data aaa|bbb|111|This is first line aaa|bbb|222|This is secõnd line aaa|bbb|333|This is third line aaa|bbb|444|This is foùrth line... (3 Replies)
Discussion started by: decci_7
3 Replies

6. AIX

Printing extended ASCII

Hi All, I'm trying to send extended ascii characters to my HP2055 as part of PCL printer control codes. What I want to do is select a bar code font, print the bar code and reset the printer to the default font. Selecting the bar code font works good. Printing the bar code goes almost ok too. ... (5 Replies)
Discussion started by: petervg
5 Replies

7. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

8. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

9. Shell Programming and Scripting

extended ascii problem

hi i would like to check text files if they contain extended ascii characters within or not. i really dont have any idea how to start your kind help would be very much appreciated thanks. (7 Replies)
Discussion started by: smooth
7 Replies

10. Programming

Extended ascii

Hi all, I would like to change the extended ascii code ( 128 - 255). I tried to change LC_ALL and LANG in current session ( values from locale -a) and for no good. Thanks. (0 Replies)
Discussion started by: avis
0 Replies
Login or Register to Ask a Question