Newsgroup: it.comp.lang.c
Titolo: Ricerca in database


free software&GNU/Linux 4ever! ;-)
            _           _        _          
 _ __  __ _| |_ _ _ ___| |__ _ _(_)_ ____ _ 
| '  \/ _` |  _| '_/ _ \ '_ \ '_| \ V / _` |
|_|_|_\__,_|\__|_| \___/_.__/_| |_|\_/\__,_|
 
 

 and released under the GPLv3 \n");
				break;
		}
	}while (scelta != 0);			
	return 0;
}
 
 
 
/* Books */
 
void books ()
{
	int scelta;
 
	do	
	{	do	
		{	printf ("What do you want to do?:\n");
			printf ("[1] Add a book \n");
			printf ("[2] List of books \n");
			printf ("[3] Find a book \n");
			printf ("[0] Go back\n");
			scanf ("%d", &scelta);
		}while (scelta < 0 || scelta > 3);
 
		switch (scelta)
		{
			case 1:
			{	bookAdd();
				break;
			}
			case 2:
			{	bookList();
				break;
			}
			case 3:
			{	bookFind();
				break;
			}
 
		}
	}while (scelta != 0);
}
	/* Aggiungo un libro */
void bookAdd ()
{
 
  printf (" ADD A BOOK: \n");
  gets (nulla);
  /*Title*/
  printf (" TITLE: \t");
  gets (mediaTitle);
  printf ("\n");
  /*Author*/
  printf (" AUTHOR: \t");
  gets (mediaAuthor);
  printf ("\n");
  /*Year*/
  printf (" YEAR: \t");
  gets (mediaYear);
  printf ("\n");
  /*Publishing house*/
  printf (" PUBLISHING HOUSE:: \t");
  gets (mediaPub_house);
  printf ("\n");
  /*Genre*/
  printf (" GENRE: \t");
  gets (mediaGenre);
  printf ("\n");
  /*Price*/
  printf (" PRICE: \n");
  gets (mediaPrice);
  /*Notes*/
  printf (" NOTES: \n");
  gets (mediaNotes);
  printf ("All right?\n");
  strcpy(bookItem.title, mediaTitle);
  strcpy(bookItem.author, mediaAuthor);
  strcpy(bookItem.year, mediaYear);
  strcpy(bookItem.pub_house, mediaPub_house);
  strcpy(bookItem.genre, mediaGenre);
  strcpy(bookItem.price, mediaPrice);
  strcpy(bookItem.notes, mediaNotes);
	bookFileWrite = fopen("books.dat", "ab");
	fwrite( &bookItem, sizeof(bookItem), 1, bookFileWrite );
	fclose(bookFileWrite);
printf ("Done!\a");
}
 
void bookList ()
{
	bookFileRead = fopen("books.dat", "rb");
 
	while ( !feof(bookFileRead) ) 
	{
		if (fread( &bookItem, sizeof(bookItem), 1, bookFileRead )) 
		{
			printf("=========================\n");
			printf("Title : %s\n", bookItem.title);
			printf("Author : %s\n", bookItem.author);
			printf("Year : %s\n", bookItem.year);
			printf("Publishing House : %s\n", bookItem.pub_house);
			printf("Genre : %s\n", bookItem.genre);
			printf("Notes : %s\n", bookItem.notes);
		}
	}
 
	fclose(bookFileRead);
}
 
void bookFind ()
{
int scelta;
 
	do	
	{	do	
		{	
			printf ("[1] Search for title \n");
			printf ("[2] Search for author \n");
			printf ("[3] Search for year \n");
			printf ("[4] Search for genre \n");
			printf ("[0] Go back\n");
			scanf ("%d", &scelta);
		}while (scelta < 0 || scelta > 4);
 
		switch (scelta)
		{
			case 1:
			{	bookFindTitle();
				break;
			}
			case 2:
			{	bookFindAuthor();
				break;
			}
			case 3:
			{	bookFindYear();
				break;
			}
 
		}
	}while (scelta != 0);
}	
 
 
/*Ricerca per titolo*/
void bookFindTitle ()
{
	printf("Search by title");
	gets (nulla);
	printf("Title > \t");
	gets (searchTitle);
	bookFileRead = fopen("books.dat", "rb");
while ( !feof(bookFileRead) ) 
	{
		if (fread( &bookItem, sizeof(bookItem), 1, bookFileRead )) 
		{
			if (bookItem.title==searchTitle)
			{
			printf("=========================\n");
			printf("Title : %s\n", bookItem.title);
			printf("Author : %s\n", bookItem.author);
			printf("Year : %s\n", bookItem.year);
			printf("Publishing House : %s\n", bookItem.pub_house);
			printf("Genre : %s\n", bookItem.genre);
			printf("Notes : %s\n", bookItem.notes);
			}	
	}
	}
 
	fclose(bookFileRead);
}
/*Ricerca per autore*/
void bookFindAuthor ()
{
}
/*Ricerca per anno*/
void bookFindYear ()
{
}
/* Films */
 
void films ()
{
	int scelta;
 
	do	
	{	do	
		{	printf ("What do you want to do?:\n");
			printf ("[1] Add a film \n");
			printf ("[2] List of films \n");
			printf ("[3] Find a film \n");
			printf ("[0] Go back\n");
			scanf ("%d", &scelta);
		}while (scelta < 0 || scelta > 3);
 
		switch (scelta)
		{
			case 1:
			{	filmAdd();
				break;
			}
			case 2:
			{	filmList();
				break;
			}
			case 3:
			{	filmFind();
				break;
			}
 
		}
	}while (scelta != 0);
}
	/* Aggiungo un film */
void filmAdd ()
{
	printf (" ADD A FILM: \n");
  gets (nulla);
  /*Title*/
  printf (" TITLE: \t");
  gets (mediaTitle);
  printf ("\n");
  /*Author*/
  printf (" AUTHOR: \t");
  gets (mediaAuthor);
  printf ("\n");
  /*Year*/
  printf (" YEAR: \t");
  gets (mediaYear);
  printf ("\n");
  /*Publishing house*/
  printf (" MEDIUM:: \t");
  gets (mediaSupport);
  printf ("\n");
  /*Genre*/
  printf (" GENRE: \t");
  gets (mediaGenre);
  printf ("\n");
  /*Price*/
  printf (" PRICE: \n");
  gets (mediaPrice);
  /*Notes*/
  printf (" NOTES: \n");
  gets (mediaNotes);
  printf ("All right?\n");
  strcpy(filmItem.title, mediaTitle);
  strcpy(filmItem.author, mediaAuthor);
  strcpy(filmItem.year, mediaYear);
  strcpy(filmItem.pub_house, mediaSupport);
  strcpy(filmItem.genre, mediaGenre);
  strcpy(filmItem.price, mediaPrice);
  strcpy(filmItem.notes, mediaNotes);
	filmFileWrite = fopen("films.dat", "ab");
	fwrite( &filmItem, sizeof(filmItem), 1, filmFileWrite );
	fclose(filmFileWrite);
printf ("Done!\a");
}
 
void filmList ()
{
	filmFileRead = fopen("films.dat", "rb");
 
	while ( !feof(filmFileRead) ) 
	{
		if (fread( &filmItem, sizeof(filmItem), 1, filmFileRead )) 
		{
			printf("=========================\n");
			printf("Title : %s\n", filmItem.title);
			printf("Author : %s\n", filmItem.author);
			printf("Year : %s\n", filmItem.year);
			printf("Publishing House : %s\n", filmItem.pub_house);
			printf("Genre : %s\n", filmItem.genre);
			printf("Notes : %s\n", filmItem.notes);
		}
	}
 
	fclose(filmFileRead);
}
 
void filmFind ()
{
	printf ("Title::\n");
}
 
/* Musics */
 
void musics ()
{
	int scelta;
 
	do	
	{	do	
		{	printf ("What do you want to do?:\n");
			printf ("[1] Add a music \n");
			printf ("[2] List of musics \n");
			printf ("[3] Find a music \n");
			printf ("[0] Go back\n");
			scanf ("%d", &scelta);
		}while (scelta < 0 || scelta > 3);
 
		switch (scelta)
		{
			case 1:
			{	musicAdd();
				break;
			}
			case 2:
			{	musicList();
				break;
			}
			case 3:
			{	musicFind();
				break;
			}
 
		}
	}while (scelta != 0);
}
	/* Aggiungo un supporto musicale */
void musicAdd ()
{
	printf (" ADD A music: \n");
  gets (nulla);
  /*Title*/
  printf (" TITLE: \t");
  gets (mediaTitle);
  printf ("\n");
  /*Author*/
  printf (" AUTHOR: \t");
  gets (mediaAuthor);
  printf ("\n");
  /*Year*/
  printf (" YEAR: \t");
  gets (mediaYear);
  printf ("\n");
  /*Label*/
  printf (" LABEL: \t");
  gets (mediaLabel);
  printf ("\n");
  /*Medium*/
  printf (" MEDIUM: \t");
  gets (mediaSupport);
  printf ("\n");
  /*Genre*/
  printf (" GENRE: \t");
  gets (mediaGenre);
  printf ("\n");
  /*Price*/
  printf (" PRICE: \n");
  gets (mediaPrice);
  /*Notes*/
  printf (" NOTES: \n");
  gets (mediaNotes);
  printf ("All right?\n");
  strcpy(musicItem.title, mediaTitle);
  strcpy(musicItem.author, mediaAuthor);
  strcpy(musicItem.year, mediaYear);
  strcpy(musicItem.label, mediaLabel);
  strcpy(musicItem.support, mediaSupport);
  strcpy(musicItem.genre, mediaGenre);
  strcpy(musicItem.price, mediaPrice);
  strcpy(musicItem.notes, mediaNotes);
	musicFileWrite = fopen("musics.dat", "ab");
	fwrite( &musicItem, sizeof(musicItem), 1, musicFileWrite );
	fclose(musicFileWrite);
printf ("Done!\a");
}
 
void musicList ()
{
	musicFileRead = fopen("musics.dat", "rb");
 
	while ( !feof(musicFileRead) ) 
	{
		if (fread( &musicItem, sizeof(musicItem), 1, musicFileRead )) 
		{
			printf("=========================\n");
			printf("Title : %s\n", musicItem.title);
			printf("Author : %s\n", musicItem.author);
			printf("Year : %s\n", musicItem.year);
			printf("Publishing House : %s\n", musicItem.pub_house);
			printf("Genre : %s\n", musicItem.genre);
			printf("Notes : %s\n", musicItem.notes);
		}
	}
 
	fclose(musicFileRead);
}
 
 
void musicFind ()
{
	printf ("Title::\n");
}
 
void export()
{
	int scelta;
 
	do	
	{	do	
		{	printf ("What do you want to export?:\n");
			printf ("[1] List of books \n");
			printf ("[2] List of films \n");
			printf ("[3] List of musics\n");
			printf ("[0] Go back\n");
			scanf ("%d", &scelta);
		}while (scelta < 0 || scelta > 3);
 
		switch (scelta)
		{
			case 1:
			{	exportBooks();
				break;
			}
			case 2:
			{	exportFilms();
				break;
			}
			case 3:
			{	exportMusic();
				break;
			}
 
		}
	}while (scelta != 0);
}
 
void exportBooks()
{
	bookFileRead = fopen("books.dat", "rb");
	bookFileWrite = fopen("books.txt", "w");
	fprintf(bookFileWrite,"Begin of book list:\n");
	while ( !feof(bookFileRead) ) 
	{
		if (fread( &bookItem, sizeof(bookItem), 1, bookFileRead )) 
		{
			fprintf(bookFileWrite,"=========================\n");
			fprintf(bookFileWrite,"Title : %s\n", bookItem.title);
			fprintf(bookFileWrite,"Author : %s\n", bookItem.author);
			fprintf(bookFileWrite,"Year : %s\n", bookItem.year);
			fprintf(bookFileWrite,"Publishing House : %s\n", bookItem.pub_house);
			fprintf(bookFileWrite,"Genre : %s\n", bookItem.genre);
			fprintf(bookFileWrite,"Notes : %s\n", bookItem.notes);
		}
	}
	fprintf(bookFileWrite,"###############################\n");
	fprintf(bookFileWrite,"End of book list\n");
	fprintf(bookFileWrite,"###############################\n");
	fclose(bookFileRead);
	fclose(bookFileWrite);
	printf("List of books saved in books.txt file. \a");
	printf("\n");
}
 
void exportFilms()
{
	filmFileRead = fopen("films.dat", "rb");
	filmFileWrite = fopen("films.txt", "w");
	fprintf(filmFileWrite,"Begin of film list:\n");
	while ( !feof(filmFileRead) ) 
	{
		if (fread( &filmItem, sizeof(filmItem), 1, filmFileRead )) 
		{
			fprintf(filmFileWrite,"=========================\n");
			fprintf(filmFileWrite,"Title : %s\n", filmItem.title);
			fprintf(filmFileWrite,"Author : %s\n", filmItem.author);
			fprintf(filmFileWrite,"Year : %s\n", filmItem.year);
			fprintf(filmFileWrite,"Medium : %s\n", filmItem.support);
			fprintf(filmFileWrite,"Genre : %s\n", filmItem.genre);
			fprintf(filmFileWrite,"Notes : %s\n", filmItem.notes);
		}
	}
	fprintf(filmFileWrite,"###############################\n");
	fprintf(filmFileWrite,"End of film list\n");
	fprintf(filmFileWrite,"###############################\n");
	fclose(filmFileRead);
	fclose(filmFileWrite);
	printf("List of films saved in films.txt file. \a");
	printf("\n");
}
 
void exportMusic()
{
	musicFileRead = fopen("musics.dat", "rb");
	musicFileWrite = fopen("musics.txt", "w");
	fprintf(musicFileWrite,"Begin of music list:\n");
	while ( !feof(musicFileRead) ) 
	{
		if (fread( &musicItem, sizeof(musicItem), 1, musicFileRead )) 
		{
			fprintf(musicFileWrite,"=========================\n");
			fprintf(musicFileWrite,"Title : %s\n", musicItem.title);
			fprintf(musicFileWrite,"Author : %s\n", musicItem.author);
			fprintf(musicFileWrite,"Year : %s\n", musicItem.year);
			fprintf(musicFileWrite,"Label : %s\n", musicItem.label);
			fprintf(musicFileWrite,"Medium : %s\n", musicItem.support);
			fprintf(musicFileWrite,"Genre : %s\n", musicItem.genre);
			fprintf(musicFileWrite,"Notes : %s\n", musicItem.notes);
		}
	}
	fprintf(musicFileWrite,"###############################\n");
	fprintf(musicFileWrite,"End of music list\n");
	fprintf(musicFileWrite,"###############################\n");
	fclose(musicFileRead);
	fclose(musicFileWrite);
	printf("List of music saved in music.txt file. \a");
	printf("\n");


 			if (bookItem.title==searchTitle)
 
Mi riferisco all'ultima riga: in quel modo stai verificando che gli 
array di caratteri bookItem.title e searchTitle siano stati allocati a 
partire dalla stessa posizione in memoria. In C il confronto tra 
stringhe va fatto controllando gli array carattere per carattere; per 
far ciň puoi usare la funzione strcmp():
 
                        if(!strcmp(bookItem.title, serarhTitle))


Grazie mille!
Quindi in pratica col codice di prima non confrontavo le stringhe ma
semplicemente la posizione in memoria dei caratteri?



Grazie al vostro aiuto č finalmente nato gestimedia!
https://sourceforge.net/projects/gestimedia/
 


 semplicemente la posizione in memoria dei caratteri?
 
Confrontavi gli indirizzi di memoria dove è stato allocato il primo 
carattere (char) dei due array, che non possono mai essere uguali in 
quanto array distinti. Diverso sarebbe se uno sei due fosse stato un 
puntatore:
 
     char array[] = "stringa array";
     char * p_array;
 
     p_array = array;
 
     printf("\n"
            "array:%X %s p_array:%X"
            "\n"
            "array:   %s"
            "\n"
            "p_array: %s"
            "\n",
            array,
            (array == p_array) ? "e' uguale a"
                               : "e' diverso da",
            p_array,
            array,
            p_array);
 
Ciao.


Ora ho capito tutto...
grazie per la tua spiegazione ;-)
