Strange character added when reading to buffer with length of 12


 
Thread Tools Search this Thread
Top Forums Programming Strange character added when reading to buffer with length of 12
# 1  
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
# 2  
Old 12-06-2008
First off check how big an int can be on your system...doubt if it can hold a 12 digit number unless it's a long long type. Even if that's alright you are most likely hitting the data segment limit for malloc'ing such a huge chunk of memory. Check its return value before moving onto the next step.
Code:
long long a;
char *str;
a = 123456789012; / a is a 12 digit number */
if ((str = malloc(a * sizeof(char))) == NULL) { /* here you are trying to allocate a huge chunk of memory */
    perror("malloc error");
    exit(errno);
}

# 3  
Old 12-07-2008
Hi,
My example is:

int a = 12;
char *str;
str = malloc(2*sizeof(char));
read(0,str,a);
printf("%s %d",str,strlen(str));

if you run the program and type: "0123456789012" means 12 characters or whatever. What you see in the screen after printf function is: 0123456789012ñ 15
it does not happen with other value of a but only 12.
# 4  
Old 12-07-2008
To allocate memory for a string of 12 chars you have to allocate memory for 12 chars + 1 for the '\0' char to terminate the string.

Regards
# 5  
Old 12-08-2008
You are either allocating too much or too little. When you need memory for "123456789012" you need to malloc memory for 13 (12 char + 1 NULL) characters otherwise you see junk concatenated in the output. Here you know there will be 12 characters so why not do...
Code:
a = 12;
str = malloc(a*sizeof(char)+1);

# 6  
Old 12-12-2008
Thanks for all,
What i need is "why".
# 7  
Old 12-13-2008
[U16511@maildev c_cpp]$ ./a.out
0123456789015
0012345678901ñ 15[U16511@maildev c_cpp]$ 5

It means nothing related to a string ending 12.

It is a memory managment issue.

Your question is "why'. I will try to answer.

In the input string there are 13 characters, but str is alloted for 12 characters. So where will keep the last ASCII character reading into ? Where will keep the charcter for string end '\0'.


So if you alllocate a location for 12, maximum you can read 11 characters. Since the 12 th charcter will be used by the '\0'.

Then next question why the wiered character is coming out through printf ? I think printf is trying to print a nonprintable charcter while searching for '\0' ; since in its format string, the first one is "%s". It means printf should print a string. By definition string should have a charcter '\0' at end . If it is not there we cannot assure what it wil print. The decision is not able to decide even by using Heisenberg's Uncertainity principle. So believe in Gods grace!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question