swscanf,swscanf_sc

http://www.itjxue.com  2023-01-08 15:51  来源:未知  点击次数: 

关于sscanf的问题

请检查你的s[i]是什么内容,还有第二个sscanf少了一个d,是%d!

================

"%fl" 你写反了,是lf( long float)

sscanf(...,"%lf\t",d);可以读

具体你看下面的msdn吧

Read formatted data from a string. These functions are deprecated because more secure versions are available; see sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l.

int sscanf(

const char *buffer,

const char *format [,

argument ] ...

);

int _sscanf_l(

const char *buffer,

const char *format,

locale_t locale [,

argument ] ...

);

int swscanf(

const wchar_t *buffer,

const wchar_t *format [,

argument ] ...

);

int _swscanf_l(

const wchar_t *buffer,

const wchar_t *format,

locale_t locale [,

argument ] ...

);

Parameters

buffer

Stored data

format

Format-control string. For more information, see Format Specifications.

argument

Optional arguments

locale

The locale to use

Return Value

Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.

If buffer or format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

For information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

Remarks

The sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function. If copying takes place between strings that overlap, the behavior is undefined.

Security Note

When reading a string with sscanf, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, improperly formatted input can easily cause a buffer overrun.

swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf does not handle multibyte hexadecimal characters. swscanf does not handle Unicode full-width hexadecimal or "compatibility zone" characters. Otherwise, swscanf and sscanf behave identically.

The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE _MBCS not defined _MBCS defined _UNICODE defined

_stscanf

sscanf

sscanf

swscanf

_stscanf_l

_sscanf_l

_sscanf_l

_swscanf_l

Requirements

Routine Required header Compatibility

sscanf, _sscanf_l

stdio.h

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

swscanf, _swscanf_l

stdio.h or wchar.h

ANSI, Windows 95, Windows 98, Windows 98 Second Edition, Windows Millennium Edition, Windows NT 4.0, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003

For additional compatibility information, see Compatibility in the Introduction.

Example

Copy Code

// crt_sscanf.c

// compile with: /W1

// This program uses sscanf to read data items

// from a string named tokenstring, then displays them.

#include stdio.h

int main( void )

{

char tokenstring[] = "15 12 14...";

char s[81];

char c;

int i;

float fp;

// Input various data from tokenstring:

// max 80 character string:

sscanf( tokenstring, "%80s", s ); // C4996

sscanf( tokenstring, "%c", c ); // C4996

sscanf( tokenstring, "%d", i ); // C4996

sscanf( tokenstring, "%f", fp ); // C4996

// Note: sscanf is deprecated; consider using sscanf_s instead

// Output the data read

printf( "String = %s\n", s );

printf( "Character = %c\n", c );

printf( "Integer: = %d\n", i );

printf( "Real: = %f\n", fp );

}

Output

String = 15

Character = 1

Integer: = 15

Real: = 15.000000

.NET Framework Equivalent

See Parse methods, such as System::Double::Parse.

See Also

Reference

Stream I/O

fscanf, _fscanf_l, fwscanf, _fwscanf_l

scanf, _scanf_l, wscanf, _wscanf_l

sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l

_snprintf, _snprintf_l, _snwprintf, _snwprintf_l

地址:

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_vccrt/html/c2dcf0d2-9798-499f-a4a8-06f7e2b9a80c.htm

MFC读取文件的问题(主要是CString问题)

你既然都用了字符串泛型宏_T()了

那就用_stscanf_s吧,这样不管是UNICODE还是多字节字符集都可以

还有你scanf里用CString是肯定不行的 scanf是向一个缓冲区中填东西

CString在空构造下显然是不会分配内存的,内部的指针是NULL,属于禁止访问内存区,

肯定报Access violation错误;

应该用TCHAR buff[21]

你可以这样:

CString?str;

int?tempint[10];

CString?tempstr[5];

TCHAR?buff[21];

for(?int?i?=?0?;?i??10?;?i?+=?2?)

{

????tempfile.ReadString(?str?);

????_stscanf_s(?str?,?_T("%6d%6d%20s"),?tempint[i],?tempint[i+1],?buff,?_countof(buff));

????tempstr[i/2]?=?buff;

}

C语言读取文本文件中的科学数据

首先,i的初始值要赋值成0,i++的位置不对,会影响到后面的printf语句,然后请记住ccs的printf遇到\n才能完成输出,while循环修改一下:

i=0;

while

(

!feof(fp)

)

{

fscanf(fp,

"%lf",

keys[i]);

printf("%lf\n",keys[i]);

i++;

}

(责任编辑:IT教学网)

更多

推荐java认证文章