Results 1 to 9 of 9

Thread: problem with binary writting a QString array

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default problem with binary writting a QString array

    hi
    i write a QString array in a file in binary mode and then read it from the file. the code is below. for what is it read wrong? and is QString suitable for saving these names?
    Qt Code:
    1. QString names[125];
    2. for(int i=0; i<125; i++)
    3. {
    4. names[i] = QString("Name%1").arg(i+1);//is QString suitable for saving these names?
    5. }
    6. QFile namef1;
    7. namef1.setFileName("names.bin");
    8. if(!namef1.open(QFile::ReadOnly))
    9. {
    10. qDebug()<<"can not open to read";
    11. }
    12. namef1.read((char*)names, 125*sizeof(QString));
    13. namef1.close();
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. QFile namef;
    2. namef.setFileName("names.bin");
    3. if(!namef.open(QFile::WriteOnly))
    4. {
    5. qDebug()<<"can not open to write";
    6. }
    7. namef.write((char*)names, 125*sizeof(QString));
    8. namef.flush();
    9. namef.close();
    To copy to clipboard, switch view to plain text mode 
    thanks for any help

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with binary writting a QString array

    First : sizeof(QString) is NOT string length, this is a size of object. Code for writing should be :
    Qt Code:
    1. for( int i = 0; i <125; i++ )
    2. namef.write(names.toAscii());
    To copy to clipboard, switch view to plain text mode 
    Second : strings have different lengths. If you save them without any separator how you split them later?

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

    Alex22 (19th December 2015)

  4. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: problem with binary writting a QString array

    thanks for your answering. you are right. then i must write in text form
    is there any example for writing a QString array (that names change in a loop) in text form? and how can i read so? is QStringList better for this?
    thanks for more helps

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem with binary writting a QString array

    If you only need this between instances of your program or between Qt programs, then an easy solution is to use QDataStream to serialize QStrings individually or as a QStringList.

    If this needs to be read by a non-Qt program and you are in charge of the format, then I would suggest something like a length field followed by UTF-8 data.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    Alex22 (20th December 2015)

  7. #5
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: problem with binary writting a QString array

    @anda thanks. please help me for writting under code:
    Qt Code:
    1. QString names[125];
    2. for(int i=0; i<125; i++)
    3. {
    4. names[i] = QString("Name%1").arg(i+1);
    5. }
    6. QFile namef1("c:/text.txt");
    7. if(!namef1.open(QFile::WriteOnly))
    8. {
    9. qDebug()<<"can not open to write";
    10. }
    11. QTextStream out(&namef1);
    12. out << names[i]//how can i first put all names[125] into the "list" and then write it?
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QFile namef("c:/text.txt");
    2. if(!namef.open(QFile::ReadOnly))
    3. {
    4. qDebug()<<"can not open to read";
    5. }
    6. QTextStream in(&namef);
    7. in>>list;// please help me also to read from file and put it into "list" and after that
    8. //split list to 125 QStrings in names[125]
    To copy to clipboard, switch view to plain text mode 

  8. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with binary writting a QString array

    Simply : forget about the table names.
    Qt Code:
    1. for(int i=0; i<125; i++)
    2. list.append(QString("Name%1").arg(i+1));
    3.  
    4. QFile namef1("c:/text.txt");
    5. if(!namef1.open(QFile::WriteOnly))
    6. {
    7. qDebug()<<"can not open to write";
    8. }
    9. QTextStream out(&namef1);
    10. out << list;
    To copy to clipboard, switch view to plain text mode 
    Work with QStringList not with table.

  9. The following user says thank you to Lesiok for this useful post:

    Alex22 (20th December 2015)

  10. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem with binary writting a QString array

    Qt Code:
    1. ....
    2. QDataStream stream(&file);
    3. stream << list;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. ....
    2. QDataStream stream(&file);
    3. stream >> list;
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    Alex22 (20th December 2015)

  12. #8
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: problem with binary writting a QString array

    @anda thanks but how i can put all names[125] (in a for loop) into the list while writting in a file. and how i can get these names from list while reading?

  13. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: problem with binary writting a QString array

    There is no point in creating the array in the first place.
    Just put the strings into the list.

    After reading, just work with the list.

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 16th November 2015, 21:18
  2. Replies: 6
    Last Post: 30th September 2015, 21:20
  3. QString to Binary Problem
    By onder_11 in forum Qt Programming
    Replies: 5
    Last Post: 22nd October 2012, 03:43
  4. Cast QString array to std::string array
    By Ishtar in forum Newbie
    Replies: 4
    Last Post: 15th July 2011, 09:28
  5. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 01:38

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.