Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define the path to the file
- local path = "/disk/Info.txt"
- -- Function to check if the file exists and list its contents
- local function checkFileAndListContents(filePath)
- -- Attempt to open the file in read mode
- local file = fs.open(filePath, "r")
- if file then
- -- File exists, read its contents
- print("File exists. Contents:")
- local contents = file.readAll()
- print(contents)
- -- Close the file
- file.close()
- else
- -- File does not exist
- print("File does not exist.")
- end
- end
- -- Function to append data to the file
- local function appendDataToFile(filePath)
- -- Prompt user for input
- print("Enter the following details to add to the file:")
- write("Name: ")
- local name = read()
- write("House number: ")
- local houseNumber = read()
- write("Job: ")
- local job = read()
- write("Date of Birth (YYYY-MM-DD): ")
- local dob = read()
- write("Gender (M/F/Other): ")
- local gender = read()
- write("Phone Number: ")
- local phoneNumber = read()
- write("Email Address: ")
- local email = read()
- -- Open the file in append mode
- local file = fs.open(filePath, "a")
- -- Write the data to the file
- file.writeLine("Name: " .. name)
- file.writeLine("House number: " .. houseNumber)
- file.writeLine("Job: " .. job)
- file.writeLine("Date of Birth: " .. dob)
- file.writeLine("Gender: " .. gender)
- file.writeLine("Phone Number: " .. phoneNumber)
- file.writeLine("Email Address: " .. email)
- file.writeLine() -- Add an empty line for better readability
- -- Close the file
- file.close()
- print("Data has been appended to the file.")
- end
- -- Function to display the main menu and handle user choices
- local function mainMenu()
- while true do
- print("\n--- Main Menu ---")
- print("1. Check file and list contents")
- print("2. Append data to file")
- print("3. Exit")
- -- Get user choice
- local choice = read()
- if choice == "1" then
- -- Check file and list contents
- checkFileAndListContents(path)
- elseif choice == "2" then
- -- Append data to the file
- appendDataToFile(path)
- elseif choice == "3" then
- -- Exit the program
- print("Exiting program...")
- break
- else
- print("Invalid choice. Please select a valid option.")
- end
- end
- end
- -- Automatically load and read the file upon startup
- print("Loading and reading the file upon startup...")
- checkFileAndListContents(path)
- -- Display the main menu
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement