The fwscanf_s function doesn't read properly

Keitel 61 Reputation points
2024-04-21T11:12:33.9666667+00:00

I have a program in C that have been working fine since I compiled it in an earlier version of Visual Express. Now that I have been compiling it in Visual Studio 2022, the fwscanf_s function doesn't work properly. It reads text and integers. The integers are displayed properly, but not the text variable.

struct svaras
	{
		TCHAR name[20];
		int t;
		int rk[2];	
		int rk2[2];
		int rs[2];
		int rs2[2];
		
	};
	struct svaras scales[150];

i = 0;

if(_waccess_s(L"shrutilist.dat", 0) != -1){
	Receivedata = GetModuleFileName(NULL, Buffer, MAX_PATH);		
	PathCchRemoveFileSpec(Buffer, MAX_PATH);				
	PathCchAppend(Buffer, MAX_PATH, L"shrutilist.dat");				
	err = _wfopen_s(&shrutiliste, Buffer, L"r");

while (i < 60) {

    test = fwscanf_s(shrutiliste, "%20ls  %i %i %i  %i %i \n",  					

    scales[i].name, 
    &scales[i].t,
	&scales[i].rk[0], &scales[i].rk[1],
	&scales[i].rk2[0], &scales[i].rk2[1],
	&scales[i].rs[0], &scales[i].rs[1],
	&scales[i].rs2[0], &scales[i].rs2[1]);

    ++1;
}
fclose(shrutiliste);
}

The variable scales[i].name shows this in the debugger:

{name=0x006fb9fc L"쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌쳌... t=-858993460 rk=0x006fba28 {-858993460, -858993460}

While when it is displayed in a list box, it only shows this: 쳌쳌쳌쳌쳌쳌쳌쳌쳌

The integers are displayed correctly in the list box.

Thanks in advance.

Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
Developer technologies | C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
{count} votes

Answer accepted by question author
  1. Minxin Yu 13,511 Reputation points Microsoft External Staff
    2024-04-22T07:17:23.9033333+00:00

    Hi, @Keitel

    Use L"" in fwscanf_s(shrutiliste, "%20ls %i %i %i %i %i \n",

    fwscanf_s(shrutiliste, L"%20ls %i %i %i %i %i \n",

    And as Viorel said, the format should be
    L"%20ls %i ...", scales[i].name, 20, &scales[i].t,...

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 82,771 Reputation points Volunteer Moderator
    2024-04-21T15:38:42.13+00:00

    Never mind

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.