Any Ideas How To Read Simple Text Files.

Discussion in 'General Discussions' started by streak, Aug 16, 2007.

  1. streak

    streak Member

    Joined:
    Jul 29, 2007
    Messages:
    690
    Location:
    Poland & UK // Currently: Ipswich
    Hi Again

    Anybody has got any idea how to read simple text files [*.txt] from SD card to memory and view it on screen?
    Im trying to make a simple text viewer, but you know, im a Delphi/Pascal baby [ not C++] so if any1 could help i'll be very thankfull. [ simple source ]

    StreaK
     
    Tags:
  2. yaustar

    yaustar UK GP32 & GP2X Owner

    Joined:
    Oct 18, 2003
    Messages:
    2,714
    Location:
    UK
  3. rabidcow

    rabidcow Still Fresh

    Joined:
    Nov 12, 2005
    Messages:
    56
    If you want to read line-by-line, there's std::getline (the one that's not a class member) for iostreams/std::strings and fgets for FILEs/char*s.
     
  4. Sphinxter

    Sphinxter Says What?

    Joined:
    Oct 1, 2006
    Messages:
    2,897
    Location:
    Silicon Valley California, USA
    Last edited by a moderator: Dec 17, 2015
  5. Sphinxter

    Sphinxter Says What?

    Joined:
    Oct 1, 2006
    Messages:
    2,897
    Location:
    Silicon Valley California, USA
    There are only files, text and binary are artificial states of mind, free yourself. I say when embedding anything extra is just extra crap so all you really need or actually want to use is:

    open
    lseek
    read
    write
    close

    Opening a file and sucking it in is probably the single most time consuming slowest thing you'll ever do on a computer so I like to rip it off right up front all at once like a band-aid.
    I'd do it in C and pragma to use it from ++. Two conditions to write for, one can I read this file all at once and git'r done or do I paw through it one byte at a time building strings to parse watching for linefeeds.

    CODE

    int rfile = open( path, O_RDONLY );
    if ( rfile > 1 )
    {
    sizeofile = lseek( rfile, 0L, SEEK_END );
    lseek( rfile, 0L, SEEK_SET );
    txt = (char *)calloc( sizeofile + 1, sizeof(char));
    if ( txt != NULL )
    {
    txt[ sizeofile ] = '\0';
    actual = read( rfile, txt, sizeofile );
    if ( actual == sizeofile )
    {
    while ( next = (char *)strtok( txt, "\n" ))
    puts( "%s" );
    }
    else
    fprintf( stderr, "error short read!\n" );
    free( txt );
    }
    else
    fprintf( stderr, "error allocating\n" );
    close( rfile );
    }
    else
    fprintf( stderr, "error open\n" );
    }



    Character at a time stick a while loop in there calling read til eof.
    Binary, same only catch the return of read with the appropriate type pointer.
    To do a cat on any size would just read char write char. Sample not compiled or claimed to.
     
  6. yaustar

    yaustar UK GP32 & GP2X Owner

    Joined:
    Oct 18, 2003
    Messages:
    2,714
    Location:
    UK
    Or you can use files streams and not worry about all the low level implementation needed such allocating and freeing memory:
    CODE
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdio>

    using namespace std;

    int main()
    {
    ifstream fin( "readme.txt" );
    string text = "";
    if( fin.is_open() )
    {
    getline( fin, text, static_cast<char>(EOF) );
    fin.close();
    }
    else
    {
    cerr << "Could not open file" << endl;
    }

    cout << text << endl;

    return 0;
    }


    Granted it may not be fastest method out there, if the OP is using C++ why re-invent the wheel? How much faster would your implementation be anyway with proof?
     
  7. streak

    streak Member

    Joined:
    Jul 29, 2007
    Messages:
    690
    Location:
    Poland &amp; UK // Currently: Ipswich
    Instead of makin' another hello world, im trying to make a simple GP2X Zine in *.gpe format so i dont need wery fast routines. Every routine will be good. Full app is a compilation of SDL playing modules + SDL viewing images and a simple text file reader - i think its a good start to another much advanced project :D

    BTW. Thank You for sample sources.
     
  8. PokeParadox

    PokeParadox Founder of Pirate Games - Penjin Coder Staff Member

    Joined:
    Dec 8, 2005
    Messages:
    6,512
    Location:
    UK
    The point of standard libs is to make things simpler for the programmer. They are usually very optimal, and while they may not be as fast as tailer-made, lower-level implementation, the differences in performances aren't going to make much of a difference in most cases...
     
  9. Dzz

    Dzz stmia r0!, {r2-r9}

    Joined:
    Jan 30, 2006
    Messages:
    1,098
    On a large file there is some advantage to using a single read, but the difference is not huge. I just tested the read() method vs the getline() method on a 1 mb text file stored on my SD card. Each run was done after a fresh reboot.

    open/malloc/read: 0.56 seconds
    ifstream/getline: 0.71 seconds

    I am not really arguing for either method (it's your code, do what you want).
     
  10. Sphinxter

    Sphinxter Says What?

    Joined:
    Oct 1, 2006
    Messages:
    2,897
    Location:
    Silicon Valley California, USA
    A man once told me there are only three things that really matter in software and that's performance, performance and performance, still believe it.
     
  11. yaustar

    yaustar UK GP32 & GP2X Owner

    Joined:
    Oct 18, 2003
    Messages:
    2,714
    Location:
    UK
    I disagree. Performance is usually the last problem to handle. Optimisation only what you need to optimise and only after profiling. Being a lazy programmer using code that is reusable and requires less maintenance is much more important in my opinion.
     
    Last edited by a moderator: Dec 18, 2015
  12. Sphinxter

    Sphinxter Says What?

    Joined:
    Oct 1, 2006
    Messages:
    2,897
    Location:
    Silicon Valley California, USA
    I'll just shut up and sink quietly into the tar pit, maybe someday my remains will be used to fuel programs built at even higher levels.
     
  13. pepone

    pepone Pouet! Pouet! Do want!

    Joined:
    Oct 14, 2005
    Messages:
    504
     
    Last edited by a moderator: Dec 18, 2015
  14. Squidge

    Squidge Certified Guru

    Joined:
    Nov 16, 2003
    Messages:
    8,495
    Location:
    UK
    Strange, what I go told in school, college & university was "Make it work first, make that the first version, and then optimise later when you have determined the parts that require optimisation through testing and profiling".

    Also, the other thing I got told regularly was "Less code = less debugging; if code is already written for the purpose you require, use it."
     
    Last edited by a moderator: Dec 18, 2015
  15. PokeParadox

    PokeParadox Founder of Pirate Games - Penjin Coder Staff Member

    Joined:
    Dec 8, 2005
    Messages:
    6,512
    Location:
    UK
    Yup... get it going... then get it going faster once you have it going.
     
  16. Sphinxter

    Sphinxter Says What?

    Joined:
    Oct 1, 2006
    Messages:
    2,897
    Location:
    Silicon Valley California, USA
    Sorry laziness is a great attribute for unix system administrators but not for programmers, appalling what they pass off in schools today, no wonder great programmers are so far and few between.
     
  17. yaustar

    yaustar UK GP32 & GP2X Owner

    Joined:
    Oct 18, 2003
    Messages:
    2,714
    Location:
    UK
    It comes under the school of thought, "Do as little work as possible to get the task completed, but no less". Why re-invent the wheel if the wheel is already good enough for the job and meets the requirements? The NIH syndrome is common among programmers.
     
  18. Sphinxter

    Sphinxter Says What?

    Joined:
    Oct 1, 2006
    Messages:
    2,897
    Location:
    Silicon Valley California, USA
    My school thought wheel building was good exercise.
     
  19. Perky_Goth

    Perky_Goth Member

    Joined:
    Feb 5, 2007
    Messages:
    129
    Location:
    Portugal
    it comes from "there's a lot of stuff to be done besides this, let's not worry about a 1Mhz problem". On the other hand, that's why pc's always feel slow despite the upgrade, but at least they're shiny :D
     
    Last edited by a moderator: Dec 18, 2015

Share This Page

Loading...