gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Reading text files and displaying

#53774 - Dannon - Sun Sep 11, 2005 8:08 pm

Is there a good tutorial for reading a text file and then displaying it, whether it be straight to the console or using a tile set for the letters?

#53781 - tepples - Sun Sep 11, 2005 9:51 pm

Is it the "text" you have a problem with, or the "file"?

Do you want full bidirectional scrolling and full word wrapping?

Or are you willing to accept unidirectional (or limited bidirectional) scrolling and text pre-formatted to 30 characters wide? In the latter case, just use GBFS and AGBTTY, both available at my GBA page. You might have to change around some of AGBTTY to account for the minor differences between the GBA and the Nintendo DS, but if you have got anything to display in a tile mode before, you should be able to figure it out.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#53782 - Dannon - Sun Sep 11, 2005 10:09 pm

A bit of both really, I know how to get to the file with the Chishm's CF Reading code but then I'm not sure on howto actually read it into memory on the DS, can I just use char* text?

I would like wrap around so that the file can be as long as it wants and everything just sorts itself out

I've not done anything with tiles yet, I understand the basic principles, but I just wanted a good simple example that I could expland on

#53801 - chishm - Mon Sep 12, 2005 2:17 am

Dannon wrote:
A bit of both really, I know how to get to the file with the Chishm's CF Reading code but then I'm not sure on howto actually read it into memory on the DS, can I just use char* text?

Well, assuming your file is called test.txt, here is a simple example of how to open it and read it, the whole file at a time. This example will open the file for reading, find out its size, then read the entire thing into memory. (please note this is not tested, only an example)
Code:
FAT_InitFiles();  // Init FAT driver
FAT_FILE* handle = FAT_fopen("test.txt", "r");  // Open test.txt for reading
FAT_fseek(handle, 0, SEEK_END);  // Go to end of file
u32 size = FAT_ftell(handle);  // Get current position in file, because it is the end it will be the size
char* text = (char*) malloc (size);  // Allocate memory for file
FAT_fseek(handle, 0, SEEK_SET);  // Go to begining of file
FAT_fread((void*)text, size, 1, handle); // Read all of file into memory
FAT_fclose(handle); // Close file

This method fails when the file is too big to fit into memory, so if you don't know the size of the file before hand, I suggest reading in bits of the file at a time.
Code:
FAT_InitFiles();  // Init FAT driver
FAT_FILE* handle = FAT_fopen("test.txt", "r");  // Open test.txt for reading
u32 size = 256;  // Read in 256 bytes at a time
char* text = (char*) malloc (size+1);  // Allocate memory for string, including end of string character
text[size] = '\0'; // Add string terminate character
FAT_fseek(handle, 0, SEEK_SET);  // Go to begining of file
while (!FAT_feof(handle)
{
    FAT_fread((void*)text, size, 1, handle); // Read next bit of file into memory
    consolePrintf(text);  // Print file to screen
}
FAT_fclose(handle); // Close file


EDIT: Updated example for newest version of lib