Any Ideas How To Read Simple Text Files.


streak

Active Member
Joined
Jul 29, 2007
Messages
690
Age
42
Location
Poland & UK // Currently: Ipswich
Website
www.openpandora.pl
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
 
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.
 
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.
 
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?
 
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.
 
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...
 
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).
 
Sphinxter said:
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.
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:
yaustar said:
Sphinxter said:
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.
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.

Indeed. One thing I learned at school was: a good coders is a lazy coders.
Still believe it.
 
Last edited by a moderator:
Sphinxter said:
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.
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:
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.
 
Sphinxter said:
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.
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:
Back
Top