QFile write/replace



  • Hello world,

    I'm developing an app and its design requires me to edit particular line in file.

    creating new file is not an option.

    My file is formatted like this

    First line - 4characters\n
    Every other line- 64characters\n

    Therefore I always know the length of line, and I need to edit the first 4 characters, and I have no idea how to do that. Could anyone help me?

    EDIT: i have 1.5 hours to implement that, if not I have to wait another 24hrs (i can test that only at 00:00 local time) so forgive me if I failed to search forum thoroughly.


  • Moderators

    You need to open file in write and append(this might be misleading a little) mode and then seek to the beginning:
    @
    QFile f("c:/some.file");
    if(f.open(QFile::WriteOnly | QFile::Append))
    {
    f.seek(0);
    f.write("test");
    f.close();
    }
    @



  • o.O this is the same as C... I feel stupid....



  • Or, you memmap the file and just manipulate the data as if you were doing it in-memory.



  • Me again... This is not quite working for me...

    so... my file is basically a 'database' and first line is number of lines after that...
    when inputing new line to file, i do following:
    -append line with QTextStream
    -seeking to begining of file and changing number to +1

    So my problem is, seek(0) does nothing. new number is apended nevertheless... should i open file only in write mode?


  • Lifetime Qt Champion

    What do you use for seeking, QFile or QTextStream ? They might be interfering one with the other



  • @ if(h_fajl.open(QFile::WriteOnly | QFile::Append))
    {
    h_fajl.seek(0);
    h_fajl.write(p.toAscii()+"\n");
    h_fajl.close();
    }@

    p is a Qstring....
    Oh and I thought that some interference could happen so i closed file, and reopened here.... still same thing...


  • Lifetime Qt Champion

    Did you also do a textStream.setDevice(0) ?



  • [quote author="SGaist" date="1361198821"]Did you also do a textStream.setDevice(0) ?[/quote]

    no. what should that be? Im not aware what that method even does... :/

    My issues are with seek...

    when i want to append to file i do
    @ h_out << hString+"\n";@

    and it works flawlesly. But when i do the
    @ if(h_fajl.open(QFile::WriteOnly | QFile::Append))
    {
    h_fajl.seek(0);
    h_fajl.write(p.toAscii()+"\n");
    h_fajl.close();
    }
    @
    this ALSO doing appending and not replacing first chars of file


  • Lifetime Qt Champion

    That will remove your file from the text stream.

    You open it with the Append flag, so it will append.



  • well Krzysztof said that should be done like that (with both append and write flags).
    ok, ill try to remove file from stream... holding my fingers crossed...


  • Lifetime Qt Champion

    He's right: you need Append when you want to append (your first step) but then you want to overwrite the content of your data (second step)



  • writting without append overwrites everything in my file. not only first chars. I want other thing in file to be intact...


  • Lifetime Qt Champion

    Try the equivalent of this:

    @QFile testFile("test.txt");
    testFile.open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text);
    testFile.write("test\n");
    testFile.close();
    testFile.open(QIODevice::ReadWrite|QIODevice::Text);
    testFile.seek(0);
    testFile.write("1\n"); @



  • i think i tried something that should be the exact equivalent of that. And it apended text.

    Right now, i am quite confused. As far as I know computer does not have 'free will'. and one piece of code (i had it saved) erased whole file and started writing, and now it appends (and append flag is not turned on).

    So now i need to find diferences between my codes, and see where did i go wrong. After that I will try to write your code, and see what's the output. Right now im confused as hell and not even sure what the hell is going on with my codes :) oh, and i even managed to get segfault with wrong qdebug call. maybe it's time to go to sleep :/

    EDIT 1.
    I made it. I held my first row in separated file, as far as memory weight of my program, it will be the same if not smaller. It will be little harder to implement reading but nothing will be to hard.

    So now i have to do some backups, and to deploy changes, and ofc to sleep. Tomorrow I'll try your code, so I'll post results for someone to see in future :)

    Still I would like an explanation about one thing. I feel like I'm missing something obvious.
    If I used qTextStream to output somethings to my QFile that is opened with ReadWrite flag, strange thing happened. It didn't overwrite, it didn't append. It did something that looked like those two combined. I don't have time to even try to understand what was going on at that moment.

    So i used write() method and every time i seek(0) before it, so it is a success...
    When i find out how to edit only one line in file, il post it here :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.