Unsolved Edit text in File
-
Hi everyone, I have one more question, how can I edit a text in a file?
In the program records several information and would like to edit two that will be what the user type in a line edit.
My code:
QString valor = ui->edtRValor->text(); QDate dateIn = ui->dateRInicial->date(); QString dataInicio = dateIn.toString(); QDate dateFi = ui->dateRFim->date(); QString FimDate = dateFi.toString(); QFile arch("C:\\Users\\Caio\\Documents\\cadastrarQuarto.txt"); if(!sr.open(QIODevice::ReadOnly | QIODevice::Text)) return; QTextStream out(&arch);
How do I get the value typed and swap with a value that is written in the file?
My file:231-1º-40-sáb jan 1 2000-qua jan 5 2000-160-2 441-4º-40-sáb jan 1 2020-qua jan 5 2200-190-8
-
@Caio.Sousa said in Edit text in File:
if(!sr.open(QIODevice::ReadOnly | QIODevice::Text))
What is sr and why do you open it in read-only mode?
"How do I get the value typed and swap with a value that is written in the file?" - you already get the typed values in the code above, right? I don't really understand your description. What do you really want to do? Do you want to append the text typed by the user to the end of the file or do you want to replace existing text with new (which text in the file exactly)?
-
Sorry, I'm Brazilian and I do not speak much English.
I want to attach the text typed by the user to one that already exists.
For example:
Caio-19 years-Player
I will receive a text in an edit line and the file will be:
Caio-40years-Student
-
@Caio.Sousa You still did not explain what "sr" actually is. I assume you mean arch:
QFile arch("C:\\Users\\Caio\\Documents\\cadastrarQuarto.txt"); if(!arch.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append)) return; QTextStream out(&arch); out << (valor + dateIn.toString() + dataInicio + dateFi.toString() + FimDate + '\n');
-
I want to search for certain text and edit in a file I will receive the text entered by the user and must be replaced by a certain text already existing in the file.
-
Example:
Cat - Dog - 19 years.
User typed fox
I want to edit the dog for the foxNew File:
Cat - Fox - 19 years. -
Hi,
Wouldn't it be simpler to do that using sed ?
Otherwise, open the file, load it completly, use a regex for the search and replace part and then dump the content back to the file.
-
I am new in QT Creator,
And I do not quite understand how you use regex and sed -
sed is *nix command that allows you to parse and modify file content using regular expressions.
As for regular expression in Qt see QRegularExpression.