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?
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?