I have been trying to remove a user from a text file in Haskell with no success. I know how to read from the file, as well as writing to it. We have a function that is called findUser that simply finds the user in the text file and returns a list with the user's password, username and account balance. The username is unique. When we want to add balance to the user's account, we need to get the user's old balance and add that to the asked amount by the user. That has already been done. But the problem is that now that we have the new user with the updated balance, we append that to the text file. But then we have to delete the old user with the old balance.
That is where we are stuck at, at the moment. All help would be appreciated. Below is the code that we are trying to get to delete the old user.
-- = Comment
-- oldUserBalance -> addBalance -> IO ()
addNewUserBalanceToFile :: [String] -> Int -> IO ()
addNewUserBalanceToFile (userName:userPassword:userBalance:) addBalance = do
let
updatedBalance = [userName,userPassword,show(read userBalance + addBalance)]
appendNewUser = unwords updatedBalance
fileName = "users.txt"
handle <- openFile fileName ReadMode -- Opens the file so that we can read from it
contents <- hGetContents handle -- Store everything in the file users.txt
let
users = lines contents
testDeleteUser = delete (unwords $ findUser userName $ words contents) $ words contents
print testDeleteUser
putStrLn "These are the users"
putStr $ unlines users
appendFile fileName $ appendNewUser ++ "\n"
hClose handle
addNewUserBalanceToFile _ _ = return ()