Welcome! Log In Create A New Profile

Advanced

Input file stream on Wii

Posted by diego_pmc 
Input file stream on Wii
September 23, 2009 03:29PM
Hello,
I'm trying to port a small text based game I wrote for Windows over to Wii Homebrew. Currently I'm trying to figure out how to read text files and print out lines from the text onto the screen. However I can't do it the same way I did it in Windows (with ifstream and getline()); I have read a tutorial from CodeMii, "Using the filesystem", but it isn't very clear in all areas. For example it doesn't specify what header files I need, what libraries I should include in the MakeFile, etc. Below is the source, and the error report that I get from devkitPro:

Source — the code isn't part of my actual game, it's just trying to read a text file and print its first line on-screen. Here's the downloadable source, or look below to see it:
main.cpp
#include 
#include string>  // all these begin with a "<", but I had to remove them in this post
#include iostream>
#include stdlib.h>
#include gccore.h>
#include wiiuse/wpad.h>
#include fat.h>

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;

void InitWii();
void WaitForHome();

int main()
{

	InitWii(); // calls all the needed init functions
	
	// OPEN a text file and print the 1st line on the screen
	FILE* txt = NULL;
	if(!fatInitDefault()) printf("\nfatInitDefault() failed.");
	
	txt = fopen("info.txt", "r");
	if (txt == NULL) printf("\nFile open failed.");
	
	char line[50];
	fgets(line, sizeof line, txt);
	for (int i = 0; i < 50; i++) {
		std::cout << line;
	}
	
	fclose(txt);

	// wait for HOME on Wiimote
	WaitForHome();

	return 0;
}

MakeFile
#---------------------------------------------------------------------------------
# Clear the implicit built in rules
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITPPC)),)
$(error "Please set DEVKITPPC in your environment. export DEVKITPPC=devkitPPC")
endif

include $(DEVKITPPC)/wii_rules

#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
#---------------------------------------------------------------------------------
TARGET		:=	$(notdir $(CURDIR))
BUILD		:=	build
SOURCES		:=	source
DATA		:=	data  
INCLUDES	:=

#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------

CFLAGS	= -g -O2 -Wall $(MACHDEP) $(INCLUDE)
CXXFLAGS	=	$(CFLAGS)

LDFLAGS	=	-g $(MACHDEP) -Wl,-Map,$(notdir $@).map

#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS	:=	-lwiiuse -lbte -logc -lm -lfat

#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS	:=

#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------

export OUTPUT	:=	$(CURDIR)/$(TARGET)

export VPATH	:=	$(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
					$(foreach dir,$(DATA),$(CURDIR)/$(dir))

export DEPSDIR	:=	$(CURDIR)/$(BUILD)

#---------------------------------------------------------------------------------
# automatically build a list of object files for our project
#---------------------------------------------------------------------------------
CFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES	:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
sFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
SFILES		:=	$(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.S)))
BINFILES	:=	$(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))

#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
	export LD	:=	$(CC)
else
	export LD	:=	$(CXX)
endif

export OFILES	:=	$(addsuffix .o,$(BINFILES)) \
					$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) \
					$(sFILES:.s=.o) $(SFILES:.S=.o)

#---------------------------------------------------------------------------------
# build a list of include paths
#---------------------------------------------------------------------------------
export INCLUDE	:=	$(foreach dir,$(INCLUDES), -iquote $(CURDIR)/$(dir)) \
					$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
					-I$(CURDIR)/$(BUILD) \
					-I$(LIBOGC_INC)

#---------------------------------------------------------------------------------
# build a list of library paths
#---------------------------------------------------------------------------------
export LIBPATHS	:=	$(foreach dir,$(LIBDIRS),-L$(dir)/lib) \
					-L$(LIBOGC_LIB)

export OUTPUT	:=	$(CURDIR)/$(TARGET)
.PHONY: $(BUILD) clean

#---------------------------------------------------------------------------------
$(BUILD):
	@[ -d $@ ] || mkdir -p $@
	@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

#---------------------------------------------------------------------------------
clean:
	@echo clean ...
	@rm -fr $(BUILD) $(OUTPUT).elf $(OUTPUT).dol

#---------------------------------------------------------------------------------
run:
	wiiload $(TARGET).dol


#---------------------------------------------------------------------------------
else

DEPENDS	:=	$(OFILES:.o=.d)

#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).dol: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)

#---------------------------------------------------------------------------------
# This rule links in binary data with the .jpg extension
#---------------------------------------------------------------------------------
%.jpg.o	:	%.jpg
#---------------------------------------------------------------------------------
	@echo $(notdir $<)
	$(bin2o)

-include $(DEPENDS)

#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------

Error report
Quote
devkitPro
> "make"
main.cpp
linking ... text_reader.elf
c:/devkitPro/libogc/lib/wii\libfat.a(disc.o): In function `get_io_wiisd':
/Users/davem/projects/devkitpro/libnds-master/libfat/libogc/../source/disc.c:48: undefined reference to `__io_wiisd'
/Users/davem/projects/devkitpro/libnds-master/libfat/libogc/../source/disc.c:48: undefined reference to `__io_wiisd'
c:/devkitPro/libogc/lib/wii\libfat.a(disc.o): In function `get_io_usbstorage':
/Users/davem/projects/devkitpro/libnds-master/libfat/libogc/../source/disc.c:51: undefined reference to `__io_usbstorage'
/Users/davem/projects/devkitpro/libnds-master/libfat/libogc/../source/disc.c:51: undefined reference to `__io_usbstorage'
collect2: ld returned 1 exit status
make[1]: *** [/c/projects/wii/text_reader/text_reader.elf] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01

So could you please tell me exactly what libraries I need to include in the MakeFile, what headers I should include, and possibly show me some code on how to print a line from the file? Thanks!



Edited 1 time(s). Last edit at 09/23/2009 03:33PM by diego_pmc.
Re: Input file stream on Wii
September 23, 2009 05:03PM
In your Makefile change the order of lfat, try this:
LIBS	:=	-lfat -lwiiuse -lbte -logc -lm
Re: Input file stream on Wii
September 23, 2009 10:07PM
Thanks, that fixed it. I have another question now:
I get this error:
Quote
devkitPro
> "make"
linking ... Risen.elf
c:/devkitpro/devkitppc/bin/../lib/gcc/powerpc-gekko/4.2.4/../../../../powerpc-gekko/lib\libc.a(lib_a-system.o): In function `_system_r':
(.text+0xbc): undefined reference to `_execve'
collect2: ld returned 1 exit status
make[1]: *** [/c/projects/wii/Risen/Risen.elf] Error 1
"make": *** [build] Error 2

> Process Exit Code: 2
> Time Taken: 00:01
Re: Input file stream on Wii
September 23, 2009 10:14PM
I think it's because you are using a function called execve and the lib file is missing or the header file with the definition is absent.
Re: Input file stream on Wii
September 23, 2009 10:21PM
The error looks exactly like the ones I mentioned previously, so I figured it might be because of some library I'm not including in the MakeFile. I certainly am not using any execve directly. Do you know what library I would need to include in the MakeFile for this?
Re: Input file stream on Wii
September 23, 2009 10:32PM
I downloaded your code and I was able to compile it without any problem. Did you add new stuff? If yes, could you upload the new version.
Re: Input file stream on Wii
September 23, 2009 10:34PM
Here it is:
> http://upload.worldofplayers.de/files4/Sx2J0TGunWRisen.rar

EDIT: The problems appears because of lines 47 and 57 in main.cpp:
47: plyr = ProcessSection(tb, plyr);
57: plyr = ProcessBattle(btl_data, plyr);

If I comment these lines out, devkitPro is able to create the *.elf and *.dol files without any problems.



Edited 4 time(s). Last edit at 09/23/2009 10:39PM by diego_pmc.
Re: Input file stream on Wii
September 23, 2009 10:58PM
The thing the two functions have both in common is they are calling this line:
system("cls");
If that's commented, you can compile without problem...



Edited 1 time(s). Last edit at 09/23/2009 10:59PM by Crayon.
Re: Input file stream on Wii
September 23, 2009 11:05PM
Check this post: => by frontier 21 Apr 2008 18:45 on [translate.google.com]
Re: Input file stream on Wii
September 24, 2009 02:32AM
printf ("\x1b[2J");

This is the clear screen function for the terminal.
Re: Input file stream on Wii
September 24, 2009 01:39PM
Yeah, but you should use something that is using termcap to make the application portable. Not to mention a lot easier to code. It's much easier to work with an api that takes care of these details for you.
Sorry, only registered users may post in this forum.

Click here to login