The following function does what it wants, at least with the question data. I tried to make the function as general as possible but you never know.
ler_txt <- function(file, path = "."){
if(path != "."){
old_dir <- setwd(path)
on.exit(setwd(old_dir))
}
txt <- readLines(file)
txt <- txt[nchar(txt) > 0]
nlinhas <- length(txt)
linha <- 1
out_list <- list()
while(linha < nlinhas){
dims <- scan(text = txt[linha])
out <- matrix(NA, nrow = dims[1], ncol = dims[2])
for(i in seq_len(dims[1])){
out[i, ] <- scan(text = txt[linha + i])
}
linha <- linha + dims[1] + 1L
out_list[[length(out_list) + 1]] <- out
}
out_list
}
ler_txt(file = "dados.txt")
#[[1]]
# [,1] [,2] [,3]
#[1,] 1 2 3
#[2,] 4 5 6
#
#[[2]]
# [,1] [,2] [,3] [,4]
#[1,] 1 2 3 4
#[2,] 5 6 7 8
#[3,] 9 10 11 12
#[4,] 13 14 15 16