#C195. Simulated File System Operations
Simulated File System Operations
Simulated File System Operations
You are required to implement a simulated file system that supports a set of operations. The file system initially contains no files or directories. You must support the following operations:
- mkdir <path>: Create a directory at the specified
path
. If a file already exists at that path or the path is invalid, printError
. - touch <path>: Create an empty file at the specified
path
. The parent directory must exist; otherwise, printError
. If a file already exists at that location, printError
. - write <path> <data>: Write
data
to the specified file. If the file does not exist or if the parent directory is missing, printError
. - read <path>: Read and print the contents of the specified file. If the file does not exist, print
Error
. - ls <path>: List all entries (both files and directories) in the specified directory in lexicographical order, separated by a single space. If the directory does not exist, print
Error
. If the directory is empty, print an empty line.
Paths are given in Unix-style format (e.g. /home/docs
). All error messages should be exactly Error
(without quotes). Operations that do not produce an output should not print anything.
Note: In case an error occurs during an operation, the simulation should catch it and print Error
for that command, then continue processing subsequent commands.
The operations are provided via standard input and the output should be directed to standard output.
inputFormat
The first line of input contains an integer n indicating the number of commands. Each of the following n lines contains a command. The command formats are as follows:
mkdir <path>
touch <path>
write <path> <data>
(Note:data
can contain spaces)read <path>
ls <path>
Each command is processed sequentially.
outputFormat
For each command that produces an output (read
and ls
) or when an error occurs, print the corresponding result on a new line. In case of an error, print exactly Error
(without quotes).
5
mkdir /home
mkdir /home/docs
touch /home/docs/file.txt
ls /home/docs
ls /home
file.txt
docs
</p>