Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MOBILE_LENGTH 11
- typedef struct {
- char name[256], mobile_number[MOBILE_LENGTH], book_name[256], author_name[256];
- int cost;
- }data;
- typedef enum {
- false,
- true
- }bool;
- int press(void);
- void save_data(data client);
- void flush(void);
- void get_data(data *client);
- bool continue_or_not(void);
- void print_data(void);
- int main(void) {
- data client;
- FILE *p;
- char file_name[MOBILE_LENGTH + 4];
- int numb;
- while(true) {
- numb = press();
- switch(numb) {
- case 1:
- get_data(&client);
- save_data(client);
- break;
- case 2:
- print_data();
- }
- if(continue_or_not())
- continue;
- else
- break;
- }
- return 0;
- }
- int press(void) {
- int temp;
- printf("press 1 for sign-up.\n");
- printf("press 2 to enter books.");
- while(true) {
- printf("\n> ");
- scanf("%d", &temp);
- if(temp == 1)
- return 1;
- else if(temp == 2)
- return 2;
- }
- }
- void save_data(data client) {
- FILE *temp;
- char file_name[MOBILE_LENGTH + 4];
- strcpy(file_name, client.mobile_number);
- strcat(file_name, ".txt");
- temp = fopen(file_name, "a+");
- if(temp == NULL) {
- fprintf(stderr, "error epenning the file.");
- exit(EXIT_FAILURE);
- }
- fprintf(temp, "%s%s\n%s%s%d\n", client.name, client.mobile_number, client.book_name, client.author_name, client.cost);
- fclose(temp);
- return;
- }
- void flush(void) {
- int flush = 0;
- while((flush = getchar()) != '\n' && flush != EOF);
- return;
- }
- void get_data(data *client) {
- flush();
- printf("customer name : ");
- fgets(client->name, 256, stdin);
- printf("mobile number : ");
- fgets(client->mobile_number, MOBILE_LENGTH, stdin);
- flush();
- printf("enter a book name : ");
- fgets(client->book_name, 256, stdin);
- printf("book author : ");
- fgets(client->author_name, 256, stdin);
- printf("cost of the book : ");
- scanf("%d", &client->cost);
- return;
- }
- bool continue_or_not(void) {
- char answer;
- while(true) {
- printf("would you like to continue ? (y/n) ");
- scanf(" %c", &answer);
- if(answer == 'y' || answer == 'Y')
- return true;
- else if(answer == 'n' || answer == 'N')
- return false;
- }
- }
- void print_data(void) {
- char file_name[MOBILE_LENGTH + 4];
- FILE *temp;
- data client;
- flush();
- printf("enter a mobile number : ");
- fgets(file_name, MOBILE_LENGTH, stdin);
- strcat(file_name, ".txt");
- temp = fopen(file_name, "r");
- if(temp == NULL) {
- printf("\nthis number isn't registered.\n");
- return;
- }
- fscanf(temp, "%s", client.name);
- fscanf(temp, "%s", client.mobile_number);
- fscanf(temp, "%s", client.book_name);
- fscanf(temp, "%s", client.author_name);
- fscanf(temp ,"%d", &client.cost);
- fclose(temp);
- printf("--------------------------------------\n");
- printf("name : %s\n", client.name);
- printf("mobile : %s\n", client.mobile_number);
- printf("--------------------------------------\n");
- printf("book : %s\n", client.book_name);
- printf("author : %s\n", client.author_name);
- printf("cost : %d\n", client.cost);
- printf("--------------------------------------\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement