我需要的是:
1)从sscanf读取字符串
2)测量sscanf的“%n”处理序列的长度
3)接受format
和上面的其他论点(无法控制)
有办法怎么做?
size_t read = 0; //Accumulator of the length of processed characters
void readfn (char* source_string, char* fmt, va_list args)
{
int length;
size_t fmtlen = strlen(fmt);
char* fmt_and_lenght = (char*)realloc(fmt, fmtlen + 3);
fmt_and_length[fmtlen] = '%';
fmt_and_length[fmtlen + 1] = 'n';
fmt_and_length[fmtlen + 2] = '\0';
va_list args_and_length = va_append(args, length); //here is the problem, i need to add &length to the list (i dont care if the list is created from scretch
vsscanf(source_str, fmt_and_length, args_and_length); //here i finally capture the length of processed string
read += length; //and i do whatever i wanted to do with it
}
即使fmt没有包含“%n”并且参数列表之前没有捕获它,它也只计算消耗的字符数?
编辑:如果有vsnscanf或它应该具有什么名称肯定会更好,这将获得已处理的字符数。但是我通过未转义的%分割格式字符串来解决这个问题。如果没有跟着*我取一个参数并迭代地处理所有这些参数,每次我添加“%n”,最后总结长度。