因此,我正在尝试使此代码正常工作,这给了我一个错误。我试图对其进行搜索,但找不到任何使其工作的方法。
错误(活动)E0513无法将类型“ errno_t”的值分配给类型“ FILE *”的实体 我得到了那个错误,还有这个
错误C2440'=':无法从'errno_t'转换为'FILE *'ch11Program
有什么想法在这里做什么?
FILE *cfPtr;
cfPtr = fopen_s(&cfPtr,"client.txt", "w");
if ( cfPtr == NULL) {
puts("File could not be opened");
}
else {
puts("Enter the account, name, and balance.");
puts("Enter E0F to end input.");
printf("%s", "? ");
unsigned int account;
char name[30];
double balance;
scanf_s("%d%29s%lf", &account, name, &balance);
while (!feof(stdin)) {
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
printf("%s", "? ");
scanf_s("%d%29s%lf", &account, name, &balance);
}
fclose(cfPtr);
}
答案 0 :(得分:2)
您不能将fopen_s()分配给cfPtr,因为它们的类型不同。而是尝试以下方法:
FILE *cfPtr;
errno_t err;
err = fopen_s(&cfPtr,"client.txt", "w");
if ( err == NULL) {
puts("File could not be opened");
}
else {
puts("Enter the account, name, and balance.");
puts("Enter E0F to end input.");
printf("%s", "? ");
unsigned int account;
char name[30];
double balance;
scanf_s("%d%29s%lf", &account, name, &balance);
while (!feof(stdin)) {
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
printf("%s", "? ");
scanf_s("%d%29s%lf", &account, name, &balance);
}
fclose(cfPtr);
}