Results 1 to 6 of 6

Thread: read ascii files with different column counts

  1. #1
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default read ascii files with different column counts

    I have the following code for reading an ascii file with 7 columns:
    Qt Code:
    1. while (!in.atEnd())
    2. {
    3.  
    4. in >> GratingType
    5. >> newEntry.delay
    6. >> newEntry.period
    7. >> newEntry.phase
    8. >> newEntry.chirp
    9. >> newEntry.powerIndex
    10. >> newEntry.powerScale;
    11.  
    12. VariationList.push_back(newEntry);
    13. }
    To copy to clipboard, switch view to plain text mode 
    However I also have files with a similar structure, but only 4 columns
    Qt Code:
    1. while (!in.atEnd())
    2. {
    3.  
    4. in >> GratingType
    5. >> newEntry.delay
    6. >> newEntry.period
    7. >> newEntry.phase;
    8.  
    9. VariationList.push_back(newEntry);
    10. }
    To copy to clipboard, switch view to plain text mode 

    How can I find out how many columns my file has?

    If I read a 4 column file with the 7 column version the data structure is read wrong.

    This is a very basic question, and maybe not really Qt and more C++ related, but I do not really know how to start looking for a clever solution.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read ascii files with different column counts

    Read each line into a string and then check the length of the string? Or perhaps check for column delimiters?

  3. #3
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: read ascii files with different column counts

    Ok, that should work with this code:
    Qt Code:
    1. int numberOfValues = 0;
    2. if (!in.atEnd()) {
    3. QString line = in.readLine();
    4. QStringList list = line.split("\t");
    5. numberOfValues = list.size();
    6. }
    7. while (!in.atEnd())
    8. {
    9. in >> GratingType
    10. >> newEntry.delay
    11. ...
    To copy to clipboard, switch view to plain text mode 
    However, how do I reset the 'in' variable to beginning of file?

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read ascii files with different column counts

    What class are you using? QTextStream? QFile? Something else?

  5. #5
    Join Date
    Feb 2008
    Posts
    157
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: read ascii files with different column counts

    Quote Originally Posted by fatjuicymole View Post
    What class are you using? QTextStream? QFile? Something else?
    I solved it now using the following code:
    Qt Code:
    1. QFile file(filename);
    2. if (!file.open(QIODevice::ReadOnly))
    3. {
    4. return;
    5. }
    6. QTextStream in(&file);
    7.  
    8. GratingVariationListEntryDef newEntry;
    9.  
    10. int numberOfValues = 0;
    11. if (!in.atEnd()) {
    12. QString line = in.readLine();
    13. QStringList list = line.split("\t");
    14. numberOfValues = list.size();
    15. in.seek(0);
    16. }
    17. while (!in.atEnd())
    18. {
    19. switch (numberOfValues)
    20. {
    21. case 4:
    22. in >> GratingType
    23. >> newEntry.delay
    24. >> newEntry.period
    25. >> newEntry.phase;
    26. break;
    27. case 5:
    28. ...
    To copy to clipboard, switch view to plain text mode 

    It works, however from the docs it is not obvious that 'seek(0);' is what I was looking for. I had tried 'reset()', but that changed nothing so that I do not even know what it does.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read ascii files with different column counts

    reset() does what you want, but since you are using QTextStream also, that buffers the file and so reset() doesn't give the expected result.

    This is actually fully described in the documentation.

Similar Threads

  1. Replies: 6
    Last Post: 6th August 2009, 17:18
  2. QT read files
    By Majdi in forum Newbie
    Replies: 3
    Last Post: 21st January 2009, 09:17
  3. How can I read Binary files with Qt
    By geo_saleh in forum Qt Programming
    Replies: 2
    Last Post: 16th August 2007, 10:37
  4. read files and save the,
    By kernel_panic in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 06:31
  5. Read files PDF
    By henriquez0 in forum Qt Programming
    Replies: 3
    Last Post: 31st December 2006, 00:20

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.