Advertisement
DOGGYWOOF

Untitled

Jul 31st, 2024 (edited)
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. -- Define the path to the file
  2. local path = "/disk/Info.txt"
  3.  
  4. -- Function to check if the file exists and list its contents
  5. local function checkFileAndListContents(filePath)
  6. -- Attempt to open the file in read mode
  7. local file = fs.open(filePath, "r")
  8.  
  9. if file then
  10. -- File exists, read its contents
  11. print("File exists. Contents:")
  12. local contents = file.readAll()
  13. print(contents)
  14.  
  15. -- Close the file
  16. file.close()
  17. else
  18. -- File does not exist
  19. print("File does not exist.")
  20. end
  21. end
  22.  
  23. -- Function to append data to the file
  24. local function appendDataToFile(filePath)
  25. -- Prompt user for input
  26. print("Enter the following details to add to the file:")
  27.  
  28. write("Name: ")
  29. local name = read()
  30.  
  31. write("House number: ")
  32. local houseNumber = read()
  33.  
  34. write("Job: ")
  35. local job = read()
  36.  
  37. write("Date of Birth (YYYY-MM-DD): ")
  38. local dob = read()
  39.  
  40. write("Gender (M/F/Other): ")
  41. local gender = read()
  42.  
  43. write("Phone Number: ")
  44. local phoneNumber = read()
  45.  
  46. write("Email Address: ")
  47. local email = read()
  48.  
  49. -- Open the file in append mode
  50. local file = fs.open(filePath, "a")
  51.  
  52. -- Write the data to the file
  53. file.writeLine("Name: " .. name)
  54. file.writeLine("House number: " .. houseNumber)
  55. file.writeLine("Job: " .. job)
  56. file.writeLine("Date of Birth: " .. dob)
  57. file.writeLine("Gender: " .. gender)
  58. file.writeLine("Phone Number: " .. phoneNumber)
  59. file.writeLine("Email Address: " .. email)
  60. file.writeLine() -- Add an empty line for better readability
  61.  
  62. -- Close the file
  63. file.close()
  64.  
  65. print("Data has been appended to the file.")
  66. end
  67.  
  68. -- Function to display the main menu and handle user choices
  69. local function mainMenu()
  70. while true do
  71. print("\n--- Main Menu ---")
  72. print("1. Check file and list contents")
  73. print("2. Append data to file")
  74. print("3. Exit")
  75.  
  76. -- Get user choice
  77. local choice = read()
  78.  
  79. if choice == "1" then
  80. -- Check file and list contents
  81. checkFileAndListContents(path)
  82. elseif choice == "2" then
  83. -- Append data to the file
  84. appendDataToFile(path)
  85. elseif choice == "3" then
  86. -- Exit the program
  87. print("Exiting program...")
  88. break
  89. else
  90. print("Invalid choice. Please select a valid option.")
  91. end
  92. end
  93. end
  94.  
  95. -- Automatically load and read the file upon startup
  96. print("Loading and reading the file upon startup...")
  97. checkFileAndListContents(path)
  98.  
  99. -- Display the main menu
  100. mainMenu()
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement