|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
[C] Rubrica telefonica
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: 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 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?
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
dir è una struct, mica un char, non puoi fare quell'assegnamento, devi fare
Codice:
memset(array, 0, ENTRIESMAX * sizeof(dim)); |
|
|
|
|
|
#3 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Di byte, non di bit
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
lapsus, pardon
|
|
|
|
|
|
#5 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Per cancellare la memoria ho messo:
Codice:
memset(entry, 0, ENTRIESMAX * sizeof(dir)); Per l'utilizzo di fscanf e del problema sopra descritto?
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#6 |
|
Senior Member
Iscritto dal: Apr 2000
Città: Vicino a Montecatini(Pistoia) Moto:Kawasaki Ninja ZX-9R Scudetti: 29
Messaggi: 53971
|
Sì, è corretto.
|
|
|
|
|
|
#7 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
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 )... Devo aver sbagliato qualcosa nell'allocazione ( che mi sfugge ) perchè quando uso l'fscanf non viene copiato niente dentro gli elementi del vettore: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>
#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;
}
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#8 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
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?
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#9 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
apri direttamente con r+ invece di chiudere e riaprire in modalità di sola scrittura (ovvero w)
|
|
|
|
|
|
#10 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Ma ho capito bene quindi? Con r+ leggo e scrivo?
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#11 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
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: Codice:
if ((fwrite(entry, sizeof(char), len, fp))!= len )
{
printf("Error in writing to file\n");
exit(1);
}
- 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+
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#12 |
|
Senior Member
Iscritto dal: Apr 2010
Città: Frosinone
Messaggi: 416
|
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 |
|
|
|
|
|
#13 | |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
Quote:
EDIT: Ho provato sta cosa: Codice:
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);
}
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...
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
Ultima modifica di kwb : 09-09-2010 alle 17:03. |
|
|
|
|
|
|
#14 |
|
Member
Iscritto dal: Aug 2005
Messaggi: 168
|
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 Codice PHP:
Codice PHP:
|
|
|
|
|
|
#15 |
|
Senior Member
Iscritto dal: Jul 2003
Città: Alessandria
Messaggi: 10167
|
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?
__________________
Dell XPS 13 (9350) :: i5-2500K - HD6870 - AsRock Z68 Pro3 - Corsair Vengeance 8GB (4x2) DDR3 :: Samsung Galaxy S4 GT-i9505
|
|
|
|
|
|
#16 |
|
Member
Iscritto dal: Aug 2005
Messaggi: 168
|
w+ cancella quello che c'è nel file, r+ no
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 00:11.











)... Devo aver sbagliato qualcosa nell'allocazione ( che mi sfugge ) perchè quando uso l'fscanf non viene copiato niente dentro gli elementi del vettore:







