Results 1 to 11 of 11

Thread: Reading a file with differences type of data

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

    Default Reading a file with differences type of data

    Hi,
    There is a file including varies type of data. data is written in this file as below:
    1- 500 doubles
    2- 500 doubles
    3- 500 doubles
    4- 500 ints
    5- 500 QString inside a QStringList

    and the code for writing this file is:
    Qt Code:
    1. void write()
    2. {
    3. QFile g1f, g2f, of, chf, namef;
    4. double g1[500];
    5. double g2[500];
    6. double ofse[500];
    7. int ch[500];
    8. QStringList names;
    9. for(int i=0; i<500; ++i)
    10. {
    11. g1[i] = i/2.0;
    12. g2[i] = 2.0*i;
    13. ofse[i] = 1;
    14. ch[i]=i;
    15. names.append(QString("name%1").arg(i));
    16. }
    17.  
    18. g1f.setFileName("oneFile.th");
    19. if(!g1f.open(QFile::WriteOnly))
    20. {
    21. qDebug()<<"can not open to write";
    22. }
    23. g1f.write((char*)g1, (500)*sizeof(double));
    24. g1f.flush();
    25. g1f.close();
    26.  
    27. g2f.setFileName("oneFile.th");
    28. if(!g2f.open(QFile::Append))
    29. {
    30. qDebug()<<"can not open to write";
    31. }
    32. g2f.write((char*)g2, (500)*sizeof(double));
    33. g2f.flush();
    34. g2f.close();
    35.  
    36.  
    37. of.setFileName("oneFile.th");
    38. if(!of.open(QFile::Append))
    39. {
    40. qDebug()<<"can not open to write";
    41. }
    42. of.write((char*)ofse, (500)*sizeof(double));
    43. of.flush();
    44. of.close();
    45.  
    46. chf.setFileName("oneFile.th");
    47. if(!chf.open(QFile::Append))
    48. {
    49. qDebug()<<"can not open to write";
    50. }
    51. chf.write((char*)ch, (500)*sizeof(int));
    52. chf.flush();
    53. chf.close();
    54.  
    55. namef.setFileName("oneFile.th");
    56. if(!namef.open(QFile::Append))
    57. {
    58. qDebug()<<"can not open to write";
    59. }
    60. QDataStream df(&namef);
    61. df << names;
    62. namef.flush();
    63. namef.close();
    64. }
    To copy to clipboard, switch view to plain text mode 

    now i want to read this file and separate to:
    1- 500 doubles >>>>> double ge1[500]
    2- 500 doubles >>>>> double ge2[500]
    3- 500 doubles >>>>> double of[500]
    4- 500 ints >>>>> int ch[500]
    5- 500 QString inside a QStringList >>>>> QStringList nam

    please help me to read and separate this file.

    thanks

  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: Reading a file with differences type of data

    Exactly the same as you save only use read instead of write.

  3. #3
    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: Reading a file with differences type of data

    And you had read working before, hadn't you?

    Cheers,
    _

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

    Default Re: Reading a file with differences type of data

    Quote Originally Posted by Lesiok View Post
    Exactly the same as you save only use read instead of write.
    But first it must be separated. how can i separate?

    Quote Originally Posted by anda_skoa View Post
    And you had read working before, hadn't you?

    Cheers,
    _
    what do you mean anda_skoa?

  5. #5
    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: Reading a file with differences type of data

    In the first step You saved to a file memory area with size 500 * sizeof (double) from the array g1. Exactly the same load the file into memory and have data array double [500]. Do not close the file between subsequent readings.

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

    Alex22 (14th January 2016)

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

    Default Re: Reading a file with differences type of data

    Quote Originally Posted by Lesiok View Post
    In the first step You saved to a file memory area with size 500 * sizeof (double) from the array g1. Exactly the same load the file into memory and have data array double [500]. Do not close the file between subsequent readings.
    Thanks. I used this:
    Qt Code:
    1. void read()
    2. {
    3. QFile g1f, g2f, of, chf, namef;
    4. double g1[500];
    5. double g2[500];
    6. double ofse[500];
    7. int ch[500];
    8. QStringList names;
    9.  
    10.  
    11. g1f.setFileName("oneFile.th");
    12. if(!g1f.open(QFile::ReadOnly))
    13. {
    14. qDebug()<<"can not open to read";
    15. }
    16. g1f.read((char*)g1, (500)*sizeof(double));
    17.  
    18.  
    19. g2f.setFileName("oneFile.th");
    20. if(!g2f.open(QFile::Append))
    21. {
    22. qDebug()<<"can not open to read";
    23. }
    24. g2f.read((char*)g2, (500)*sizeof(double));
    25.  
    26.  
    27.  
    28. of.setFileName("oneFile.th");
    29. if(!of.open(QFile::Append))
    30. {
    31. qDebug()<<"can not open to read";
    32. }
    33. of.read((char*)ofse, (500)*sizeof(double));
    34.  
    35.  
    36. chf.setFileName("oneFile.th");
    37. if(!chf.open(QFile::Append))
    38. {
    39. qDebug()<<"can not open to read";
    40. }
    41. chf.read((char*)ch, (500)*sizeof(int));
    42.  
    43.  
    44. namef.setFileName("oneFile.th");
    45. if(!namef.open(QFile::Append))
    46. {
    47. qDebug()<<"can not open to write";
    48. }
    49. QDataStream df(&namef);
    50. df >> names;
    51. namef.close();
    52.  
    53. for(int y=0; y<500; y++)
    54. qDebug()<<ch[y];
    55. }
    To copy to clipboard, switch view to plain text mode 

    now i have 2 issues. it gives me this when i want to read:
    QIODevice::read (QFile, "oneFile.th"): WriteOnly device
    QIODevice::read (QFile, "oneFile.th"): WriteOnly device
    QIODevice::read (QFile, "oneFile.th"): WriteOnly device
    QIODevice::read (QFile, "oneFile.th"): WriteOnly device

    and for example in "int ch[500]", after reading, ch[499] must equal to 499 but it equals to 497! what is wrong?

  8. #7
    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: Reading a file with differences type of data

    When you write the one file use only a single QFile, open file to write, write all the data, close the file.
    When you read the one file use only a single QFile, open file to read, read all the data in the same order it was written, close the file.

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

    Alex22 (14th January 2016)

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

    Default Re: Reading a file with differences type of data

    Quote Originally Posted by ChrisW67 View Post
    When you write the one file use only a single QFile, open file to write, write all the data, close the file.
    When you read the one file use only a single QFile, open file to read, read all the data in the same order it was written, close the file.
    Thanks ChrisW67. I used this code:
    Qt Code:
    1. void write()
    2. {
    3. QFile g1f;
    4. double g1[500];
    5. double g2[500];
    6. double ofse[500];
    7. int ch[500];
    8. QStringList names;
    9. for(int i=0; i<500; ++i)
    10. {
    11. g1[i] = i/2.0;
    12. g2[i] = 2.0*i;
    13. ofse[i] = 1;
    14. ch[i]=i;
    15. names.append(QString("name%1").arg(i));
    16. }
    17.  
    18. g1f.setFileName("oneFile.th");
    19. if(!g1f.open(QFile::WriteOnly))
    20. {
    21. qDebug()<<"can not open to write";
    22. }
    23. g1f.write((char*)g1, (500)*sizeof(double));
    24. g1f.write((char*)g2, (500)*sizeof(double));
    25. g1f.write((char*)ofse, (500)*sizeof(double));
    26. g1f.write((char*)ch, (500)*sizeof(int));
    27. QDataStream df(&g1f);
    28. df << names;
    29. g1f.flush();
    30. g1f.close();
    31. }
    32.  
    33. void read()
    34. {
    35.  
    36. QFile g1f;
    37. double g1[500];
    38. double g2[500];
    39. double ofse[500];
    40. int ch[500];
    41. QStringList names;
    42. names.clear();
    43.  
    44.  
    45. g1f.setFileName("oneFile.th");
    46. if(!g1f.open(QFile::ReadOnly))
    47. {
    48. qDebug()<<"can not open to read";
    49. }
    50. g1f.read((char*)g1, (500)*sizeof(double));
    51. g1f.read((char*)g2, (500)*sizeof(double));
    52. g1f.read((char*)ofse, (500)*sizeof(double));
    53. g1f.read((char*)ch, (500)*sizeof(int));
    54. QDataStream df(&g1f);
    55. df >> names;
    56. g1f.close();
    57.  
    58. for(int y=0; y<500; y++)
    59. qDebug()<<y<<names;
    60. }
    To copy to clipboard, switch view to plain text mode 

    it is well now

  11. #9
    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: Reading a file with differences type of data

    Don't open the file for appending when you write. No need to call flush().
    Do open the file for reading before you try to read from it.

    You may be better off using QDataStream for all the data and QVector<double>/QVector<int> instead of bare arrays

  12. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (14th January 2016)

  13. #10
    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: Reading a file with differences type of data

    Quote Originally Posted by Alex22 View Post
    what do you mean anda_skoa?
    You already had code to read the data.

    http://www.qtcentre.org/threads/6476...167#post286167

    Did you somehow lose it and forget that you also posted it?

    Cheers,
    _

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

    Alex22 (15th January 2016)

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

    Default Re: Reading a file with differences type of data

    @anda_skoa, you are right. sorry about that. I could write in one file but i could not read from that file. thanks for mention and sorry again.

Similar Threads

  1. Qwtplot: reading data from text file
    By GG2013 in forum Newbie
    Replies: 0
    Last Post: 31st May 2013, 08:03
  2. Replies: 3
    Last Post: 8th June 2011, 07:36
  3. Reading XML file into a data file correctly?
    By falconium in forum Qt Programming
    Replies: 3
    Last Post: 9th May 2011, 19:55
  4. Reading data from xls file
    By addu in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2010, 10:33
  5. reading data from file
    By offline in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2010, 11:31

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.