Went over basic day one things, like what the course will cover and what is expected. Talked about basic commands we use such as copying, editing, deleting and what was actually involved within them. Created a program to read a file and had it display the contents of the file.
Still working on “group of” project, adding animals and groups still isn't functional yet. Created CHMOD function which turned out to be incredible simple and consisted mainly of checking for errors.
Made “ls” or list to list the files in the directory. Learned about “dirent.h” in the process and some of the capabilities of the functions within. DIR * dirPtr, never occurred to me that directory pointers existed but it makes sense since there are files and directories that there would be file pointers and directory pointers. Also had a very interesting experience in computer organization. Created a 2 to four bit decoder and ran Joe's test and it worked first try with no errors. I don't know if I have ever written a program that didn't take a shit on me the first few times I tried to run it.
Maybe forgot that I had an opus to update. Have been working on the projects and code from the book despite what the opus updates would lead you to believe. The Matrix multiplier project was presented a while ago. Figured out the algorithm for multiplying two matrices and then the hard part was added to the assignment with all the options that need to be implemented. Whenever i try to give an argument it accepts the argument and then spits out the help message so some bugs left to work out there. Segmentation faults are on the rise in the group of tool. Created copy and write programs and even ls. Worked with signal handling and even forking and creating child processes which is still challenging to wrap my head around.
“Groupof” Works! i have no idea what my original plans were because that code looked like a bunch of pieces of different puzzles trying to fit together.
Algorithm to find match for the group entered
data = (char*)malloc(sizeof(char)*count); fptr = fopen("group.txt", "r"); for(i=0;i<count;i++) { fscanf(fptr, "%s", &data[i]); if(strcmp(argv[1], &data[i]) == 0) { fscanf(fptr, "%s", &data[i+1]); fscanf(fptr, "%s", &data[i+2]); printf("A group of %s is a %s\n", argv[1], &data[i+2]); break; } fclose(fptr); }
To get count i read the file in a string at a time. Then went through the file a string at a time comparing the string with the argument entered. If a match is found I read the next two in that way I'm not trying to print something that hasn't been read in yet. Then close the file.
List option -l
fptr = fopen("group.txt", "r"); if(fptr == NULL) { fprintf(stdout, "Error opening file\n"); fclose(fptr); exit(1); } count = 0; while(fscanf(fptr, "%c", &i) != EOF) { count++; } fclose(fptr); fopen("group.txt", "r"); data = (char*)malloc(sizeof(char)*count); for(i=0;i<count;i++) { fscanf(fptr, "%c", &data[i]); printf("%c", data[i]); } fclose(fptr);
For this option I read the file in a character at a time then printed out the array. I had to redo count to adjust for using characters instead of strings.
Count -c
printf("%d\n", (count/3));
This one was simple because my file is in the form:“string = string” So dividing by three produced the number of lines or entries.
Add “animal” -a
data2 = (char*)malloc(sizeof(char)*count); printf("Enter the group that this description describes\n"); printf("Example: \n"); printf("lab46:~$./groupof -a lions\n"); printf("pride\n"); scanf("%s", &data2[0]); fptr = fopen("group.txt", "a"); fprintf(fptr, "%s = %s\n", &data2[0], optarg); fclose(fptr);
Display information on how to give input then get the input. Open the file for appending and print the data to the file.
Add group -g
data2 = (char*)malloc(sizeof(char)*count); printf("Enter the group that this description describes\n"); printf("Example: \n"); printf("lab46:~$./groupof -g pride\n"); printf("lions\n"); scanf("%s", &data2[0]); fptr = fopen("group.txt", "a"); fprintf(fptr, "%s = %s\n", optarg, &data2[0]); fclose(fptr);
Same as before just switch data around.
Help -h
printf("-h, --help: Displays usage information.\n"); printf("-l, --list: Lists the groups already contained in the file.\n"); printf("-v, --verbose: Displays specific information on what operations are taling place.\n"); printf("-c, --count: Displays number of entries in file.\n"); printf("-a, --add-description: add a description to the file.\n"); printf("-g, --add-group: Add group name to the file.\n");
Print usage.
Verbose -v
printf("Creating Array...\n"); sleep(1); data = (char*)malloc(sizeof(char)*count); printf("Opening file...\n"); sleep(1); fptr = fopen("group.txt", "r"); printf("Scanning file...\n"); sleep(1); for(i=0;i<count;i++) { fscanf(fptr, "%s", &data[i]); if(strcmp(argv[2], &data[i]) == 0) { printf("Match found!\n"); sleep(1); fscanf(fptr, "%s", &data[i+1]); fscanf(fptr, "%s", &data[i+2]); printf("A group of %s is a %s\n", argv[2], &data[i+2]); sleep(1); break; } } fclose(fptr); printf("Closing file...\n");
This option has a lot more print statements detailing the process occurring but is the same algorithm as the original.