Page 1 of 1

File IO

Posted: Wed Jul 15, 2009 3:16 pm
by Pickzell
I've been trying to learn Text File I/O for about 35 minutes total, and this is my way of grabbing a few words, now obviously this will take a lot of space. I have 8 char variables, which I think are 4 bytes each, so that adds up to 32 bytes. I'm trying to be a GBA programmer which multiplies the size of the game by 8, so that would be 256 bytes just to output "This text will now be inside of example.txt"

Is there a better way of doing this?
It would also help if someone would tell me you can even use text files on GBA. :lol:

Code: Select all

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
   char str[10];
   char str2[10];
   char str3[10];
   char str4[10];
   char str5[10];
   char str6[10];
   char str7[10];
   char str8[15];
   
   
   ofstream writefile( "example.txt" );
   writefile << "This text will now be inside of example.txt";
   writefile.close();
   
   ifstream readfile( "example.txt" );
   readfile >> str;
   readfile >> str2;
   readfile >> str3;
   readfile >> str4;
   readfile >> str5;
   readfile >> str6;
   readfile >> str7;
   readfile >> str8;
   cout << str << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << str6 << " " << str7 << " " << str8 << "\n";
   
   cin.get();
   return 0;
   
}

Re: File IO

Posted: Thu Jul 16, 2009 8:32 am
by hurstshifter
Pickzell wrote:I've been trying to learn Text File I/O for about 35 minutes total, and this is my way of grabbing a few words, now obviously this will take a lot of space. I have 8 char variables, which I think are 4 bytes each, so that adds up to 32 bytes. I'm trying to be a GBA programmer which multiplies the size of the game by 8, so that would be 256 bytes just to output "This text will now be inside of example.txt"

Is there a better way of doing this?
It would also help if someone would tell me you can even use text files on GBA. :lol:

Code: Select all

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
   char str[10];
   char str2[10];
   char str3[10];
   char str4[10];
   char str5[10];
   char str6[10];
   char str7[10];
   char str8[15];
   
   
   ofstream writefile( "example.txt" );
   writefile << "This text will now be inside of example.txt";
   writefile.close();
   
   ifstream readfile( "example.txt" );
   readfile >> str;
   readfile >> str2;
   readfile >> str3;
   readfile >> str4;
   readfile >> str5;
   readfile >> str6;
   readfile >> str7;
   readfile >> str8;
   cout << str << " " << str2 << " " << str3 << " " << str4 << " " << str5 << " " << str6 << " " << str7 << " " << str8 << "\n";
   
   cin.get();
   return 0;
   
}


I'm actually not yet familiar with file i/o, but I can tell you that the size of this should be even bigger than you expect. A char is actually 1 byte but these are char arrays so each would be at least 10 bytes (according to how you sized them), so about 80 bytes total. I'm not sure what you mean by a GBA game multiplies the size of it by 8.

Re: File IO

Posted: Thu Jul 16, 2009 9:48 am
by avansc
you might wanna learn how to do C file io if you wanna be a GBA programmer.

also

if you wanna know how many bytes a string has do this.

char *str = "bla bla bla";
int size = sizeof(char)*strlen(str); // you might have to add one. i always forget about the null terminating char.

anyways.

Re: File IO

Posted: Fri Jul 17, 2009 6:55 pm
by MarauderIIC
avansc wrote:if you wanna know how many bytes a string has do this.

char *str = "bla bla bla";
int size = sizeof(char)*strlen(str); // you might have to add one. i always forget about the null terminating char.
sizeof(str)?
hurstshifter wrote:A char is actually 1 byte but these are char arrays so each would be at least 10 bytes (according to how you sized them), so about 80 bytes total.
This is correct.
I'm not sure what you mean by a GBA game multiplies the size of it by 8.
I would guess, judging from NDS games, that what he means is that games are in sizes that are powers of 2? 2 mb, 4 mb, 8 mb, 64 mb, 128 mb, 256 mb... Really what should be done here is to find the size of whatever you have and then round up to the nearest power of 2, padding the rest of the space with null characters.

Re: File IO

Posted: Fri Jul 17, 2009 7:03 pm
by Pickzell
hurstshifter wrote: I'm not sure what you mean by a GBA game multiplies the size of it by 8.
When a game is put on a GBA cartridge the file size of the game is multiplied by 8, so a 1 MB game on PC would be an 8 MB game on GBA.
EG: A 256 MB cartridge would only actually hold 32 MBs.

I can't really describe it better. ):

And apparently you can use either C or C++ for GBA.

Re: File IO

Posted: Sat Jul 18, 2009 3:15 am
by Scoody
MarauderIIC wrote:
avansc wrote:if you wanna know how many bytes a string has do this.

char *str = "bla bla bla";
int size = sizeof(char)*strlen(str); // you might have to add one. i always forget about the null terminating char.
sizeof(str)?
That would just give the size of the pointer

Re: File IO

Posted: Sat Jul 18, 2009 9:40 am
by avansc
Scoody wrote:
MarauderIIC wrote:
avansc wrote:if you wanna know how many bytes a string has do this.

char *str = "bla bla bla";
int size = sizeof(char)*strlen(str); // you might have to add one. i always forget about the null terminating char.
sizeof(str)?
That would just give the size of the pointer

yes thats correct. in this case it would be the same. but you never use sizeof on a variable, only on data types.
the reason you use sizeof(datatype) and not sizeof(number) is because that code is not considered portable. although most
architectures these days have the same data sizes there can be some differences.

Re: File IO

Posted: Mon Aug 03, 2009 11:59 pm
by Ginto8
OK first of all: iostream would probably run like a cheetah carrying an elephant over quicksand on gba. Second: I believe all images and stuff are converted and simply part of the rom. This means that instead of reading from a file you would convert the picture into a source file containing info about it. For example, if you were making NDS homebrew using PAlib, you'd probably use PAgfx to convert your graphics. I believe that grit and gfx2gba are 2 programs you can use for the same purpose. I guess what I'm trying to say is that all file I/O on GBA or NDS would probably be using something similar libfat, to access either memory provided on the gba flash cart or (on DS) the microSD card. other than that you'd have all that stuff packed into your rom. And no, not all memory usage is multiplied by 8. To me it just seems that your rom would get padded with NOP's at the end of your rom until it is a power of 2 in size. And I think that for a beginner programmer like you it would be better to start with NDS development, because that allows a bit more wiggle room (it seems that the GBA has <1 MB of ram, while the DS has 4 MB. Also, the NDS has 2 processors, not just one). Both are very similar (NDS actually borrowed some of the GBA's input hardware), so it shouldn't be too difficult to transition from one to the other.

I seem to have rambled on, and I don't know how to end this, so I'll just post this how it is. ;) enjoy

Re: File IO

Posted: Tue Aug 04, 2009 7:20 am
by Falco Girgis
Seriously, if you don't learn C IO, you are going to murder your poor GBA. You may be able to use C++, but the C++ standard libraries are pretty taxing even on a Dreamcast or PSP. You're going to rape a GBA (if those libraries even run).

Re: File IO

Posted: Tue Aug 04, 2009 9:26 am
by Amarant
Pickzell wrote:
hurstshifter wrote: I'm not sure what you mean by a GBA game multiplies the size of it by 8.
When a game is put on a GBA cartridge the file size of the game is multiplied by 8, so a 1 MB game on PC would be an 8 MB game on GBA.
EG: A 256 MB cartridge would only actually hold 32 MBs.
You're probably confusing megabit with megabyte, often the sizes of roms will be denoted using megabits.
If you have the size of a rom in megabytes and want it in megabits you multiply by 8.

So the cartridge is likely to have 256 megabit of storage and not 256 megabyte.

Re: File IO

Posted: Wed Aug 05, 2009 5:34 am
by RyanPridgeon
Agreed with Falco, use C.

In which case your code will look nicer aswell xD

Code: Select all

#include "stdio.h"

int main(int argc, char** argv){
    // variables; one to handle the file, one to store the string
    FILE* file;
    char str[128];
    
    // now we open the file in write-mode ("w") and print some text into it
    file = fopen("example.txt", "w");
    fprintf(file, "This text will now be inside of example.txt");
    fclose(file);

    // now we open it again in read-mode ("r") and use fgets to take a line of text out of the file into str
    file = fopen("example.txt", "r");
    fgets(str, 128, file);
    fclose(file);

    // now we print the contents of str, which should be what was in the file :)
    printf("%s", str);

    return 0;
}