Results 1 to 9 of 9

Thread: Fgets after a seek .. catch text after seek

  1. #1
    Join Date
    Apr 2012
    Posts
    7
    Qt products
    Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Fgets after a seek .. catch text after seek

    i've been some post around trying to know how fgets and the most of the people use readLine ..

    my prob comes because i dont want to get the full line ..

    i've made a seek inside a text file and now i need to grab ONLY the last 20 chars inside the file

    in C i make it with fgets

    char ruta[20];

    kidboot=fopen("/dos/config.txt","r"); --> cambio.open(QIODevice::ReadOnly | QIODevice::Text);

    fseek(kidboot,66,SEEK_SET); --> cambio.seek(66);

    fgets(ruta,20,kidboot); --> ?????????????? HOW DO I READ THIS IN QT) so i get only the LAST 20bytes ????????????

    greets

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: Fgets after a seek .. catch text after seek

    In fact, people don't use "deadLine", but QFile::readLine()...

    Now, when you looked into what functions QFile offers for reading, in QFile class reference, you perhaps did not notice readLineData().

  3. #3
    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: Fgets after a seek .. catch text after seek

    QFile is a QIODevice so QIODevice::read() is a good place to look.
    Of course, your C code will work just fine built into a C++ program also.

    Neither set of code will get the last 20 bytes in the file unless the file is exactly 86 bytes long. For that you need to query the length of the file and seek to a point 20 bytes from the end (and handle a file shorter than 20 bytes), or use SEEK_END in your fseek call. You should also check that fseek() or seek() succeeded.

    QFile::readLineData() is protected and not useful in this circumstance.
    Last edited by ChrisW67; 16th May 2012 at 00:39.

  4. #4
    Join Date
    Apr 2012
    Posts
    7
    Qt products
    Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Fgets after a seek .. catch text after seek

    what i want is grab the string until EOF,it's not just a task of read those 20bytes because i'm trying tocget a specific root chat inside the text file, in c , i just make a fgets and then a strlen(ruta)-1 ,, but i need to port this to qt

  5. #5
    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: Fgets after a seek .. catch text after seek

    what i want is grab the string until EOF,
    Your original code doesn't do that. Your fgets() call reads up to 20 bytes (including a terminating NUL) from the current location in the file until terminated by the EOL or EOF. So, you might get 20 bytes that don't reach the end of anything, you might get less than 20 bytes reaching the end-of-line or end-of-file.

    What you asked for:
    i've made a seek inside a text file and now i need to grab ONLY the last 20 chars inside the file
    Try this program:
    Qt Code:
    1. #include <QtCore>
    2. #include <QDebug>
    3. #include <cstdio>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication app(argc, argv);
    8.  
    9. // Your code
    10. FILE *kidboot;
    11. char ruta[20];
    12. kidboot=fopen("test.txt","r");
    13. fseek(kidboot,66,SEEK_SET);
    14. fgets(ruta,20,kidboot);
    15. fclose(kidboot);
    16. qDebug("Your result: \"%s\"", ruta);
    17.  
    18. // what your code does
    19. QFile file("test.txt");
    20. QFileInfo info(file);
    21. qint64 fileSize = info.size();
    22. if (fileSize >= 66 && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    23. if (file.seek(66)) {
    24. QByteArray ba = file.readLine();
    25. ba = ba.left(19);
    26. qDebug("This result: \"%s\"", ba.data());
    27. }
    28. else
    29. qWarning() << "File seek failed";
    30. file.close();
    31. }
    32. else
    33. qWarning() << "File too short";
    34.  
    35. // what you asked for
    36. if (fileSize >= 20 && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    37. if (file.seek(fileSize - 20))
    38. qDebug() << "Last 20:" << file.readAll();
    39. else
    40. qWarning() << "File seek failed";
    41. file.close();
    42. }
    43. else
    44. qWarning() << "File too short";
    45.  
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 
    with this test.txt file to see the difference:
    Qt Code:
    1. The quick brown fox jumps over the lazy dog a couple of times to make this
    2. long enough.
    3. 0 1 2 3 4 5 6 7
    4. 01234567890123456789012345678901234567890123456789012345678901234567890123456789
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Apr 2012
    Posts
    7
    Qt products
    Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Fgets after a seek .. catch text after seek

    let's test


    Added after 18 minutes:


    firstly give you thanks for the fast reply to the post ,, meanwhile i've changed the code a little bit and i'm trying to read all the .iso files at the subfolders of a specific root .. it's not
    working for a unknows reason .. may you take a glance ? what i do wrong ? thanks in advance for your help

    here i took a path_Cat function from some other

    char *config:ath_cat (const char *str1, char *str2)
    {
    size_t str1_len = strlen(str1);
    size_t str2_len = strlen(str2);
    char *result;
    result = (char *)malloc((str1_len+str2_len+2)*sizeof *result);
    strcpy (result,str1);
    size_t i,j;
    for(i=str1_len, j=0; ((i<(str1_len+str2_len)) && (j<str2_len));i++, j++)
    {
    result[i]=str2[j];
    }
    result[str1_len+str2_len]='\0';
    return result;
    }

    and then i try to read the .iso files inside any subfolder of the main named root (/mount) at this case

    char path[255];
    struct dirent *dp;

    // enter existing path to directory below


    const char *dir_path="/mount";

    DIR *dir = opendir(dir_path);
    if (!dir) return false;

    while ((dp=readdir(dir)) != NULL)
    {

    if (strcmp(dp->d_name,".")==0) continue;
    if (strcmp(dp->d_name,"..")==0) continue;

    char *tmp;
    tmp = path_cat(dir_path, dp->d_name);


    if (strstr (tmp, ".iso")!=NULL)
    {
    Last edited by wilcd; 16th May 2012 at 18:23.

  7. #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: Fgets after a seek .. catch text after seek

    what i do wrong ?
    You didn't use [code]...[/code] tags around your code

    This is a Qt forum, mostly C++, some Python and little traditional C code, so your questions are slightly misplaced.

    Your requirement is easily met by Qt components, without the risk of the memory leakage you probably have e.g. in path_cat(), better handling files containing ".iso" somewhere other than the end of the file name.

    You use QDir like this:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDir>
    3. #include <QDebug>
    4.  
    5. void scanDir(const QString &path)
    6. {
    7. QDir dir(path);
    8. QStringList filters;
    9. filters << "*.iso";
    10. foreach(const QString &file, dir.entryList(filters, QDir::Files))
    11. qDebug() << dir.filePath(file);
    12. }
    13.  
    14. int main(int argc, char **argv)
    15. {
    16. QCoreApplication app(argc, argv);
    17. scanDir("test");
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Apr 2012
    Posts
    7
    Qt products
    Qt/Embedded
    Platforms
    MacOS X Unix/X11

    Default Re: Fgets after a seek .. catch text after seek

    i used your code,and i've been checking the class doc,but looks like this code only works with specific root,and doesn't look for the .iso files inside the subfolders as i told you at the past post ... how to ?

  9. #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: Fgets after a seek .. catch text after seek

    Gosh, I don't know, perhaps you could program it to recurse.

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDir>
    3. #include <QDebug>
    4.  
    5. QStringList scanDir(const QString &path)
    6. {
    7. static const QStringList filters = QStringList() << "*.iso";
    8.  
    9. QStringList result;
    10. QDir dir(path);
    11. // Add the files in this directory
    12. foreach(const QString &file, dir.entryList(filters, QDir::Files))
    13. result << dir.filePath(file);
    14. // Check the sub directories
    15. foreach(const QString &subdir, dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot ))
    16. result << scanDir(dir.filePath(subdir));
    17. return result;
    18. }
    19.  
    20. int main(int argc, char **argv)
    21. {
    22. QCoreApplication app(argc, argv);
    23. qDebug() << scanDir("test");
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Phonon+seek
    By Fastman in forum Qt Programming
    Replies: 8
    Last Post: 8th September 2010, 11:31
  2. How to use the QFile::seek() in Qt-4.4.3
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2010, 12:54
  3. sqlite seek() bug?
    By ibergmark in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 08:44
  4. QFile, QTextStream, seek()
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 29th September 2006, 16:07

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.