我正在尝试创建一个文本文件并将其写入,但是当我打开该文本文件时,输出结果错误。
样品:
Please enter number of students?
2
Please Enter details of the Students in the Following sequence: Name Age GPA
alex 18 3.2
dan 21 3.5
输出:
tttttttttkkkkkkkkksssssssssss
代码:
#include "stdafx.h"
#include<stdio.h>
#include<Windows.h>
struct Student {
WCHAR name[20];
int age;
float gpa;
};
int _tmain(int argc, _TCHAR* argv[])
{
struct Student St;
int NumOfStu;
printf("Please enter number of students\n");
scanf("%d" , &NumOfStu);
HANDLE f = CreateFile(L"d:\\SPR2.txt" , GENERIC_WRITE , 0 , NULL , CREATE_ALWAYS , FILE_ATTRIBUTE_NORMAL , NULL);
if(f == INVALID_HANDLE_VALUE)
{
printf("Could not Create The File\n");
return 0;
}
printf("Please Enter details of the Sutdents in the Following sequence: Name Age GPA\n");
DWORD actual;
for(int i=0 ;i<NumOfStu;i++)
{
scanf("%s %d %f" , &St.name , &St.age , &St.gpa );
WriteFile(f , , sizeof(struct Student) , &actual , NULL);
}
CloseHandle(f);
return 0;
}
最佳答案
scanf("%s %d %f" , &St.name , &St.age , &St.gpa );
name
已经衰减到一个指针,您不应将其与&
一起使用。而且,它是一个宽字符串,scanf并不期望。这样你就会腐败。