Results 1 to 4 of 4

Thread: Append inserting random char into beginning of QTString

  1. #1
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Append inserting random char into beginning of QTString

    Hello everyone, I'm making a small program that will ease the process of opening files at the law firm I work for. Anyways, I'm having a difficult time with the append class of QTString.

    I am trying to write to a csv file through user input, but need to get past this step first. (everything but 1 will be through user input, and date will take the system date)

    The specific format it needs to look like is:
    ,"defendant",advs.,"plaintiff",Lawyer,1,date,,, ,,,

    The function:
    Qt Code:
    1. void csvWriting()
    2. {
    3. QFile file("register.csv");
    4. file.open(QIODevice::Append | QIODevice::Text);
    5. QDataStream out(&file);
    6. QString string = " ,";
    7. QString defendant = "Defendant";
    8. QString camma = ",";
    9. QString advs = "advs.";
    10. QString plaintiff = "Plaintiff";
    11. QString lawyer = "lawyer";
    12. QString date = "date";
    13. QString ending = ", , , , , \n";
    14. string.append(defendant + camma + advs + camma + plaintiff + camma + lawyer + camma + date + ending);
    15. out << string;
    16. file.close();
    17. }
    To copy to clipboard, switch view to plain text mode 

    However, when it outputs, a random character (seems to like b/d) is inserted into the string:

    d , D e f e n d a n t , a d v s . , P l a i n t i f f , l a w y e r , d a t e , , , , ,
    I'm assuming it has something to do with the appending process as when I was doing the test writing with a single string this did not happen. Does a "," have an escape character I could use instead? The subsequent "," that are appending don't produce this outcome though...

    Where is this d coming from, and how would I fix it? As a side note, I am not sure why all the letters have spaces between them, but, for my purposes, it doesn't seem to matter.

    (perhaps it would just be easier to do "out << string; out << camma;" etc.)
    Last edited by TomJoad; 22nd March 2011 at 16:43.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Append inserting random char into beginning of QTString

    The problem (probably) has to do with you using QDataStream.
    Use QTextStream.
    Also you can do all this much more elegant if you append strings to a QStringList, and then loop for adding the commas when you output to the file.
    Like so:
    Qt Code:
    1. QStringList strList;
    2. strList<<"some string";
    3. strList<<"some other string";
    4. ...
    5. QFile file;
    6. //open file etc
    7. QTextStream out(&file);
    8. for(int i=0; i<strList.size()-1; i++){
    9. out<<strList<<",";
    10. }
    11. out<<strList.at(strList.size()-1); //to avoid comma after the last string.
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. The following user says thank you to high_flyer for this useful post:

    TomJoad (22nd March 2011)

  4. #3
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Append inserting random char into beginning of QTString

    Using QTextStream actually fixed all of the problems. I will investigate QStringList later tonight as it looks very useful.

    It's taken me a while to wrap my head around C++ and QT and it finally feels like it is starting to come together.

    Thank you.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Append inserting random char into beginning of QTString

    Or just:
    Qt Code:
    1. parts << "1" << "2" << "3";
    2. ...
    3. out << parts.join(",");
    To copy to clipboard, switch view to plain text mode 
    Depending on what will read the file, you might also need to account for the possibility of commas or quotes in some of the parts.

Similar Threads

  1. read file from end to beginning..
    By soul_rebel in forum Qt Programming
    Replies: 11
    Last Post: 4th June 2012, 01:20
  2. qt from beginning to advanced
    By sachinmcajnu in forum Newbie
    Replies: 4
    Last Post: 8th March 2011, 07:58
  3. QDockWidget floating at the beginning
    By Caius Aérobus in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 13:15
  4. Replies: 1
    Last Post: 7th April 2010, 16:26
  5. Cannot append to QFile using QIODevice::Append
    By philwinder in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2008, 09:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.