File IO

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Pickzell
Chaos Rift Junior
Chaos Rift Junior
Posts: 233
Joined: Sat May 16, 2009 10:21 am

File IO

Post 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;
   
}
I'm an altogether bad-natured Cupid.
User avatar
hurstshifter
ES Beta Backer
ES Beta Backer
Posts: 713
Joined: Mon Jun 08, 2009 8:33 pm
Favorite Gaming Platforms: SNES
Programming Language of Choice: C/++
Location: Boston, MA
Contact:

Re: File IO

Post 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.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: File IO

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: File IO

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Pickzell
Chaos Rift Junior
Chaos Rift Junior
Posts: 233
Joined: Sat May 16, 2009 10:21 am

Re: File IO

Post 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.
I'm an altogether bad-natured Cupid.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: File IO

Post 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
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: File IO

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: File IO

Post 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
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: File IO

Post 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).
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: File IO

Post 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.
177
User avatar
RyanPridgeon
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 447
Joined: Sun Sep 21, 2008 1:34 pm
Current Project: "Triangle"
Favorite Gaming Platforms: PC
Programming Language of Choice: C/C++
Location: UK
Contact:

Re: File IO

Post 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;
}

Ryan Pridgeon
C, C++, C#, Java, ActionScript 3, HaXe, PHP, VB.Net, Pascal
Music | Blog
Post Reply