如何通过lua编辑文件中的特定行文本?

发布于 2025-01-07 11:31:43 字数 563 浏览 2 评论 0原文

我正在 Corona SDK 中制作一个应用程序,它从 SD 卡读取 .txt 文件,以更清晰的形式呈现数据,然后允许您对其进行编辑。 一些示例文本:

#207 USER PREFERENCES Time Between Pressure Log Samples
207=15
#208 USER PREFERENCES Auto Print Each Pressure Log Sample
208=No
#209 USER PREFERENCES Auto Print Each Event Log Entry
209=No
#210 USER PREFERENCES Selective Range Printing
210=1

基本上,我需要能够使用纯 Lua 从文件中获取特定的文本行,对其进行编辑,然后将其放回原处。例如,我可能想将 208=No 更改为 208=Yes,而不更改文件中的其他任何内容。

我已经搜索过这个网站、Google 和 Corona 的 API 页面,但似乎没有找到我要找的东西。我是否必须读取该行之前和该行之后的所有文件并将其连接起来?

I'm making an app in the Corona SDK that reads a .txt file from an SD card, presents the data in a cleaner form, then allows you to edit it.
Some example text:

#207 USER PREFERENCES Time Between Pressure Log Samples
207=15
#208 USER PREFERENCES Auto Print Each Pressure Log Sample
208=No
#209 USER PREFERENCES Auto Print Each Event Log Entry
209=No
#210 USER PREFERENCES Selective Range Printing
210=1

Basically I need to be able to take a specific line of text from a file, edit it, and put it back in place, with pure Lua. For example, I might want to change 208=No to 208=Yes without changing anything else in the file.

I've already searched this site, Google, and Corona's API page, but nothing seems to have what I'm looking for. Am I going to have to read all the file up to that line and after that line and concatenate it all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2025-01-14 11:31:43

我是否必须读取该行之前和该行之后的所有文件并将其连接起来?

您不必连接它。只需继续读取文件并存储行,直到到达要更改的行。进行更改,将文件的其余部分作为一个字符串读取,然后按顺序写入所有先前读取的行。

它看起来像这样:

local hFile = io.open(..., "r") --Reading.
local lines = {}
local restOfFile
local lineCt = 1
for line in hFile:lines() do
  if(lineCt == ...) then --Is this the line to modify?
    lines[#lines + 1] = ModifyLine(line) --Change old line into new line.
    restOfFile = hFile:read("*a")
    break
  else
    lineCt = lineCt + 1
    lines[#lines + 1] = line
  end
end
hFile:close()

hFile = io.open(..., "w") --write the file.
for i, line in ipairs(lines) do
  hFile:write(line, "\n")
end
hFile:write(restOfFile)
hFile:close()

Am I going to have to read all the file up to that line and after that line and concatenate it all?

You don't have to concatenate it. Just keep reading the file and storing lines until you reach the line you want to change. Make your change, read the entire rest of the file as one string, and then write all the previously read lines in order.

It would look something like this:

local hFile = io.open(..., "r") --Reading.
local lines = {}
local restOfFile
local lineCt = 1
for line in hFile:lines() do
  if(lineCt == ...) then --Is this the line to modify?
    lines[#lines + 1] = ModifyLine(line) --Change old line into new line.
    restOfFile = hFile:read("*a")
    break
  else
    lineCt = lineCt + 1
    lines[#lines + 1] = line
  end
end
hFile:close()

hFile = io.open(..., "w") --write the file.
for i, line in ipairs(lines) do
  hFile:write(line, "\n")
end
hFile:write(restOfFile)
hFile:close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文