Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define SIZE 10000
- int TestIfFileExists(char *FileName);
- void meny(int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp);
- struct RegisterNewProduct(storage s[],int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp);
- struct storage
- {
- int ProductId;
- char ProductName[30];
- int StorageBalance;
- };
- //typedef struct storage Storage;
- int TestIfFileExists(char *FileName);
- int main()
- {
- int ProductId, StorageBalance;
- char ProductName[20];
- struct storage s[SIZE];
- //int size = sizeof(s)/sizeof(s[0]);
- char FileName[100];
- printf("Enter filename (xxx.txt): ");
- scanf("%s", FileName);
- FILE *fp;
- int TrueFalse = TestIfFileExists(FileName);
- int i = 0;
- int AmountToScan = 0;
- int n = 0;
- char Buffer[100];
- if (TrueFalse)
- {
- printf("Reading existing file: %s\n", FileName);
- fp = fopen(FileName, "r+");
- while (!feof(fp))
- {
- fgets(Buffer, 100, fp);
- AmountToScan++;
- }
- fclose(fp);
- fp = fopen(FileName, "r+");
- AmountToScan--;
- while (i != AmountToScan)
- {
- fscanf(fp, "%d", &s[i].ProductId);
- fscanf(fp, "%s", s[i].ProductName);
- fscanf(fp, "%d", &s[i].StorageBalance);
- printf("The product id is: %d\n", s[i].ProductId);
- printf("The product name is: %s\n", s[i].ProductName);
- printf("The storage balance is: %d\n", s[i].StorageBalance);
- n++;
- i++;
- }
- fclose(fp);
- fp = fopen(FileName, "a");
- meny(ProductId, ProductName, StorageBalance, AmountToScan, fp);
- fclose(fp);
- }
- if (!TrueFalse)
- {
- printf("Creating new file: %s \n", FileName);
- fp = fopen(FileName, "w");
- struct storage s[SIZE] = { 0 };
- }
- }
- int TestIfFileExists(char *FileName)
- {
- FILE *fp;
- if (fp = fopen(FileName, "r"))
- {
- fclose(fp);
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void meny(int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp)
- {
- int MenyChoise;
- int i = 0;
- do
- {
- printf("Would you like to:\nRegister a new product(1)?\nPrint out all products(2)?\nSearch for Product(3)?\nChange storage balance(4)?\nSort products(5)?\nUnregister Product(6)?\nTerminate program(0)?\n");
- printf("Enter your choise: ");
- scanf("%d",&MenyChoise);
- if (MenyChoise == 1)
- {
- RegisterNewProduct(ProductId, ProductName, StorageBalance, AmountToScan, fp);
- }
- }
- while(MenyChoise != 0);
- }
- struct RegisterNewProduct(storage s[],int ProductId, char ProductName[], int StorageBalance, int AmountToScan, FILE *fp)
- {
- fprintf(fp,"%d", s[0].ProductId);
- printf("%d",s[0].ProductId);
- int i;
- {
- i = ++AmountToScan;
- AmountToScan--;
- printf("%d\n",i);
- printf("Enter the product id: ");
- scanf("%d",&s[i].ProductId);
- for (i=0; i < AmountToScan; i++)
- {
- if ((s[AmountToScan].ProductId) != (s[i].ProductId))
- {
- fprintf(fp,"%d", s[i].ProductId);
- printf("Enter the product name is: ");
- scanf("%s",&s[i].ProductName);
- fprintf(fp," %s", s[i].ProductName);
- printf("Enter the storage balance: ");
- scanf("%d",&s[i].StorageBalance);
- fprintf(fp," %d\n", s[i].StorageBalance);
- }
- else
- {
- printf("error!\n");
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement