|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Levi_Marvin 于 2022-4-1 17:03 编辑
萌新求助,为什么 fgetws 一直读取不到呀,errno是22
错误位置在 student.c 的 DATABASE_STUDENT_del() 函数中 while 里的 fgetws()
main.cint main() {
if (setlocale(LC_ALL, "zh_CN.utf8") == NULL) {
return 2;
}
int select = 0;
DATABASE *db = malloc(sizeof(DATABASE));
STUDENT *st = malloc(sizeof(STUDENT));
STUDENT_new(st);
wprintf_s(TEXT_MENU_WELCOME);
wprintf_s(L"请输入你的选择:");
wscanf_s(L"%d", &select);
switch (select) {
case 0:
{
break;
}
case 1:
{
wchar_t *input_no = (wchar_t *) malloc(sizeof(wchar_t *)),
*input_name = (wchar_t *) malloc(sizeof(wchar_t *)), *input_sex = (wchar_t *) malloc(sizeof(wchar_t *)),
*input_birthday = (wchar_t *) malloc(sizeof(wchar_t *)), *input_workday = (wchar_t *) malloc(sizeof(wchar_t *)),
*input_address = (wchar_t *) malloc(sizeof(wchar_t *)), *input_telephone = (wchar_t *) malloc(sizeof(wchar_t *)),
*input_email = (wchar_t *) malloc(sizeof(wchar_t *));
*input_no = 0, *input_name = 0, *input_sex = 0, *input_birthday = 0, *input_workday = 0,
*input_address = 0, *input_telephone = 0, *input_email = 0;
wprintf_s(TEXT_MENU_STU_ADD);
wprintf_s(L" >");
wscanf(
L"%ls %ls %ls %ls %ls %ls %ls %ls",
input_no, input_name, input_sex, input_birthday, input_workday,
input_address, input_telephone, input_email
);
STUDENT_set(st,
input_no, input_name, input_sex,
input_birthday, input_workday,
input_address, input_telephone, input_email
);
DATABASE_STUDENT_put(db, st);
wprintf_s(L"操作成功完成!");
break;
}
case 2:
{
wchar_t *input = (wchar_t *) malloc(sizeof(wchar_t *));
*input = 0;
wprintf_s(TEXT_MENU_STU_QUE);
wprintf_s(L" >");
wscanf(L"%ls", input);
DATABASE_STUDENT_get(db, st, input);
STUDENT_printf(st);
wprintf_s(L"操作成功完成!");
break;
}
case 3:
{
wchar_t *input = (wchar_t *) malloc(sizeof(wchar_t *));
*input = 0;
wprintf_s(TEXT_MENU_STU_DEL);
wprintf_s(L" >");
wscanf(L"%ls", input);
DATABASE_STUDENT_del(db, st, input);
break;
}
default:
{
wprintf_s(L"输入有误!");
}
}
STUDENT_free(st);
return 0;
}
student.cint DATABASE_STUDENT_del(DATABASE *pDb, STUDENT *pSt, wchar_t *pCo)
{
int rel = 0;
DATABASE *pDb_tmp = malloc(sizeof(DATABASE));
STUDENT *pSt_tmp = malloc(sizeof(STUDENT));
DATABASE_new(pDb, DATABASE_FILENAME);
DATABASE_new(pDb_tmp, "tmp.txt");
STUDENT_new(pSt_tmp);
DATABASE_STUDENT_get(pDb, pSt_tmp, pCo);
int availableSize = DATABASE_STUDENT_DATA_LINE_MAXSIZE;
wchar_t input_data[DATABASE_STUDENT_DATA_LINE_MAXSIZE];
wchar_t line[DATABASE_STUDENT_DATA_LINE_MAXSIZE];
for (int i = 0; i < DATABASE_STUDENT_DATA_LINE_MAXSIZE; ++i) {
input_data[i] = '\0';
line[i] = '\0';
}
DATABASE_STUDENT_get(pDb, pSt, pCo);
STUDENT_printf(pSt);
wcscat_s(input_data, availableSize, pSt->no);
availableSize -= STUDENT_NO_SIZE;
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->name);
availableSize -= (int) wcslen(pSt->name);
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->sex);
availableSize -= (int) wcslen(pSt->sex);
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->birthday);
availableSize -= (int) wcslen(pSt->birthday);
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->workday);
availableSize -= (int) wcslen(pSt->workday);
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->address);
availableSize -= (int) wcslen(pSt->address);
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->telephone);
availableSize -= (int) wcslen(pSt->telephone);
wcscat_s(input_data, availableSize, DATABASE_STUDENT_SPLIT_CHAR);
availableSize -= (int) wcslen(DATABASE_STUDENT_SPLIT_CHAR);
wcscat_s(input_data, availableSize, pSt->email);
while (1) {
if (fgetws(line, DATABASE_STUDENT_DATA_LINE_MAXSIZE, pDb->file) == NULL) {
wprintf_s(L"fgetws() err in DATABASE_STUDENT_del() in student.c");
}
int e = errno;
if (e != 0) {
wprintf_s(L"%ls", strerror(e));
}
if (wcscmp(input_data, line) != 0) {
rel = fputws(line, pDb_tmp->file);
if (rel != 0) {
rel = 1;
break;
}
}
if (!wcscmp(line, L"\n") || !wcscmp(line, L"\r") || !wcscmp(line, L"\r\n")) {
break;
}
}
STUDENT_free(pSt_tmp);
DATABASE_free(pDb_tmp);
DATABASE_free(pDb);
return rel;
}
database.cDATABASE *DATABASE_new(DATABASE *pDb, const char *pFn)
{
pDb->dataNum = 0;
if (fopen(pFn, "r") == NULL) {
pDb->file = fopen(pFn, "w");
if (errno != 0) {
printf_s(strerror(errno));
}
fclose(pDb->file);
}
pDb->file = fopen(pFn, "a+");
/*if (errno != 0) {
printf_s(strerror(errno));
}*/
return pDb;
}
int DATABASE_free(DATABASE *pDb) {
int rel;
rel = fclose(pDb->file);
if (rel != 0) {
printf_s(strerror(errno));
rel = 1;
}
free(pDb->file);
pDb->file = NULL;
return rel;
}
|
|