Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Rewrite specific string in file

Rewrite specific string in file

Scheduled Pinned Locked Moved General and Desktop
qfileqtextstream
2 Posts 2 Posters 977 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jjan
    wrote on 15 Oct 2015, 11:01 last edited by jjan
    #1

    Hey,
    I currently try to write a little configuration "parser" for myself. I want to overwrite an existing value. The INI file looks like this:

    test = abc123
    

    I have tried it with this:

    void INIHelper::writeValue(QString& file_path, QString& property, QString value)
    {
    	QFileInfo check(file_path);
    	if (!check.exists())
    	{
    		// We have no configuration file. Lets contact ConfigurationWriter to initialize one.
    		// TODO(15.10.2015, jan): Write configuration file.
    	}
    	else
    	{
    		QFile f(file_path);
    		if (f.open( QIODevice::Append ))
    		{
    			QTextStream inw(&f);
    			while (!inw.atEnd())
    			{
    				QString l = inw.readLine();
    				QString _property(property + " = ");
    				int propertyPos = l.indexOf(_property);
    				if (propertyPos >= 0)
    				{ // we are now behind 'test = '
    					QString old_value = l.mid(propertyPos + _property.length());
    					// old_value is the value of test!
    					old_value.clear();
    					old_value = value;
    					inw << old_value << endl;
    					f.close();
    				}
    			}
    		}
    	}
    }
    

    And I test it with:

      QString path("c://users//jan//desktop//test.ini");
      QString prop2("test");
      QString ret_test = ini_h->getValue(path, prop2);
      ini_h->writeValue(path, prop2, "testststst");
    

    But this doesn't works, nothing changes there. If I set a breakpoint on ret_test I get test (or better test = ) back, so its a problem at rewriting the value to the file. How can I make this work?

    ~jan

    1 Reply Last reply
    0
    • J Offline
      J Offline
      JKSH
      Moderators
      wrote on 15 Oct 2015, 13:10 last edited by
      #2

      Hi,

      Why not use QSettings?

      Anyway, if you want to implement this yourself, you first need to learn about file pointers and how to read and write to the same file at the same time:

      • http://www.cplusplus.com/forum/beginner/66306/
      • http://stackoverflow.com/questions/17536570/reading-and-writing-to-the-same-file-using-the-same-fstream

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      1

      2/2

      15 Oct 2015, 13:10

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved