TEXT   88

lv81

Guest on 22nd April 2022 06:51:49 AM

  1. MAIN.C
  2.  
  3. #include <stdio.h>
  4. #include "Header.h"
  5.  
  6. int main(void) {
  7.  
  8.     int inputOption;
  9.     char* fileName = "Clanovi.txt";
  10.  
  11.     do {
  12.  
  13.         printMenu();
  14.  
  15.         scanf("%d", &inputOption); getchar();
  16.  
  17.         switch (inputOption) {
  18.             case 1:
  19.                 createFile(fileName);
  20.                 break;
  21.             case 2:
  22.                 readFile(fileName);
  23.                 break;
  24.             case 3:
  25.                 saveMember(fileName);
  26.                 break;
  27.             case 4:
  28.                 exit(EXIT_SUCCESS);
  29.             default:
  30.                 puts("Ta operacija ne postoji!\n");
  31.                 break;
  32.         }
  33.     } while (inputOption != 4);
  34.  
  35.     return 0;
  36. }
  37.  
  38.  
  39. HEADER.H
  40.  
  41. #ifndef Header_h
  42. #define Header_h
  43.  
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46.  
  47. typedef struct memberInfo {
  48.     char lName[20];
  49.     char fName[20];
  50.     int code;
  51.     char address[100];
  52.     int phoneNumber;
  53. } MEMBER;
  54.  
  55. void printMenu(void);
  56. void createFile(char* fileName);
  57. void readFile(char* fileName);
  58. void saveMember(char* fileName);
  59.  
  60. #endif /* Header_h */
  61.  
  62.  
  63. FUNCTIONS.C
  64.  
  65. #include "Header.h"
  66.  
  67. void printMenu(void) {
  68.     printf("Pick your options: \n");
  69.     printf("1- Kreiranje datoteke Clanovi.txt\n");
  70.     printf("2- Citanje iz datoteke Clanovi.txt\n");
  71.     printf("3- Dodavanje novih clanova u Clanovi.txt\n");
  72.     printf("4- Zavrsetak programa.\n");
  73. }
  74.  
  75. void createFile(char* fileName) {
  76.     FILE* file = fopen(fileName, "ab");
  77.     puts("\nFile created.\n");
  78.     fclose(file);
  79. }
  80.  
  81. void readFile(char* fileName) {
  82.     FILE* inFile = fopen(fileName, "rb");
  83.     MEMBER member;
  84.     if (inFile != NULL) {
  85.         while (fread(&member, sizeof(MEMBER), 1, inFile) == 1) {
  86.             printf("\nName: %s %s\n", member.fName, member.lName);
  87.             printf("ID: %d\nPhone num: %d\n", member.code, member.phoneNumber);
  88.             printf("Address: %s\n", member.address);
  89.         }
  90.         fclose(inFile);
  91.     }
  92.     puts("\n---- END OF FILE ----\n");
  93. }
  94.  
  95. void saveMember(char* fileName) {
  96.     FILE* file = fopen(fileName, "ab");
  97.  
  98.     MEMBER member;
  99.     printf("Enter member first name: ");
  100.     scanf(" %[^\n]", member.fName);
  101.  
  102.     printf("Enter member last name: ");
  103.     scanf(" %[^\n]", member.lName);
  104.  
  105.     printf("Enter member code: ");
  106.     scanf("%d", &member.code);
  107.     getchar();
  108.  
  109.     printf("Enter memeber address: ");
  110.     scanf(" %[^\n]", member.address);
  111.  
  112.     printf("Enter member phone number: ");
  113.     scanf("%d", &member.phoneNumber); getchar();
  114.  
  115.     fwrite(&member, sizeof(MEMBER), 1, file);
  116.  
  117.     puts("\nMember added.\n");
  118.  
  119.     fclose(file);
  120. }
  121.  
  122.  
  123.  
  124.  
  125. NEW MAIN.C
  126.  
  127.  
  128. #include <stdio.h>
  129. #include <stdlib.h>
  130.  
  131. typedef struct memberInfo {
  132.     char lName[20];
  133.     char fName[20];
  134.     int code;
  135.     char address[100];
  136.     int phoneNumber;
  137. } MEMBER;
  138.  
  139. void printMenu(void);
  140. void createFile(char* fileName);
  141. void readFile(char* fileName);
  142. void saveMember(char* fileName);
  143.  
  144. int main(void) {
  145.  
  146.     int inputOption;
  147.     char* fileName = "Clanovi.txt";
  148.  
  149.     do {
  150.         printMenu();
  151.         scanf("%d", &inputOption); getchar();
  152.         switch (inputOption) {
  153.             case 1:
  154.                 createFile(fileName);
  155.                 break;
  156.             case 2:
  157.                 readFile(fileName);
  158.                 break;
  159.             case 3:
  160.                 saveMember(fileName);
  161.                 break;
  162.             case 4:
  163.                 exit(EXIT_SUCCESS);
  164.             default:
  165.                 puts("Ta operacija ne postoji!\n");
  166.                 break;
  167.         }
  168.     } while (inputOption != 4);
  169.  
  170.     return 0;
  171. }
  172.  
  173. void printMenu(void) {
  174.     printf("Pick your options: \n");
  175.     printf("1- Kreiranje datoteke Clanovi.txt\n");
  176.     printf("2- Citanje iz datoteke Clanovi.txt\n");
  177.     printf("3- Dodavanje novih clanova u Clanovi.txt\n");
  178.     printf("4- Zavrsetak programa.\n");
  179. }
  180.  
  181. void createFile(char* fileName) {
  182.     FILE* file = fopen(fileName, "ab");
  183.     puts("\nFile created.\n");
  184.     fclose(file);
  185. }
  186.  
  187. void readFile(char* fileName) {
  188.     FILE* inFile = fopen(fileName, "rb");
  189.     MEMBER member;
  190.     if (inFile != NULL) {
  191.         while (fread(&member, sizeof(MEMBER), 1, inFile) == 1) {
  192.             printf("\nName: %s %s\n", member.fName, member.lName);
  193.             printf("ID: %d\nPhone num: %d\n", member.code, member.phoneNumber);
  194.             printf("Address: %s\n", member.address);
  195.         }
  196.         fclose(inFile);
  197.     }
  198.     puts("\n---- END OF FILE ----\n");
  199. }
  200.  
  201. void saveMember(char* fileName) {
  202.     FILE* file = fopen(fileName, "ab");
  203.  
  204.     MEMBER member;
  205.     printf("Enter member first name: ");
  206.     scanf(" %[^\n]", member.fName);
  207.  
  208.     printf("Enter member last name: ");
  209.     scanf(" %[^\n]", member.lName);
  210.  
  211.     printf("Enter member code: ");
  212.     scanf("%d", &member.code);
  213.     getchar();
  214.  
  215.     printf("Enter memeber address: ");
  216.     scanf(" %[^\n]", member.address);
  217.  
  218.     printf("Enter member phone number: ");
  219.     scanf("%d", &member.phoneNumber); getchar();
  220.  
  221.     fwrite(&member, sizeof(MEMBER), 1, file);
  222.  
  223.     puts("\nMember added.\n");
  224.  
  225.     fclose(file);
  226. }

Raw Paste


Login or Register to edit or fork this paste. It's free.