Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "structs.h"
- // void PrintRecord(struct user ben); TEST
- int CaseCheck(char a, char b);
- void AddRecord(int i, int userNInput, struct user record[100]);
- void ReadFile(char singleLine[150], FILE * fPointer);
- void WriteFile(char userWInput);
- void AppendFile(char AppendInput);
- int main()
- {
- /*struct user ben;
- struct user emily; */ // TEST
- struct user record[100];
- char userInput;
- char singleLine[150];
- char userWInput = '\0';
- char AppendInput = '\0';
- int userNInput;
- int i = '\0';
- FILE * fPointer;
- while(1) {
- printf("\nWelcome to the main menu");
- printf("\n\nA) Add new record \nB) Read file\nC) Write File\nD) Append file\nE) Read written file\nQ) Quit\n\n");
- printf("Select choice\n> ");
- scanf(" %c", &userInput);
- if(CaseCheck(userInput, 'a')) {
- printf("\nAdd new record\n");
- printf("How many records do you want to add: ");
- scanf("%d", &userNInput);
- AddRecord(i, userNInput, record);
- } else if (CaseCheck(userInput, 'b')) {
- printf("\nRead file\n\n");
- fPointer = fopen ("Example_Data.ipb", "r"); //Opens file. 'r' means to read the file.
- ReadFile(singleLine, fPointer);
- } else if (CaseCheck(userInput, 'c')) {
- printf("\nWrite File\n");
- WriteFile(userWInput);
- } else if (CaseCheck(userInput, 'd')) {
- printf("\nAppend file\n");
- AppendFile(AppendInput);
- } else if (CaseCheck(userInput, 'e')) {
- printf("\nRead file(s)\n");
- fPointer = fopen ("WriteFile.ipb", "r"); //Opens file. 'r' means to read the file.
- ReadFile(singleLine, fPointer);
- } else if (CaseCheck(userInput, 'q')) {
- exit(0);
- }
- }
- /* printf("\nEnter customerID: ");
- scanf(" %d", &ben.customerID);
- printf("\nEnter time in 2016-12-11 12:34:56 format: ");
- scanf(" %[^\n]s", ben.timestamp); // '[^\n]' allows the user to type information including whitespaces.
- printf("\nEnter domain: ");
- scanf("%s", ben.domain);
- PrintRecord(ben); */ // TEST
- return 0;
- }
- /* void PrintRecord(struct user ben)
- {
- printf("\ncustomerID: %d\n", ben.customerID);
- printf("TimeStamp: %s\n", ben.timestamp);
- printf("Domain: %s\n", ben.domain);
- printf("%s: User %d visited %s\n", ben.timestamp, ben.customerID, ben.domain);
- }*/ // TEST
- int CaseCheck(char a, char b) {
- if (a==b || a+32 == b || a == b+32){
- return 1;
- } else {
- return 0;
- }
- }
- //DONE
- void AddRecord(int i, int userNInput, struct user record[100]) {
- for(i=0; i < userNInput; i++) {
- printf("Enter customerID\n> ");
- scanf(" %d", &record[i].customerID);
- printf("Enter timestamp\n> ");
- scanf(" %[^\n]s", record[i].timestamp);
- printf("Enter domain\n> ");
- scanf(" %s", record[i].domain);
- printf("\n%s: User %d has visited %s\n\n", record[i].timestamp, record[i].customerID, record[i].domain);
- }
- if (i == 1){
- printf("There is 1 record stored.");
- } else if (i > 1) {
- printf("There are %d records stored. ", i);
- } else {
- printf("There are no records stored.");
- }
- }
- // DONE
- void ReadFile(char singleLine[150], FILE * fPointer) {
- while(!feof(fPointer)) { // Loops until file is printed.
- fgets(singleLine, 150, fPointer); // Read the file line by line
- puts(singleLine); // Print file line by line
- }
- fclose(fPointer);
- }
- // IN PROGRESS
- void WriteFile(char WriteInput) {
- FILE *fPointer;
- if( (fPointer = fopen( "WriteFile.ipb","w" )) != NULL )
- {
- fputs( "TEST", fPointer );
- fputs( "\nTEST1", fPointer );
- // fputc( '\n', fPointer );
- fclose( fPointer );
- }
- else
- printf( "error message\n" );
- }
- // IN PROGRESS
- void AppendFile(char AppendInput) {
- FILE * fPointer;
- fPointer = fopen ("Example_Data.ipb", "a");
- printf("Enter information\n> ");
- scanf(" %[^\n]s\n", &AppendInput);
- fprintf(fPointer," %s", &AppendInput);
- fclose(fPointer);
- }
Advertisement
Add Comment
Please, Sign In to add comment