【发布时间】:2020-05-08 17:42:26
【问题描述】:
我有一个txt 文件来配置串行设备的设置,如下所示:
`R ref, version, "config_ID", menu language, Power timeout (hours), Number of users
R R1 1 "Template for setting parameters" ENGLISH 1 1
`U ref, "user name", language, volume, number of activities
U U1 "Any user" ENGLISH 100% 1
`A ref, "activity name", max duration, max cycles, startingPW%, available/hidden
A A1 "Setup stim levels" 0min 0 0% AVAILABLE FALSE FALSE TRUE TRUE
B SA1 1 "Engine tests"
` These limits apply to all phases
` M ref stim, channel, max current, minPW, maxPW, freq, waveform, output name
M CH1 1 1 120mA 10us 450us 40Hz ASYM "Channel 1"
M CH2 1 2 120mA 10us 450us 40Hz ASYM "Channel 2"
P P0 "Test phase" 0ms NONE 2000ms STOP STOP STOP
` Delay RR rate PW
O CH1 0mA 0ms 0ms 600000ns 180us RATE
O CH2 0mA 0ms 0ms 600000ns 180us RATE
在我的程序中,我需要读取这个文件并更改一些值并保存。
例如,在文本的最后几行:
P P0 "Test phase" 0ms NONE 2000ms STOP STOP STOP
` Delay RR rate PW
O CH1 0mA 0ms 0ms 600000ns 180us RATE
O CH2 0mA 0ms 0ms 600000ns 180us RATE
我需要将那些 PW 值 (180us) 更改为通过 QSlider 调整的值
ui->verticalSlider_ch1->value()
ui->verticalSlider_ch2->value()
您能告诉我如何从 txt 文件中访问这些值并进行更改吗?
附言
在上述配置文件中,cmets 用反引号 `` 括起来并替换为空格字符。单个反引号 ` 开始一个持续到行尾的注释。
编辑
从cmets,我尝试将问题分解为三个部分:
1) 读取文件并提取 O 行的内容,2) 使用它来呈现带有滑块的屏幕
QString filename = "config_keygrip";
QString path = QCoreApplication::applicationDirPath()+"/"+filename+".txt";
QFile file(path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QMessageBox::information(this, "Unable to open file for read", file.errorString());
return;
}
else
{
QTextStream in(&file);
while(!in.atEnd())
{
QString line = in.readLine();
QString trackName("O CH1");
int pos = line.indexOf(trackName);
if (pos >= 0)
{
QStringList list = line.split(' ', QString::SkipEmptyParts); // split line and get value
QString currVal = QString::number(ui->verticalSlider->value());
list[3] = currVal; // replace value at position 3 with slider value
}
}
file.close();
}
在这里我做了记忆的改变。
- 将新值写回文件(内容不变)。
这是我难以实现的。如何将这些修改后的行写回原始文件?
【问题讨论】:
-
显示你尝试过的东西,即使它不起作用,清楚地指出你卡在哪里,因为 SO 不是 SW 写作服务
-
您的文本文件中有一些可能不属于那里的单引号。如果您选择代码然后按编辑窗口顶部栏上的“代码示例”按钮,代码格式化会更容易
-
@foreknownas_463035818 您好,在我的配置文件中,cmets 用反引号 ` ` 括起来并替换为空格字符。单个反引号 ` 开始一个持续到行尾的注释。
-
好的,那么请将此信息添加到问题中,因为它并不明显。糟糕的格式看起来很相似;)
-
这个问题由三个部分组成:1)读取文件并提取O行的内容; 2)用它来呈现一个带有滑块的屏幕; 3) 将新值写回文件(可能内容完整)。你分别尝试了什么?