CS代考 SOFT3410 Tutorial 2 Standard Library, Files and Function Pointers

SOFT3410 Tutorial 2 Standard Library, Files and Function Pointers
Question 1: Files
We are able to access files on your system using the file api functions and types. The FILE type is a portable abstraction for interacting with files and streams on your system. Once opened, you can use functionslikefwrite, fgets, fread, fputs, …toreadorwrite.
int main() {
FILE* f = fopen(“numbers.txt”, “r”); char buf[512];
while(fgets(buf, 512, f)) {
printf(“%s”, buf);
}
fclose(f); }
Write an application that will generate a list of numbers and write them to a file. You will need to use the rand function from stdlib.h to do this.
Please refer to the man pages when looking up library functions and system calls
Question 2: Limit
Construct a program that will accept three command line arguments. The first argument specifies if the program will read from the beginning or the end of the file. The second argument specifies the number of lines it will output. The last argument is the file name which you will open and read from.
Try and solve the following in order.
• Solve for only reading the first n lines
• Rewrite your solution to not use fgets, instead create a function to read character by character and check for a new line
• Change your logic to allow for reading from the end of the file Note: Do not use a variable length array.
1

SOFT3410 Standard Library, Files and Function Pointers
Question 3: Word Counting
You have been tasked with recreating the wc unix tool. This will need to accept a file and count the number of characters, words and lines contained within it. You can assume that each character is ASCII encoded.
1 ./wc /usr/share/dict/words
You can consider a word as any string between spaces. The escape character sequence for a line is
\n.
Your application should have a similar output format to wc.
1 ./wc words.txt
2 1068 356 100
Construct some test cases to make sure your logic is correct and counts the correct number of charac- ters, words and lines.
Question 4: Stitching
Your program will be given a set of command line arguments, each argument representing a file path. You will need to use each argument to open a file and print the contents to a column. Each column should be separated with a tab. This command is similar to a unix tool paste.
Example output with the following command
1 ./stitch stock.txt prices.txt
1 Lemon 3.00
2 Apple 2.50
3 Soda 4.50
4 Orange 1.75
Note: Try to avoid using dynamic allocation for this problem.
Question 5: Function pointers
Pointers are very useful creatures, on top of providing a form of indirection to data, it can be used to store the address of a function. By now you should be familiar with pointers, pointing to data. However we have access to function pointers in C. You can somewhat relate this to an interface in Java.
#include
int multiply(int x, int y) { return x * y;
}
Concurrency
Page 2 of 4

SOFT3410 Standard Library, Files and Function Pointers
int add(int x, int y) { return x + y;
}
int main() {
int (*fn)(int, int) = multiply; printf(“%d\n”, fn(2, 5));
fn = add;
printf(“%d\n”, fn(2, 5));
return 0; }
The above example shows that we are able to create a pointer and specify the type to match a functions signature. Given that the functions multiple and add have matching signatures, we are able to assign a function’s address to the variable fn and invoke it.
• When would you use a function pointers?
• Construct a function that will accept a function pointer
• Discusswithyourtutorthefunctionyouhavecreatedandwhyyouhaveusedafunctionpointer
Question 6: Sort – Homework
You are to construct a program that will read the contents of a file and output the contents in a sorted order to stdout. The program can sort the data by their alpha-numeric or numerical representation. Each line of the file can be considered a single entry by your program.
Your program will accept the following command line arguments:
• First argument is the file name, this is required
• Secondoptionalargumentspecifiestheoutputorderaseitherascendingorder(-a)ordescending (-d), by default it is ascending if the argument is omitted
• Third optional argument specifies the comparison method, if the contents to be compared by their alpha-numeric (-alph) or numerical (-num) representation, default is alpha-numeric repre- sentation
You can use the following example of the file below being sorted.
1 Orange
2 Crepe
3 Cake
4 Apple
5 Banana
Concurrency
Page 3 of 4

SOFT3410 Standard Library, Files and Function Pointers
1 ./sorter words.txt -a -alph
1 Apple
2 Banana
3 Cake
4 Crepe
5 Orange
However, there are some rules your program will need to adhere to.
• No dynamic memory
• No VLAs
• Minimise code repetition
• No global or static variables • No invalid memory access
• No bubble sort or bogosort
For your submission, create a github repository with the following name soft3410hw2 and invite your tutor to your repository.
You will need to supply the following as part of your submission.
• Place all .c source files in your src folder
• Test cases must be in a folder called test
• Supply a makefile or a script to compile your program
• You must supply a README.md file that specifies how to build your program and a small description of your project and how it solves the problem
• You must ensure that it compiles and executes correctly on linux
You must make a submission to the master branch by 11:59pm, 13th September, 2020. Please make sure your last commit and push occurs before this time.
Concurrency Page 4 of 4