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

Wczytywanie informacjiz pliku

Object Storage Arubacloud
0 głosów
131 wizyt
pytanie zadane 18 czerwca 2018 w C i C++ przez kietek Początkujący (280 p.)

Podczas wczytywania z pliku dane pierwszego filmu wczytują się dwa razy, reszta normalnie. Pewnie coś jest nie tak z funkcja "movies_from_file" lecz nie wiem dokładanie co. Proszę o pomoc.

#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","w");
    if(movies_txt){
    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 movies_from_file(struct movies * movies_from_file_ptr, struct movies_pointers *movies_ptr){

        int id;
        char title[30];
        char producer[30];
        char year[30];
        char type[30];
        int avaibility;
        int renter;
		FILE *movies;
        movies = fopen("movies.txt","r");
        if(movies){

		while(!feof(movies))
		{


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

                            fscanf(movies,"%d",&id);
                            fscanf(movies," %[^\n]s",title);
						   	fscanf(movies," %[^\n]s",producer);
						   	fscanf(movies," %[^\n]s",year);
						   	fscanf(movies," %[^\n]s",type);
						   	fscanf(movies,"%d",&avaibility);
						   	fscanf(movies,"%d",&renter);



if(new_movie){
                            new_movie->id=id;
						   	strncpy(new_movie->title,title,N);
						   	strncpy(new_movie->producer,producer,N);
						   	strncpy(new_movie->year,year,N);
						   	strncpy(new_movie->type,type,N);
						   	new_movie->avaibility=avaibility;
						   	new_movie->renter=renter;

                            new_movie->next=movies_ptr->head;

                            movies_ptr->head=new_movie;



}
							//new_movie->next=movies_from_file_ptr;
							//new_movie->id=new_movie->next->id+1;
							//movies_from_file_ptr=new_movie;
   				}
        }
        else{
            puts("blad pliku");
        }


	fclose(movies);

}



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","w");
    while(tmp!=NULL)
    {
        fprintf(clients_txt, "%d\n", tmp->id);
        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");
    }

}


void clients_from_file(struct clients * clients_from_file_ptr, struct clients_pointers * clients_ptr){

    int id;
    char name[N];
    char surname[N];
    char phone[N];

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

        fscanf(clients,"%d",&id);
        fscanf(clients," %[^\n]s",name);
        fscanf(clients," %[^\n]s",surname);
        fscanf(clients," %[^\n]s",phone);
        if(new_client){
            new_client->id=id;
            strncpy(new_client->name,name,N);
            strncpy(new_client->surname,surname,N);
            strncpy(new_client->phone,phone,N);

            new_client->next=clients_ptr->head;
            clients_ptr->head=new_client;

        }


    }
    }
    else{
        puts("\nblad pliku");
    }

fclose(clients);

}


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


   struct clients* clients_from_file_ptr = NULL;








    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 \n7 - SAVE\n8 - LOAD FROM FILE ");
    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;
case 7: save_movies(&movie);
        save_clients(&client);
        break;
case 8: movies_from_file(movies_from_file_ptr, &movie);
        clients_from_file(clients_from_file_ptr, &client);
        break;
default: puts("wrong code.");
}

    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 \n7 - SAVE\n8 - LOAD FROM FILE");
    scanf("%d",&x);
    }



    return 0;
}


 

komentarz 18 czerwca 2018 przez Sedi Stary wyjadacz (10,200 p.)
Sam to pisałeś?
komentarz 18 czerwca 2018 przez kietek Początkujący (280 p.)
Kolega pisał i też się głowi co jest nie tak.

1 odpowiedź

+1 głos
odpowiedź 18 czerwca 2018 przez j23 Mędrzec (194,920 p.)
wybrane 18 czerwca 2018 przez kietek
 
Najlepsza

Pętlę czytającą zrób tak:

		while(fscanf(movies,"%d",&id) == 1 &&
			fscanf(movies," %[^\n]s",title) == 1 &&
			fscanf(movies," %[^\n]s",producer) == 1 &&
			fscanf(movies," %[^\n]s",year) == 1 &&
			fscanf(movies," %[^\n]s",type) == 1 &&
			fscanf(movies,"%d",&avaibility) == 1 &&
			fscanf(movies,"%d",&renter) == 1)
		{
			struct movies* new_movie = (struct movies *) malloc(sizeof(struct movies));

			new_movie->id=id;
			strncpy(new_movie->title,title,N);
			strncpy(new_movie->producer,producer,N);
			strncpy(new_movie->year,year,N);
			strncpy(new_movie->type,type,N);
			new_movie->avaibility=avaibility;
			new_movie->renter=renter;
			new_movie->next = movies_ptr->head;
			movies_ptr->head = new_movie;
		}

 

komentarz 18 czerwca 2018 przez kietek Początkujący (280 p.)

Dzięki wielkiesmiley

 

Podobne pytania

0 głosów
2 odpowiedzi 156 wizyt
pytanie zadane 15 maja 2016 w C i C++ przez DragonCoder Nałogowiec (36,500 p.)
0 głosów
2 odpowiedzi 356 wizyt
pytanie zadane 8 lipca 2015 w C i C++ przez niezalogowany
0 głosów
4 odpowiedzi 782 wizyt
pytanie zadane 18 maja 2016 w Java przez jaroslaw.slaby Początkujący (410 p.)

92,555 zapytań

141,402 odpowiedzi

319,540 komentarzy

61,938 pasjonatów

Motyw:

Akcja Pajacyk

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

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...