Функция void delete". Требуется идентификатор - C (СИ)
Формулировка задачи:
В общем, ругается на функцию void delete, не понимаю почему. Помогите исправить.
#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_STRING_LENGTH 256 FILE *db = NULL; char *dbdir = NULL; typedef struct { unsigned int id; unsigned int deleted; char name[MAX_STRING_LENGTH]; float cost; char supplier[MAX_STRING_LENGTH]; } merch; void printMenu(){ printf("1. Add merch\n"); printf("2. Delete merch\n"); printf("3. Restore\n"); printf("4. Save as\n"); printf("5. View merch of supplier\n"); printf("6. View merch with cost lesser than setting\n"); printf("7. View all\n"); printf("0. Exit\n"); printf("\n"); } void loadcfg(char *name){ char *cfgname = (char *) malloc (strlen(name) + 1); memset(cfgname, 0, strlen(name) + 1); strcpy (cfgname, name); int haveDot = 0; int i = strlen(name) - 1; while ( i >= 0 && name[i] != '/'){ if (name[i] == '.'){ haveDot = i; break; } i--; } if (haveDot){ if (strlen(name) - 1 - haveDot < 3) cfgname = (char *) realloc (cfgname, 5 + haveDot); cfgname[haveDot + 1] = 'c'; cfgname[haveDot + 2] = 'f'; cfgname[haveDot + 3] = 'g'; cfgname[haveDot + 4] = '\0'; } else{ cfgname = (char *) realloc (cfgname, strlen(name) + 5); cfgname[strlen(name)] = '.'; cfgname[strlen(name) + 1] = 'c'; cfgname[strlen(name) + 2] = 'f'; cfgname[strlen(name) + 3] = 'g'; cfgname[strlen(name) + 4] = '\0'; } FILE *cfg = fopen(cfgname, "r+b"); if (cfg == NULL){ printf("Cfg file %s not found\n", cfgname); exit(1); } char *cfgline = (char *) malloc (MAX_STRING_LENGTH); memset(cfgline, 0, MAX_STRING_LENGTH); fgets(cfgline, MAX_STRING_LENGTH, cfg); printf("%s\n", cfgline); char *tmp = 0; if (strstr(cfgline, "DB = ")[0] == cfgline[0]){ dbdir = (char *) malloc (MAX_STRING_LENGTH); memset(dbdir, 0, MAX_STRING_LENGTH); int i = 5; while (cfgline[i] != '\0'){ dbdir[i - 5] = cfgline[i]; i++; } dbdir[i] = '\0'; } else{ printf("Cfg file have errors\n"); exit(0); } } void addMerch(){ merch *pnew = (merch *) malloc (sizeof(merch)); memset(pnew, 0, sizeof(merch)); pnew->id = 1; printf("Input merch name: "); scanf("%s", &pnew->name); printf("Input supplier name: "); scanf("%s", &pnew->supplier); printf("Input merch cost: "); scanf("%f", &pnew->cost); merch *tmp = (merch *) malloc (sizeof(merch)); memset(tmp, 0, sizeof(merch)); fseek(db, 0, SEEK_SET); while (!feof(db)){ fread(tmp, sizeof(merch), 1, db); if (tmp->deleted != 0){ pnew->id = tmp->id; fseek(db, sizeof(merch) * -1, SEEK_CUR); break; } } if (pnew->id == 1) pnew->id = tmp->id + 1; fwrite(pnew, sizeof(merch), 1, db); free(tmp); free(pnew); } void view(merch *m){ printf("ID: %d\n", m->id); printf("Name: %s\n", m->name); printf("Cost: %f\n", m->cost); printf("Supplier's name: %s\n", m->supplier); } int viewAll(){ int isEmpty = 1; fseek(db, 0, SEEK_SET); merch *tmp = (merch *) malloc (sizeof(merch)); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); if (fread(tmp, sizeof(merch), 1, db) > 0) if (tmp->deleted == 0){ isEmpty = 0; view(tmp); } printf("\n"); } free(tmp); return isEmpty; } int viewDeleted(){ int haveDeleted = 0; fseek(db, 0, SEEK_SET); merch *tmp = (merch *) malloc (sizeof(merch)); printf("\nList of deleted ... :\n"); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); if (fread(tmp, sizeof(merch), 1, db) > 0){ if (tmp->deleted != 0){ haveDeleted = 1; view(tmp); } } printf("\n"); } free(tmp); return haveDeleted; } void restore(unsigned int id){ fseek(db, 0, SEEK_SET); merch *tmp = (merch *) malloc (sizeof(merch)); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); fread(tmp, sizeof(merch), 1, db); if (tmp->id == id && tmp->deleted != 0){ tmp->deleted = 0; fseek(db, sizeof(merch) * -1, SEEK_CUR); fwrite(tmp, sizeof(merch), 1, db); break; } } free(tmp); } void delete(unsigned int id){ fseek(db, 0, SEEK_SET); merch *tmp = (merch *) malloc (sizeof(merch)); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); fread(tmp, sizeof(merch), 1, db); if (tmp->id == id && tmp->deleted == 0){ tmp->deleted = 1; fseek(db, sizeof(merch) * -1, SEEK_CUR); fwrite(tmp, sizeof(merch), 1, db); break; } } free(tmp); } void saveAs(){ printf("Input filename: "); char *pfilename = (char *) malloc (MAX_STRING_LENGTH); memset(pfilename, 0, MAX_STRING_LENGTH); scanf("%s", pfilename); FILE *ndb = fopen(pfilename, "w+b"); fseek(db, 0, SEEK_SET); merch *tmp = (merch *) malloc (sizeof(merch)); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); fread(tmp, sizeof(merch), 1, db); fwrite(tmp, sizeof(merch), 1, ndb); } free(pfilename); fclose(ndb); } int main(int argc, char **argv){ loadcfg(argv[0]); db = fopen(dbdir, "r+b"); if (db == NULL) db = fopen(dbdir, "w+b"); if (db == NULL) return -1; fseek(db, 0, SEEK_END); while (1){ printMenu(); printf(">> "); int opt = 0; scanf("%d", &opt); switch (opt){ case 0: fclose(db); return 0; case 1: addMerch(); break; case 2: if (!viewAll()){ unsigned int id = 0; printf ("Input detele id: "); scanf("%d", &id); delete(id); break; } case 3: if (viewDeleted()){ unsigned int id = 0; printf ("Input restore id: "); scanf("%d", &id); restore(id); } break; case 4: saveAs(); break; case 5: printf("Input supplier name: "); char *msupplier = (char *) malloc (MAX_STRING_LENGTH); memset(msupplier, 0, MAX_STRING_LENGTH); scanf("%s", msupplier); fseek(db, 0, SEEK_SET); merch *tmp = (merch *) malloc (sizeof(merch)); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); if (fread(tmp, sizeof(merch), 1, db) > 0) if (!strcmp(tmp->supplier, msupplier)){ view(tmp); printf("\n"); } } free(msupplier); free(tmp); break; case 6: printf("Input cost: "); float mcost = 0; scanf("%f", &mcost); fseek(db, 0, SEEK_SET); tmp = (merch *) malloc (sizeof(merch)); while (!feof(db)){ memset(tmp, 0, sizeof(merch)); if (fread(tmp, sizeof(merch), 1, db) > 0) if (tmp->cost < mcost) view(tmp); printf("\n"); } free(tmp); break; case 7: viewAll(); break; default: printf("Unknow\n"); } } return 0; }
Решение задачи: «Функция void delete". Требуется идентификатор»
textual
Листинг программы
1>c:\users\...\visual studio 2010\projects\testc\main.c(220): error C2065: pfilename: необъявленный идентификатор
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д