In the config file, I have the data:

6.30 0.92 1111 1 11.80 2222 2 11.80 58.00 50.8 

but I have a problem with the fact that when I press the button, I fill in the data, but not exactly .

 void MainWindow::on_pushButton_4_clicked() { FILE *fid=fopen("config.ini","rt"); if(fid) { double val=0; char str[255]; fscanf(fid,"%lf",&val); qDebug() << val; ui->sb_dal_izl_gidrof->setValue(val); fscanf(fid,"%lf",&val); ui->sb_dal_priem_gidrof->setValue(val); fscanf(fid,"%s", &str); ui->le_tipIzmGidr2->setText(QString("%1").arg(str)); fscanf(fid,"%s", &str); ui->le_namIzmGidr2->setText(QString("%1").arg(str)); //Если это сторка // char str[0]=0; fscanf(fid,"%lf", &val); ui->sb_chywstIzmGidrofon2->setValue(val); fscanf(fid,"s", &str); ui->le_tipPriemGidr2->setText(QString("%1").arg(str)); fscanf(fid,"s", &str); ui->le_namPriemGidr2->setText(QString("%1").arg(str)); fscanf(fid,"lf", &val); ui->sb_chywstPriemGidrofon2->setValue(val); fclose(fid); } } 

At the exit:

 fscanf(fid,"s", &str); ui->le_tipPriemGidr2->setText(QString("%1").arg(str)); fscanf(fid,"s", &str); ui->le_namPriemGidr2->setText(QString("%1").arg(str)); 

Meaning 1 and 1 . And it is necessary to 2222 and 2 .

At first I thought that the str buffer is full, and therefore I decided to clear it srt[0]=0; and srt[255]=0; but failed. The result was the same.

Then I decided to just keep the new buffer for these two variables.

 double val=0; char str[255]; char str1[255]; // вот новый буфер // а тут его новое использование fscanf(fid,"s", &str1); ui->le_tipPriemGidr2->setText(QString("%1").arg(str1)); fscanf(fid,"s", &str1); ui->le_namPriemGidr2->setText(QString("%1").arg(str1)); 

The result is very sad ^Cj and ^Cj - that's what he gave me at the exit.
Tell me, please, how to make it output what is written in the file, but not cracks?

  • That's what you write - fscanf(fid,"s", &str1); ? Those. "s" , not "%s" ? And, by the way, you can not write & with lines - the address of the line will be transmitted and so ... - Harry
  • one
    And generally use QString and discard fscanf and similar low-level string functions when working with C ++ and Qt. - gil9red
  • Attaching an example of working with files and strings in Qt: pastebin.com/RiBdrW6S - gil9red
  • @ gil9red thanks I will know)) - timob256

1 answer 1

Harry, thank you for answering my question with a suggestive question. "s", not "% s"?

I put% s and returned str to str1

  fscanf(fid,"%s", &str); ui->le_tipPriemGidr2->setText(QString("%1").arg(str)); fscanf(fid,"%s", &str); ui->le_namPriemGidr2->setText(QString("%1").arg(str)); 

and it all worked: 3 Thank you, before the call