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!!
10 More Discussions You Might Find Interesting
1. UNIX for Dummies Questions & Answers
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
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
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
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
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
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
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
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
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?
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
LEARN ABOUT ULTRIX
sprintf
printf(3int) printf(3int)
Name
printf, fprintf, sprintf - print formatted output
Syntax
#include <stdio.h>
int printf ( format [, arg ] ... )
char *format;
int fprintf ( stream, format [, arg ] ... )
FILE *stream;
char *format;
int sprintf ( s, format [, arg ] ... )
char *s, *format;
Description
The international functions and are similar to the standard I/O functions. The difference is that the international functions allow you to
use the %digit$ conversion character in place of the % character you use in the standard I/O functions. The digit is a decimal digit n from
1 to 9. The international functions apply conversions to the n th argument in the argument list, rather than to the next unused argument.
You can use the % conversion character in the international functions. However, you cannot mix the % conversion character with the %digit$
conversion character in a single call.
You can indicate a field width or precision by an asterisk (*) instead of a digit string in format strings containing the % conversion
character. If you use an asterisk, you can supply an integer arg that specifies the field width or precision. In format strings containing
the %digit$ conversion character, you can indicate field width or precision by the sequence *digit$. You use a decimal digit from 1 to 9
to indicate which argument contains an integer that specifies the field width or precision.
The conversion characters and their meanings are identical to
You must use each digit argument at least once.
In all cases, the radix character uses is defined by the last successful call to category If category has not been called successfully or
if the radix character is undefined, the radix character defaults to a period (.).
International Environment
LC_NUMERIC If this environment is set and valid, uses the international language database named in the definition to determine radix
character rules.
LANG If this environment variable is set and valid uses the international language database named in the definition to determine
collation and character classification rules. If is defined, its definition supercedes the definition of LANG.
Examples
The following example illustrates using an argument to specify field width:
printf ("%1$d:%2$.*3$d:%4$.*3$d
",
hour, min, precision, sec);
The format string *3$ indicates that the third argument, which is named precision, contains the integer field width specification.
To print the language independent date and time format use the following statement:
printf (format, weekday, month, day, hour, min);
For American use, format could be a pointer to the following string:
"%1$s, %2$s %3$d, %4$d:%5$.2d
"
This string gives the following date format:
Sunday, July 3, 10:02
For use in a German environment, format could be a pointer to the following string:
"%1$s, %3$d. %2$s, %4$d:%5$.2d
"
This string gives the following date format:
Sonntag, 3. Juli, 10:02
Return Values
and return zero for success and EOF for failure. The subroutine returns its first argument for success and EOF for failure.
In the System V and POSIX environments, and return the number of characters transmitted for success. The function ignores the null termi-
nator ( ) when calculating the number of characters transmitted. If an output error occurs, these routines return a negative value.
See Also
intro(3int), setlocale(3), scanf(3int), printf(3s), putc(3s), scanf(3s), stdio(3s)
Guide to Developing International Software
printf(3int)