• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Zapis i odczyt z pliku

0 głosów
55 wizyt
pytanie zadane 15 czerwca 2018 w C i C++ przez kietek Początkujący (280 p.)

Witam,mam problem z zapisem i wczytaniem z pliku. Do pliku niby się zapisuje ale nie do końca dobrze, natomiast w ogóle się nie wczytuje.Wyskakuje jeszcze kilka warning. Proszę o pomoc.

In function 'movies_from_file':

warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=]|
warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=]|
warning: format '%d' expects argument of type 'int *', but argument 3 has type 'int' [-Wformat=]|

In function 'search':|
warning: 'return' with a value, in function returning void|
warning: 'return' with a value, in function returning void|
warning: 'return' with a value, in function returning void|

In function 'movies_from_file':|
warning: control reaches end of non-void function [-Wreturn-type]|

 

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

#define N 30

struct movies {
    int id;
    char title[N];
    char producer[N];
    char year[N];
    char type[N];
    int avaibility;
    int renter;

    struct movies *next;

};

struct movies_pointers
{
    struct movies *head , *tail;
}movie;




void load_movie(struct movies *movie){

    puts("Title: ");
    scanf(" %[^\n]s",movie->title);
    puts("Producer: ");
    scanf(" %[^\n]s",movie->producer);
    puts("Publication date: ");
    scanf(" %[^\n]s",movie->year);
    puts("Type of movie: ");
    scanf(" %[^\n]s",movie->type);

    movie->next=NULL;
}

struct movies* add_movie(struct movies_pointers *movies_ptr)
{
	if(!movies_ptr) return NULL;

		struct movies* new_movie = (struct movies *) malloc(sizeof(struct movies));
		if(new_movie){

					   	load_movie(new_movie);
                        new_movie->next=movies_ptr->head;
                        movies_ptr->head=new_movie;
         			   	if(!movies_ptr->tail) movies_ptr->tail = new_movie;
                        new_movie->id = new_movie->next ? new_movie->next->id + 1 : 0;
                        return new_movie;
					   }

					   	printf("Malloc error!\n");
						return new_movie;


}

int how_many_movies(struct movies_pointers movie){
    int wynik=0;

    while(movie.head){
        movie.head=movie.head->next;
        wynik++;
    }
return wynik;

}

void print_movies(struct movies_pointers movie){
    //movie.head->avaibility=0;
    //movie.head->renter=0;
    printf("AMOUNT OF MOVIES IN DATABASE: %d \n\n",how_many_movies(movie) );
    how_many_movies(movie);
    puts("SHOW ALL MOVIES - 1 \nSHOW RENTED MOVIES - 2\nSHOW AVAILABLE MOVIES -3");
    int x;
    scanf("%d",&x);
    switch(x){
    case 1:
    puts("ID \tTITLE \t\t PRODUCER \t\t PUBLICATION DATE \t\t TYPE \t\t AVAIBILITY \t\t RENTER");
    while(movie.head){
          printf("%d \t %s \t\t %s \t\t\t %s \t\t %s \t\t %d \t\t %d\n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year, movie.head->type, movie.head->avaibility, movie.head->renter);
        movie.head=movie.head->next;
    }
    puts(" ");
    break;
    case 2:
        puts("ID \tTITLE \t\t PRODUCER \t\t PUBLICATION DATE \t\t TYPE \t\t AVAIBILITY \t\t RENTER");
    while(movie.head){
            if(movie.head->avaibility==1)
          printf("%d \t %s \t\t %s \t\t\t %s \t\t %s \t\t %d \t\t %d\n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year, movie.head->type, movie.head->avaibility, movie.head->renter);
        movie.head=movie.head->next;
    }
    puts(" ");
    break;
    case 3:
          puts("ID \tTITLE \t\t PRODUCER \t\t PUBLICATION DATE \t\t TYPE \t\t AVAIBILITY \t\t RENTER");
    while(movie.head){
            if(movie.head->avaibility==0)
          printf("%d \t %s \t\t %s \t\t\t %s \t\t %s \t\t %d \t\t %d\n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year, movie.head->type, movie.head->avaibility, movie.head->renter);
        movie.head=movie.head->next;
    }
    puts(" ");
    break;


    }
}

void save_movies(struct movies_pointers *save_movie_ptr)
{
    struct movies *tmp= save_movie_ptr->head;
    if(save_movie_ptr)
    {
    FILE *movies_txt = NULL;
    movies_txt = fopen("movies.txt","a+");
    while(tmp!=NULL)
    {
        fprintf(movies_txt, "%d\n", tmp->id);
        fprintf(movies_txt, "%s\n", tmp->title);
        fprintf(movies_txt, "%s\n", tmp->producer);
        fprintf(movies_txt, "%s\n", tmp->year);
        fprintf(movies_txt, "%s\n", tmp->type);
        fprintf(movies_txt, "%d\n", tmp->avaibility);
        fprintf(movies_txt, "%d\n", tmp->renter);

        tmp = tmp->next;

    }
      fclose(movies_txt);
    }
    else
    {
        printf("Error");
    }

}

struct movies *movies_from_file(struct movies * movies_from_file_ptr){
        if(movies_from_file_ptr){
		FILE * movies_txt = fopen("movies.txt","r");
		while(!feof(movies_txt))
		{
  			  struct movies* new_movie = (struct movies *) malloc(sizeof(struct movies));
			if(new_movie){
                            fscanf(movies_txt," %d\n",new_movie->id);
                            fscanf(movies_txt," %[^\n]s",new_movie->title);
						   	fscanf(movies_txt," %[^\n]s",new_movie->producer);
						   	fscanf(movies_txt," %[^\n]s",new_movie->year);
						   	fscanf(movies_txt," %[^\n]s",new_movie->type);
						   	fscanf(movies_txt," %d\n",new_movie->avaibility);
						   	fscanf(movies_txt," %d",new_movie->renter);

							new_movie->next=movies_from_file_ptr;
							new_movie->id=new_movie->next->id+1;
							movies_from_file_ptr=new_movie;
   							}
		   else
			   	printf("Malloc error\n");
				return new_movie;

		}
            fclose(movies_txt);

	}

}

void rent_a_movie(struct movies_pointers movie){
    int c, m;
    puts("type ID of client and movie: ");
    scanf("%d",&c);
    scanf("%d",&m);
    while(movie.head){
            if(movie.head->avaibility==0){
        if(movie.head->id==m){
            movie.head->avaibility=1;
            movie.head->renter=c;

        }
        else{


    movie.head=movie.head->next;
    }

    }

}
}


void search(struct movies_pointers movie){
    char tag[N];
    puts("SEARCHING BY TITLE - 1 \nSEARCHING BY PRODUCER - 2 \nSEARCHING BY TYPE - 3");
    int x;
    scanf("%d",&x);

        if(x==1){
    puts("Type name of movie you looking for.");
    scanf(" %[^\n]s",tag);
    while(movie.head){
            if(!strcmp(movie.head->title,tag)){
printf("%d \t %s \t\t %s \t\t\t %s \t\t %s \t\t %d \t\t %d\n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year, movie.head->type, movie.head->avaibility, movie.head->renter);
        movie.head=movie.head->next;

            }
        else {puts("no results");
        return -1;

        }


    }
        }

        else if(x==2){

             puts("Type name of producer you looking for.");
    scanf(" %[^\n]s",tag);
    while(movie.head){
            if(!strcmp(movie.head->producer,tag)){
printf("%d \t %s \t\t %s \t\t\t %s \t\t %s \t\t %d \t\t %d\n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year, movie.head->type, movie.head->avaibility, movie.head->renter);
        movie.head=movie.head->next;

            }
            else {puts("no results");
    return -1;}


    }

    }

       else if(x==3){
    puts("Type name of movie you looking for.");
    scanf(" %[^\n]s",tag);
    while(movie.head){
            if(!strcmp(movie.head->type,tag)){
printf("%d \t %s \t\t %s \t\t\t %s \t\t %s \t\t %d \t\t %d\n",movie.head->id, movie.head->title, movie.head->producer, movie.head->year, movie.head->type, movie.head->avaibility, movie.head->renter);
        movie.head=movie.head->next;

            }
        else {puts("no results");
        return -1;
        }
        }
       }
    else {puts("wrong code, try again");
        search(movie);}


}


struct clients {
    int id;
    char name[N];
    char surname[N];
    char phone[N];

    struct clients *next;
};

struct clients_pointers
{
    struct clients *head , * tail;
}client;





void load_client(struct clients *client){
   // puts("Id: ");
   // scanf("%d",client->id);
    puts("Name: ");
    scanf(" %[^\n]s",client->name);
    puts("Surname: ");
    scanf(" %[^\n]s",client->surname);
    puts("Phone number: ");
    scanf(" %[^\n]s",client->phone);

    client->next=NULL;
}

struct clients* add_client(struct clients_pointers *clients_ptr)
{
	if(!clients_ptr) return NULL;
		struct clients *new_client = (struct clients*) malloc(sizeof(struct clients));
		if(new_client){

					   	load_client(new_client);

						new_client->next=clients_ptr->head;
						clients_ptr->head=new_client;
                        if(!clients_ptr->tail) clients_ptr->tail =new_client;
                        new_client->id =new_client->next ? new_client->next->id + 1 : 0;
         			   	return new_client;
					   }

					   	printf("Malloc error!\n");
						return new_client;

}




void print_clients(struct clients_pointers client){

    puts("ID\t NAME \t\t SURNAME \t\t PHONE");
    while(client.head){
          printf("%d \t %s \t\t %s  \t\t %s\n",client.head->id, client.head->name, client.head->surname, client.head->phone);
        client.head=client.head->next;
    }
    puts(" ");
    }


void save_clients(struct clients_pointers *save_clients_ptr)
{
    struct clients *tmp = save_clients_ptr->head;
    if(save_clients_ptr)
    {
    FILE *clients_txt = NULL;
    clients_txt = fopen("clients.txt","a+");
    while(tmp!=NULL)
    {
        fprintf(clients_txt, "%s\n", tmp->name);
        fprintf(clients_txt, "%s\n", tmp->surname);
        fprintf(clients_txt, "%s\n", tmp->phone);
        tmp = tmp->next;

    }
      fclose(clients_txt);
    }
    else
    {
        printf("Error");
    }

}


struct clients *clients_from_file(struct clients *pointer){
	 if(pointer){
		FILE * clients_txt = fopen("clients.txt","r");
		while(!feof(clients_txt))
		{
  			  struct clients* new_client = (struct clients *) malloc(sizeof(struct clients));
			if(new_client){

						   	fscanf(clients_txt,"%s",new_client->name);
						   	fscanf(clients_txt,"%s",new_client->surname);
						   	fscanf(clients_txt,"%s",new_client->phone);

							new_client->next=pointer;
							new_client->id=new_client->next->id+1;
							pointer=new_client;
   							}
		   else{
			   	printf("Malloc error\n");
				return pointer;
		   }
		}
		fclose(clients_txt);
		return pointer;
	}
	else{
		printf("Lack of list");
		return pointer;
	}
}



int main()
{
    struct movies* movies_from_file_ptr=NULL;

    movies_from_file_ptr=movies_from_file(movies_from_file_ptr);

    //movies_from_file(&movie);
    int x;

   puts("0 - CLOSE \t\n1 - LIST OF MOVIES \t\n2 - LIST OF CLIENTS \t\n3 - SEARCHING \t\n4 - ADD MOVIE \t\n5 - ADD CLIENT \t\n6 - RENT A MOVIE ");
    scanf("%d",&x);

    while(x!=0){

    switch(x)
{
case 1: print_movies(movie); break;
case 2: print_clients(client); break;
case 3: search(movie); break;
case 4: add_movie(&movie); break;
case 5: add_client(&client); break;
case 6: rent_a_movie(movie); break;
default: puts("wrong code.");
}
    save_movies(&movie);
    save_clients(&client);
    puts("0 - CLOSE \t\n1 - LIST OF MOVIES \t\n2 - LIST OF CLIENTS \t\n3 - SEARCHING \t\n4 - ADD MOVIE \t\n5 - ADD CLIENT \t\n6 - RENT A MOVIE ");
    scanf("%d",&x);
    }



    return 0;
}


 

1 odpowiedź

0 głosów
odpowiedź 15 czerwca 2018 przez j23 VIP (106,320 p.)
  • Linie 151, 156 i 157: powinno być &new_movie->id, &new_movie->avaibility i &new_movie->renter
  • Linia 165: co ten return tam robi?

 

Podobne pytania

0 głosów
1 odpowiedź 71 wizyt
pytanie zadane 7 grudnia 2018 w C i C++ przez matiibal Użytkownik (500 p.)
0 głosów
1 odpowiedź 52 wizyt
pytanie zadane 3 listopada 2018 w C i C++ przez gorgonek Obywatel (1,220 p.)
0 głosów
2 odpowiedzi 105 wizyt
pytanie zadane 1 stycznia w C i C++ przez nooblike Nowicjusz (120 p.)
Porady nie od parady
Publikując kody źródłowe korzystaj ze specjalnego bloczku koloryzującego składnię (przycisk z napisem code w edytorze). Nie zapomnij o ustawieniu odpowiedniego języka z rozwijanego menu oraz czytelnym formatowaniu kodu.Przycisk code

66,384 zapytań

113,133 odpowiedzi

239,497 komentarzy

46,640 pasjonatów

Przeglądających: 158
Pasjonatów: 0 Gości: 158

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto dwie polecane książki warte uwagi. Pełną listę znajdziesz tutaj.

...