snprintf
От: Alexander Zobov Россия  
Дата: 02.07.03 15:59
Оценка:
добавит ли snprintf нулевой символ в конце строки, если форматная стока больше или равна параметру "n"?

например strncy не добавит...
Re: snprintf
От: Alexander Zobov Россия  
Дата: 02.07.03 16:13
Оценка:
AZ>например strncpy не добавит...

нашел например следущее в интернете:
http://mail.gnu.org/archive/html/bug-glibc/2001-11/msg00108.html

If n is zero, nothing is written, and s may be a
null pointer. Otherwise, output characters beyond the n-1st are
discarded rather than being written to the array, and a null
character is written at the end of the characters actually written
into the array.

Все бы было хорошо не будь все так плохо. Некоторые компиляторы этот 0
в конец строки не добавляют...
Re: snprintf
От: Павел Кузнецов  
Дата: 02.07.03 16:44
Оценка: 5 (2)
Здравствуйте, Alexander Zobov, Вы писали:

AZ> добавит ли snprintf нулевой символ в конце строки, если форматная

AZ> стока больше или равна параметру "n"?

До недавнего времени функция snprintf не входила в стандарты C или C++. Фактически
это означало, что разработчики RTL были вольны делать так, как им вздумается.
В лучшем случае можно было расчитывать на соответствие стандарту POSIX.

В C99 была добавлена спецификация snprintf:

7.19.6.5 The snprintf function
Synopsis
1

#include <stdio.h>
int snprintf(char * restrict s, size_t n,
const char * restrict format, ...);

Description
2
The snprintf function is equivalent to fprintf, except that the output is written into
an array (specified by argument s) rather than to a stream. If n is zero, nothing is written,
and s may be a null pointer. Otherwise, output characters beyond the n-1st are
discarded rather than being written to the array, and a null character is written at the end
of the characters actually written into the array. If copying takes place between objects
that overlap, the behavior is undefined.
Returns
3
The snprintf function returns the number of characters that would have been written
had n been sufficiently large, not counting the terminating null character, or a negative
value if an encoding error occurred. Thus, the null-terminated output has been completely
written if and only if the returned value is nonnegative and less than n.


Можно надеяться, что следующая версия C++ включит данное определение, соответственно, можно
ожидать, что потихоньку разработчики RTL подтянут свою реализацию к данной спецификации.
Posted via RSDN NNTP Server 1.6 RC1
Легче одурачить людей, чем убедить их в том, что они одурачены. — Марк Твен
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.