Sponsored Content
Top Forums Programming Strange character added when reading to buffer with length of 12 Post 302265276 by tazan_007 on Saturday 6th of December 2008 09:37:55 AM
Old 12-06-2008
Strange character added when reading to buffer with length of 12

Hi all,
I got trouble with reading and writing ( to standard input/output, file, socket whatever...).
I will briefly describe it by giving this example. I want to read a long string from keyboard but i don't know how long it is b4. So i need to know the number of character i will read first. Then i initialize my buffer to fit my desirable string and read this string from standard input.
However, everything is ok until i try to read 12 characters from keyboard. A weird character is automatically added : "ñ". And when i check the string length, it is 15. I don't know why because with other cases, there isn't some things like this happened.
Just a sample code:

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
main(){
int fd;
char *str;
int a;
char enter[1];
scanf("%d",&a);
str = malloc(a*sizeof(char));
read(0,str,a);
printf("%s ----- %d",str,strlen(str));
}


I have searched but still found nothing related to this.

Thanks
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

there was a strange character(^M) been added automatically in UNIX

I used Notepad to compile my txt file and then I transferred this file to UNIX. When I use vi to open the file, I found that at the end of each line there was a "^M" character. In the original txt file there was not this character. Why this character would been added automatically in UNIX? (4 Replies)
Discussion started by: Jasmine
4 Replies

2. UNIX for Dummies Questions & Answers

Reading character by character - BASH

Hello every one and thanks in advance for the time you will take to think about my problem. I would like to know if it's possible (in BASH) to read a text file character after character. Exactly this is what I would like to do : Txt file : ATGCAGTTCATTGCCAAA...... (~2.5 millions... (3 Replies)
Discussion started by: sluvah
3 Replies

3. Shell Programming and Scripting

awk to txt provide strange character

Hi all, I executed this script top -n 1 | awk '{ if (NR==3) print $2 }'and it will give me in console '31.6%us,'however, if I put the same script in txt file top -n 1 | awk '{ if (NR==3) print $2 }' >> test.txtwhen I open the test.txt I see 31.6%ESC(BESCI just actually need the 31.6 % to be... (1 Reply)
Discussion started by: peuceul
1 Replies

4. Shell Programming and Scripting

Problem with character by character reading

Hi friend, i have the following problem: when i am writting the below command on the command prompt , its working. while read -n 1 ch; do echo "$ch" ; echo "$ch" ; done<file_name.out. but when i am executing it after saving it in a ksh file, its not working. Please helppppppppp .. thankss... (18 Replies)
Discussion started by: neelmani
18 Replies

5. Shell Programming and Scripting

Filtering escape character from expect buffer

I have written an application in Tcl-Expect which spawns minicom and sends and receives data via the serial port. Sometimes i see that the application receives control characters along with human readable data. A search on the internet tells me that the control characters are nothing but the VT... (0 Replies)
Discussion started by: cityprince143
0 Replies

6. Shell Programming and Scripting

Preserve spaces while reading character character

Hi All, I am trying to read a file character by character, #!/bin/bash while read -n1 char; do echo -e "$char\c" done < /home/shak/testprogram/words Newyork is a very good city. Newyorkisaverygoodcityforliving I need to preserve the spaces as thats an... (3 Replies)
Discussion started by: Kingcobra
3 Replies

7. Shell Programming and Scripting

Warning while sorting : A newline character was added to the end of file

Hi, I am trying to sort a csv file which has say 10 lines each line having a row size that is upto 30183 no. of COLUMNS (Row length = 30183). There is a LINE FEED (LF) at the end of each line. When I try to sort this file say, based on the second FIELD using the below command, sort -t ',' +1... (5 Replies)
Discussion started by: DHeisenberg
5 Replies

8. Shell Programming and Scripting

Get column number with the same character length

so, i want to tail about the last 3000 lines of a log file and find the column that has the same number of characters across all 3000 lines (or most of the 3000 lines) tail -3000 logfile | while read line do ColumnCharCount=$(for eachwordorwhatever in "${echo $line}" do ... (8 Replies)
Discussion started by: SkySmart
8 Replies

9. Shell Programming and Scripting

Add string based on character length

Good day, I am a newbie here and thanks for accepting me I have a task to modify input data where my input data looks like 123|34567|CHINE 1|23|INDIA 34512|21|USA 104|901|INDIASee that my input has two columns with different character length but max length is 5 and minimum length is 0 which... (1 Reply)
Discussion started by: fastlearner
1 Replies

10. What is on Your Mind?

Updated Forum Search Index Min Word Length to 2 Chars and Added Quick Search Bar

Today I changed the forum mysql database to permit 2 letter searches: ft_min_word_len=2 I rebuilt the mysql search indexes as well. Then, I added a "quick search bar" at the top of each page. I have tested this and two letter searches are working; but it's not perfect,... (1 Reply)
Discussion started by: Neo
1 Replies
getnstr(3XCURSES)					  X/Open Curses Library Functions					 getnstr(3XCURSES)

NAME
getnstr, getstr, mvgetnstr, mvgetstr, mvwgetnstr, mvwgetstr, wgetnstr, wgetstr - get a multibyte character string from terminal SYNOPSIS
cc [ flag... ] file... -I /usr/xpg4/include -L /usr/xpg4/lib -R /usr/xpg4/lib -lcurses [ library... ] c89 [ flag... ] file... -lcurses [ library... ] #include <curses.h> int getnstr(char *str, int n); int getstr(char *str); int mvgetnstr(int y, int x, char *str, int n); int mvgetstr(int y, int x, char *str); int mvwgetnstr(WINDOW *win, int y, int x, char *str, int n); int mvwgetstr(WINDOW *win, int y, int x, char *str); int wgetnstr(WINDOW *win, char *str, int n); int wgetstr(WINDOW *win, char *str); DESCRIPTION
The getstr() and wgetstr() functions get a character string from the terminal associated with the window stdscr or window win, respec- tively. The mvgetstr() and mvwgetstr() functions move the cursor to the position specified in stdscr or win, respectively, then get a character string. These functions call wgetch(3XCURSES) and place each received character in str until a newline is received, which is also placed in str. The erase and kill characters set by the user are processed. The getnstr(), mvgetnstr(), mvwgetnstr() and wgetnstr() functions read at most n characters. These functions are used to prevent over- flowing the input buffer. The getnstr(), wgetnstr(), mvgetnstr(), and mvwgetnstr() functions only return complete multibyte characters. If the area pointed to by str is not large enough to hold at least one character, these functions fail. PARAMETERS
str Is a pointer to the area where the character string is to be placed. n Is the maximum number of characters to read from input. y Is the y (row) coordinate of starting position of character string to be read. x Is the x (column) coordinate of starting position of character string to be read. win Points to the window associated with the terminal from which the character is to be read. RETURN VALUES
On success, these functions return OK. Otherwise, they return ERR. ERRORS
None. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
getch(3XCURSES), libcurses(3XCURSES), attributes(5), standards(5) SunOS 5.10 5 Jun 2002 getnstr(3XCURSES)
All times are GMT -4. The time now is 12:54 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy