In this assignment, you are asked to complete a program that gets a file name (and path)
as its only command-line argument and shuffles the file content randomly using low-level
I/O functions open, read, write, and lseek. Here is the incomplete program: (See Attached Image. I could not copy paste the code, so I have to screenshot, sorry.)
As you see, the program first opens a file, say “io.txt”, using “O RDWR” mode since it’s
supposed to both read the file and write into it. Then, using lseek, it finds the number of
bytes/characters stored in the file. If we assume that originally, the file “io.txt” contains the
sequence of characters a1, a2, . . . , an, your task is to complete the implementation of main
function so that the program manipulates the file in a way that after termination of the
program, the file contains the same same sequences of characters, but with a random permutation: aπ1
, aπ2
, . . . , aπn where π1, π2, . . . , πn is a random permutation of numbers 1, 2, . . . , n.
For example, if the file content is originally “eat”, after running the program, the file content
must be one of the following six words:
eat, eta, ate, aet, tea, tae
Of course, a correct implementation will output each word with an equal chance of 1/6.
Also, a correct implementation should not assume any upper-bound for the file size.
As the only deliverable of this assignment, you need to submit a .c file.


0 comments