I have a txt
file to configure settings of a serial device, which looks like this:
`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
in my program , I need to read this file and change some values and save.
for example, in the last lines of the text:
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
I need to change those PW
values (180us
) into a value adjusted through a QSlider
ui->verticalSlider_ch1->value()
ui->verticalSlider_ch2->value()
Can you show me how to access those values from the txt file and change it?
p.s.
In the above-mentioned config file, comments are enclosed in backticks `` and replaced with space characters. A single backtick ` starts a comment that continues to the end of the line.
EDIT
From the comments, I tried to breakdown the problem into three parts:
1) Reading the file and extracting the contents of the O lines, 2) using that to present a screen with sliders
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();
}
here I did the change in the memory.
- Writing back the new values to the file (with contents intact).
This is something I've difficulty implement with. How do I write those modified lines back to the original file?