PDA

View Full Version : [C] Rubrica telefonica


kwb
27-08-2010, 17:49
Sono sempre alle prese con la gestione dei file.
Ora devo fare una sorta di rubrica telefonica, dove, dato un file in input formattato nel seguente modo:
<telefono1> <nome1>
<telefono2> <nome2>

Devo fare varie operazioni, a cui però non sono ancora arrivato perchè ho problemi a monte, vi posto il codice:

/*
Write a C program that manages a simple telephone directory. At startup, the program must read the directory from a file named “dir.txt”.
Each line in the file represents a directory entry and is composed of a telephone number
(a sequence of characters without spaces of maximum length 15) followed by a name (the rest of the line).
The following is a sample fragment of the file:
0115641234 Mario Rossi
+393396109876 John Smith
...

The program must store the directory in memory, and must work with up to a maximum number of entries (defined by a constant)
and with names of maximum length defined by another constant.
If the entries in the file exceed these limits, the program must terminate with an error message.
After reading the file, the program must interact with the user by a menu with the following items:
L name looks for the specified name in the directory and prints the corresponding number
A tel name adds a new tel-name entry to the in-memory directory (if space is available).
If an entry with the same name already exists, the telephone of that entry is updated
D name deletes the entry with the given name in the in-memory directory (if present)
S saves the current in-memory directory to the file dir.txt
Q quits the program
*/

#include <stdio.h>
#include <stdlib.h>

#define NAMEL 40
#define ENTRIESMAX 40
#define PHONEMAX 15

typedef struct entry
{
char phone_number[PHONEMAX];
char name[NAMEL];
}dir;

void clear_array ( dir array[]);
int main(void)
{
FILE *fp;
dir entry[ENTRIESMAX];
int i;

// clear_array(entry);

if((fp=fopen("tel_dir.txt", "r"))==NULL)
{
printf("Open file error, QUIT\n");
return -1;
}
for ( i = 0; i < ENTRIESMAX; i++)
fscanf(fp, "%s %s", entry[i].phone_number, entry[i].name);
for ( i=0; i < ENTRIESMAX; i++)
printf("%s %s %s\n", entry[i].phone_number, entry[i].name);
}
/*
void clear_array ( dir array[] )
{
int i;
for ( i = 0; i < ENTRIESMAX; i++)
array[i] = '\0';
}*/


In giallo: non riesco a far riempire di \0 il vettore, non so perchè... Sinceramente ste strutture devo ancora capirle bene. XCode mi dice: "Incompatible types in assignment"

In verde: Siccome ogni riga del file è struttura così
+39123456789 Pinco Pallino

Oppure
1234856 Tizio Caio

l'fscanf dando come parametri ("%s %s") mi prende solo il numero di telefono e il nome, omettendo il cognome, che viene preso al giro successivo del ciclo. Come si fa a dirgli che anche se c'è lo mi deve mettere in un unico array il nome e il cognome?

tuccio`
27-08-2010, 18:11
dir è una struct, mica un char, non puoi fare quell'assegnamento, devi fare


memset(array, 0, ENTRIESMAX * sizeof(dim));


che riempie di 0 il numero di bit corrispondente al terzo argomento a partire dalla posizione puntata da array (che esattamente quello che devi fare)

cionci
27-08-2010, 19:59
Di byte, non di bit ;)

tuccio`
27-08-2010, 21:41
lapsus, pardon

kwb
28-08-2010, 11:21
Per cancellare la memoria ho messo:

memset(entry, 0, ENTRIESMAX * sizeof(dir));

Ho il dubbio sul terzo argomento, penso sia corretto però...

Per l'utilizzo di fscanf e del problema sopra descritto? :stordita:

cionci
28-08-2010, 12:09
Sì, è corretto.

kwb
28-08-2010, 12:42
Allora, cambio di programma...
Ho optato per fare un'allocazione dinamica per l'array che dovrà contenere le informazioni.
Ugualmente, ho allocato anche la memoria per la stringa, escludendo il numero telefonico che so di essere al massimo 15 caratteri. Tutto questo dopo aver determinato tra le varie stringhe, quella più lunga.
L'allocazione sul nome secondo me va fatta perchè uno si può chiamare "Pinco Pallino" ma anche "Michele Precipitevolissimevolmente" che ovviamente richiedono memorie diverse...
Tuttavia c'è un problema ( ovviamente, se no non sarei qua :asd: )... Devo aver sbagliato qualcosa nell'allocazione ( che mi sfugge ) perchè quando uso l'fscanf non viene copiato niente dentro gli elementi del vettore:

/*
Write a C program that manages a simple telephone directory. At startup, the program must read the directory from a file named “dir.txt”.
Each line in the file represents a directory entry and is composed of a telephone number
(a sequence of characters without spaces of maximum length 15) followed by a name (the rest of the line).
The following is a sample fragment of the file:
0115641234 Mario Rossi
+393396109876 John Smith
...

The program must store the directory in memory, and must work with up to a maximum number of entries (defined by a constant)
and with names of maximum length defined by another constant.
If the entries in the file exceed these limits, the program must terminate with an error message.
After reading the file, the program must interact with the user by a menu with the following items:
L name looks for the specified name in the directory and prints the corresponding number
A tel name adds a new tel-name entry to the in-memory directory (if space is available).
If an entry with the same name already exists, the telephone of that entry is updated
D name deletes the entry with the given name in the in-memory directory (if present)
S saves the current in-memory directory to the file dir.txt
Q quits the program
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ENTRIESMAX 40
#define PHONEMAX 15

typedef struct entry
{
char phone_number[PHONEMAX];
char *name;
}dir;

int line_counter (FILE *fp, int *len);

int main(void)
{
FILE *fp= NULL;
dir *entry;
int i;
int entry_len; //lenght of the entry
int lines; //Number of lines

if((fp=fopen("tel_dir.txt", "r"))==NULL)
{
printf("Open file error, QUIT\n");
return -1;
}
lines= line_counter(fp, &entry_len); //Determino quante entrate ci sono nella rubrica
entry = (dir*)calloc(lines, sizeof(dir)); //Alloco la memoria necessaria
for ( i = 0; i < lines; i++)
{ entry[i].name = calloc(entry_len-15, sizeof(char)); //Alloco la memoria per il nome
fscanf(fp, "%15s", entry[i].phone_number); //Carico i primi 15 caratteri della riga dentro l'elemento phone_number
fscanf(fp, "%s", entry[i].name); //Carico il resto dentro name
}
for ( i=0; i < lines; i++)
printf("%s %s\n", entry[i].phone_number, entry[i].name);
}

int line_counter ( FILE *fp, int *len)
{
int i; //Contatore per le entrare
char line[60];
for ( i=0; i < 60; i++)
line[i] = 0;
i=0;
while((fgets(line, 59, fp))!= NULL)
{
if ( strlen(line) > *len)
*len = strlen(line);
i++;
}
return i;
}

kwb
30-08-2010, 12:02
Ho proceduto col mio programma.
Ora però ho una domanda sulla gestione dei file.
Per leggere il file di testo ho dato:

fp=fopen(nome_file, "r");

Ora che devo andarlo a modificare cosa devo fare?
Chiudere il file con:
fclose(fp)
E dare:
fp=fopen(nome_file, "r+")
Che se ho ben capito serve sia per scrivere che per leggere...
Oppure cosa?

tuccio`
30-08-2010, 12:22
apri direttamente con r+ invece di chiudere e riaprire in modalità di sola scrittura (ovvero w)

kwb
30-08-2010, 16:14
apri direttamente con r+ invece di chiudere e riaprire in modalità di sola scrittura (ovvero w)

Ma ho capito bene quindi? Con r+ leggo e scrivo?

kwb
08-09-2010, 17:00
Ho la necessità di salvare un'array dentro il file di testo che contiene i dati della rubrica. Come faccio ad usare fwrite?
Ho provato una cosa del genere:

if ((fwrite(entry, sizeof(char), len, fp))!= len )
{
printf("Error in writing to file\n");
exit(1);
}


Entry è l'array che contiene due sotto array:
- entry[i].name allocato dinamicamente che contiene il nome del contatto in rubrica
- entry[i].phone_number che contiene il numero di telefono, 15 caratteri.

len è il numero di entrate che ci sono nell'array.

Tuttavia così facendo non cambia niente.

Il file aperto è in modalità r+

tuccio`
09-09-2010, 07:59
se devi scrivere nel formato descritto sopra, considera l'uso di fputs

devi scrivere un file ascii.. puoi usare le funzioni fputs, fprintf...

fwrite è molto più utile per scrivere su file binari

kwb
09-09-2010, 16:34
se devi scrivere nel formato descritto sopra, considera l'uso di fputs

devi scrivere un file ascii.. puoi usare le funzioni fputs, fprintf...

fwrite è molto più utile per scrivere su file binari

Ah e poi mi basta un semplice fclose(fp) per salvare il tutto?

EDIT: Ho provato sta cosa:

void saveData (FILE *fp, dir entry[], int entry_len, int len)
{
int i;
for ( i=0; i < len; i++)
if ((fputs(entry[i].name, fp) || fputs(entry[i].phone_number, fp))==EOF)
{
printf("Error in writing to file\n");
exit(1);
}

fclose(fp);
}


Ma ha due problemi:
1. Non mi cancella quello che c'era scritto prima nel file ( e ne ho la necessità visto che tutti i dati sono memorizzati dentro l'array - compresi quelli aggiornati )
2. Mi vengono salvati nel file solo i nomi dei contatti, non i numeri...

Kenger
10-09-2010, 03:16
1- Apri il file in w+ allora
2- Visto che la prima scrittura ha successo (e quindi da un valore diverso da 0, che per il C è true) e visto che l'operatore è un || (OR) la seconda parte della espressione non viene neanche valutata. Infatti 1 in OR con qualsiasi cosa da sempre 1.
Hai scritto male le condizioni dell'if.

Tu hai scritto

(A || B) == EOF


Mentre avresti dovuto scrivere

A == EOF || B == EOF

kwb
10-09-2010, 11:28
Alla fine sono riuscito a risolvere aprendo il file in sola lettura quando devo caricare i dati nell'array, poi lo chiudo.
Lo riapro in sola scrittura quando devo aggiornare i dati.

Una cosa sul libro di programmazione non ho capito ed è spiegata male...
La differenza che intercorre tra "r+" e "w+".
Sul libro c'è scritto che il primo serve a leggere e scrivere e il secondo serve a scrivere e leggere...
Dove sta la differenza? :mbe:

Kenger
10-09-2010, 14:24
w+ cancella quello che c'è nel file, r+ no ;)