Ciao a tutti,
sto imparando un po' di C e sto cercando di capire la gestione dei file...
ho creato una rubrica che salvi i contatti in un file, fin qui tutto bene.
la prima versione che ho fatto caricava all'apertura tutti contatti e li storava tutti insieme alla chiusura.
ora sto provando a gestire dinamicamente il file durante l'utilizzo della rubrica.
ho quindi creato la function void AddContact() che dopo aver inserito il contatto lo stori nel file.
il problema è che se eseguo AddContact() senza eseguire LoadAddressBook() funziona correttamente, se invece eseguo prima LoadAddressBook() (come dev'essere) la fopen_s() fallisce dicendomi che non ho i permessi necessari (errno 13).
questa è la function LoadAddressBook():
void LoadAddressBook()
{
contactStructType* curContact = (contactStructType*)malloc(sizeof(contactStructType));
memset(curContact, 0, sizeof(contactStructType));
char name[30];
char surname[30];
char number[30];
int i = 0;
FILE* file;
fopen_s(&file, "./addressBook.list", "rb");
fread(curContact, sizeof(contactStructType), 1, file);
while (!feof(file))
{
strcpy_s(name, curContact->name);
strcpy_s(surname, curContact->surname);
strcpy_s(number, curContact->number);
fread(curContact, sizeof(contactStructType), 1, file);
LoadContact(name, surname, number);
}
fclose(file); //va a buon fine restituendo 0
}
mentre questa è la AddContact():
void AddContact(char insName[30], char insSurname[30], char insNumber[30])
{
contactStructType* curContact = contact;
contactStructType* newContact = (contactStructType*)malloc(sizeof(contactStructType));
memset(newContact, 0, sizeof(contactStructType));
while (curContact->next != NULL)
{
curContact = curContact->next;
}
idCount++;
curContact->next = newContact;
newContact->prev = curContact;
newContact->id = idCount;
strcpy_s(newContact->name, insName);
strcpy_s(newContact->surname, insSurname);
strcpy_s(newContact->number, insNumber);
FILE* file;
fopen_s(&file, "./addressBook.list", "ab");
fwrite(newContact, sizeof(contactStructType), 1, file);
fclose(file);
}
allego anche tutto il listato:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <conio.h>
#include <locale.h>
typedef struct contactStruct
{
int id;
char name[30];
char surname[30];
char number[30];
contactStruct* next;
contactStruct* prev;
}contactStructType;
contactStructType* contact = NULL;
static int idCount = 0;
void BoldRed()
{
printf("\033[1;31m");
}
void Red()
{
printf("\033[0;31m");
}
void BoldGreen()
{
printf_s("\033[1;32m");
}
void Green()
{
printf_s("\033[0;32m");
}
void BoldYellow()
{
printf_s("\033[1;33m");
}
void Yellow()
{
printf_s("\033[0;33m");
}
void BoldBlue()
{
printf_s("\033[1;34m");
}
void Blue()
{
printf_s("\033[0;34m");
}
void BoldMagenta()
{
printf_s("\033[1;35m");
}
void Magenta()
{
printf_s("\033[0;36m");
}
void BoldCyan()
{
printf_s("\033[1;36m");
}
void Cyan()
{
printf_s("\033[0;36m");
}
void ResetColor()
{
printf_s("\033[0m");
}
void Intestation()
{
BoldCyan();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
BoldGreen();
printf_s("\t\t\t\t\t --- Rubrica 1.0 ---");
BoldCyan();
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
void LoadContact(char insName[30], char insSurname[30], char insNumber[30])
{
contactStructType* curContact = contact;
contactStructType* newContact = (contactStructType*)malloc(sizeof(contactStructType));
memset(newContact, 0, sizeof(contactStructType));
while (curContact->next != NULL)
{
curContact = curContact->next;
}
idCount++;
curContact->next = newContact;
newContact->prev = curContact;
newContact->id = idCount;
strcpy_s(newContact->name, insName);
strcpy_s(newContact->surname, insSurname);
strcpy_s(newContact->number, insNumber);
}
void AddContact(char insName[30], char insSurname[30], char insNumber[30])
{
contactStructType* curContact = contact;
contactStructType* newContact = (contactStructType*)malloc(sizeof(contactStructType));
memset(newContact, 0, sizeof(contactStructType));
while (curContact->next != NULL)
{
curContact = curContact->next;
}
idCount++;
curContact->next = newContact;
newContact->prev = curContact;
newContact->id = idCount;
strcpy_s(newContact->name, insName);
strcpy_s(newContact->surname, insSurname);
strcpy_s(newContact->number, insNumber);
FILE* file;
fopen_s(&file, "./addressBook.list", "ab");
fwrite(newContact, sizeof(contactStructType), 1, file);
fclose(file);
}
void FillContact()
{
char insName[30];
char insSurname[30];
char insNumber[30];
Intestation();
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Cyan();
printf_s("\n\t\t\t\t\t --- nuovo contatto ---");
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
BoldCyan();
printf_s("\n\tinserisci il nome \t\t-->\t\t");
scanf_s("\t%s", insName, sizeof(insName));
printf_s("\n\tinserisci il cognome \t\t-->\t\t");
scanf_s("%s", insSurname, sizeof(insSurname));
printf_s("\n\tinserisci il numero di telefono -->\t\t");
scanf_s("%s", insNumber, sizeof(insNumber));
BoldMagenta();
printf_s("--------------------------------------------------------------------------------------------------------");
ResetColor();
AddContact(insName, insSurname, insNumber);
system("cls");
Intestation();
contactStructType* curContact = contact->next;
contactStructType* newContact = (contactStructType*)malloc(sizeof(contactStructType));
memset(newContact, 0, sizeof(contactStructType));
while (curContact->next != NULL)
{
curContact = curContact->next;
}
newContact = curContact;
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Yellow();
printf_s("\n\tnuovo contatto aggiunto alla rubrica:\t%d)\t%s %s --> %s", newContact->id, newContact->name, newContact->surname, newContact->number);
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
ResetColor();
}
void LoadAddressBook()
{
contactStructType* curContact = (contactStructType*)malloc(sizeof(contactStructType));
memset(curContact, 0, sizeof(contactStructType));
char name[30];
char surname[30];
char number[30];
int i = 0;
FILE* file;
fopen_s(&file, "./addressBook.list", "rb");
fread(curContact, sizeof(contactStructType), 1, file);
while (!feof(file))
{
strcpy_s(name, curContact->name);
strcpy_s(surname, curContact->surname);
strcpy_s(number, curContact->number);
fread(curContact, sizeof(contactStructType), 1, file);
LoadContact(name, surname, number);
}
fclose(file); //va a buon fine restituendo 0
}
void InitializeAddressBook()
{
contact = (contactStructType*)malloc(sizeof(contactStructType));
memset(contact, 0, sizeof(contactStructType));
LoadAddressBook();
}
int CountContact()
{
int counter = 0;
contactStructType* contactCounter = contact;
while (contactCounter->next != NULL)
{
contactCounter = contactCounter->next;
counter++;
}
return counter;
}
contactStructType* CheckSearchByNameNull(char name[30])
{
contactStructType* srchdContact = contact->next;
while (srchdContact != NULL)
{
if (strcmp(srchdContact->name, name) == 0)
{
return srchdContact;
}
srchdContact = srchdContact->next;
}
return NULL;
}
contactStructType* CheckSearchBySurnameNull(char surname[30])
{
contactStructType* srchdContact = contact->next;
while (srchdContact != NULL)
{
if (strcmp(srchdContact->surname, surname) == 0)
{
return srchdContact;
}
srchdContact = srchdContact->next;
}
return NULL;
}
contactStructType* CheckSearchByNumberNull(char number[30])
{
contactStructType* srchdContact = contact->next;
while (srchdContact != NULL)
{
if (strcmp(srchdContact->number, number) == 0)
{
return srchdContact;
}
srchdContact = srchdContact->next;
}
return NULL;
}
void SearchByName(char name[30])
{
if (contact->next == NULL)
{
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t\t--- nessun contatto da cercare ---");
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
else
{
contactStructType* srchdContact = contact;
if (CheckSearchByNameNull(name) == NULL) //perchè non posso mettere != NULL ? ?
{
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t --- nessun contatto corrispondente al nome %s ---", name);
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
else
{
system("cls");
Intestation();
for (int i = 1; i <= idCount; i++)
{
while (srchdContact != NULL)
{
if (strcmp(srchdContact->name, name) == 0)
{
ResetColor();
printf_s("\n\t%d)\t%s %s\t\t-->\t\t%s", srchdContact->id, srchdContact->name, srchdContact->surname, srchdContact->number);
}
srchdContact = srchdContact->next;
}
}
printf_s("\n");
}
}
}
void SearchBySurname(char surname[30])
{
if (contact->next == NULL)
{
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t\t--- nessun contatto da cercare ---");
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
else
{
contactStructType* srchdContact = contact->next;
if (CheckSearchBySurnameNull(surname) == NULL) //perchè non posso mettere != NULL ? ?
{
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t --- nessun contatto corrispondente al cognome %s ---", surname);
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
else
{
system("cls");
Intestation();
for (int i = 1; i <= idCount; i++)
{
while (srchdContact != NULL)
{
if (strcmp(srchdContact->surname, surname) == 0)
{
ResetColor();
printf_s("\n\t%d)\t%s %s\t\t-->\t\t%s", srchdContact->id, srchdContact->name, srchdContact->surname, srchdContact->number);
}
srchdContact = srchdContact->next;
}
}
printf_s("\n");
}
}
}
void SearchByNumber(char number[30])
{
if (contact->next == NULL)
{
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t\t--- nessun contatto da cercare ---");
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
else
{
contactStructType* srchdContact = contact->next;
if (CheckSearchByNumberNull(number) == NULL)
{
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t --- nessun contatto corrispondente al numero %s ---", number);
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n\n");
ResetColor();
}
else
{
system("cls");
Intestation();
for (int i = 1; i <= idCount; i++)
{
while (srchdContact != NULL)
{
if (strcmp(srchdContact->number, number) == 0)
{
ResetColor();
printf_s("\n\t%d)\t%s %s\t\t-->\t\t%s", srchdContact->id, srchdContact->name, srchdContact->surname, srchdContact->number);
}
srchdContact = srchdContact->next;
}
}
printf_s("\n");
}
}
}
void DelContact(int id)
{
if (id == idCount)
{
contactStructType* toDel = contact;
while (toDel->id != id)
{
toDel = toDel->next;
}
contactStructType* toDelPrev = toDel->prev;
toDelPrev->next = NULL;
idCount = idCount - 1;
free(toDel);
system("cls");
Intestation();
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Yellow();
printf_s("\n\t\t\t\t --- contatto eliminato --- ");
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
ResetColor();
}
else if (id == 0 or id > idCount)
{
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
printf_s("\n\t\t\t --- inserisci un numero compreso tra 1 e %d ---", idCount);
printf_s("\n--------------------------------------------------------------------------------------------------------");
ResetColor();
}
else
{
contactStructType* toDel = contact;
while (toDel->id != id)
{
toDel = toDel->next;
}
contactStructType* toDelNext = toDel->next;
contactStructType* toDelPrev = toDel->prev;
toDelNext->prev = toDel->prev;
toDelPrev->next = toDel->next;
while (toDelNext != NULL)
{
toDelNext->id = toDelNext->id - 1;
toDelNext = toDelNext->next;
}
idCount = idCount - 1;
free(toDel);
system("cls");
Intestation();
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Yellow();
printf_s("\n\t\t\t\t --- contatto eliminato --- ");
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
ResetColor();
}
}
int main()
{
InitializeAddressBook();
char c;
char c2;
Intestation();
do
{
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
ResetColor();
BoldCyan();
printf_s("\t- premi 1 per aggiungere un contatto\n");
printf_s("\t- premi 2 per cercare un contatto\n");
printf_s("\t- premi 3 per consultare la rubrica\n");
printf_s("\t- premi 4 per eliminare un contatto\n");
printf_s("\t- premi 9 per uscire dal programma\n");
BoldMagenta();
printf_s("--------------------------------------------------------------------------------------------------------\n");
printf("\033[0m");
c = _getch();
system("cls");
int w = 1;
switch (c)
{
case '1':
{
FillContact();
do
{
BoldMagenta();
printf_s("\n\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\tpremi 1 per inserire un nuovo contatto");
setlocale(LC_ALL, "");
printf_s("\n\tpremi 9 per tornare al menù principale");
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
c2 = _getch();
system("cls");
;
if (c2 == '1')
{
FillContact();
}
else
{
w = 0;
}
} while (w == 1);
Intestation();
break;
}
case '2':
{
char srch[30];
int s;
system("cls");
Intestation();
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\n\t- premi 1 per cercare un contatto in base al nome\n");
printf_s("\n\t- premi 2 per cercare un contatto in base al cognome\n");
printf_s("\n\t- premi 3 per cercare un contatto in base al numero di telefono\n");
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
s = _getch();
system("cls");
Intestation();
if (s == '1')
{
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\n\tnome da cercare --> ");
scanf_s("%s", srch, sizeof(srch));
ResetColor();
SearchByName(srch);
}
else if (s == '2')
{
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\n\tcognome da cercare --> ");
scanf_s("%s", srch, sizeof(srch));
ResetColor();
SearchBySurname(srch);
}
else if (s == '3')
{
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\n\tnumero da cercare --> ");
scanf_s("%s", srch, sizeof(srch));
ResetColor();
SearchByNumber(srch);
}
break;
}
case '3':
{
contactStructType* curContact = contact->next;
Intestation();
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Yellow();
printf_s("\n\t\t\t\t --- sono presenti %d contatti --- ", idCount);
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------\n");
if (curContact == NULL)
{
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Red();
printf_s("\n\t\t\t --- nessun contatto presente in rubrica ---");
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n");
}
else
{
for (int l = 1; l <= idCount; l++)
{
ResetColor();
printf_s("\n\t%d)\t%s %s\t\t\t-->\t\t%s", curContact->id, curContact->name, curContact->surname, curContact->number);
ResetColor();
curContact = curContact->next;
}
BoldMagenta();
printf_s("\n\n--------------------------------------------------------------------------------------------------------\n\n");
ResetColor();
}
break;
}
case '4':
{
int delId = 0;
Intestation();
if (contact->next == NULL)
{
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------");
Red();
printf_s("\n\t\t\t --- nessun contatto presente in rubrica ---");
BoldRed();
printf_s("\n--------------------------------------------------------------------------------------------------------\n\n");
}
else
{
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\t\t\t\t--- Stai eliminando un contatto --- ");
BoldMagenta();
printf_s("\n--------------------------------------------------------------------------------------------------------");
BoldCyan();
printf_s("\n\n\t- inserisci l'id del contatto da eliminare \t\t-->\t\t");
scanf_s("\t%d", &delId);
ResetColor();
DelContact(delId);
}
break;
}
case '5':
{
system("cls");
Intestation();
printf_s("\n\t%d", CountContact());
}
default:
{
}
}
} while (c != '9');
}
qualche dritta?
thanks