Wrong with my read code?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Wrong with my read code?

Post by Ghoti »

Hi folks,

i have this read code that reads the settings for info and saves that into variables

the problem however is that the opening of a file goes correctly but the actual reading not:

Code: Select all

// -----------------------------------------
//
// LoadSettings
//
// -----------------------------------------
	int LoadSettings(void){

		int fdout;
		int fd2;
		//char sTmpLetter[2];

		fdout = sceIoOpen("settings.txt", PSP_O_RDONLY, 0777);
		if&#40;fdout < 0&#41;
		&#123;
			DebugPrint&#40;1,1, "error opening SETTINGS.TXT &#58;&#40;"&#41;;
			fdout = sceIoOpen&#40;"SETTINGS.TXT", PSP_O_RDONLY, 0777&#41;;
				if&#40;fdout < 0&#41;
				&#123;
					DebugPrint&#40;1,2, "error opening settings.txt &#58;&#40;"&#41;;
				&#125;
		&#125;
		
		fd2 = sceIoRead&#40;fdout, Settings, strlen&#40;Settings&#41;&#41;;
		sprintf&#40;sBuffer, "%i", fd2&#41;;
		if&#40;fd2 <= 0&#41;
		&#123;	
			DebugPrint&#40;1,3, "error bytes read error"&#41;;
			DebugPrint&#40;1,4, sBuffer&#41;;
			sceIoClose&#40;fdout&#41;;
			sceGuTerm&#40;&#41;;
			sceKernelExitGame&#40;&#41;;
		&#125;

		int a, i;
		int aLast, aTmp;
		aLast = 0;
		aTmp = 1;

		for &#40;a=0;a<101;a++&#41;&#123;
			if &#40;Settings&#91;a&#93; == '*'&#41;&#123;
				if &#40;aTmp == 1&#41; &#123;
					sprintf&#40;sBuffer, "0"&#41;;
					for &#40;i=aLast;i<a;i++&#41;&#123;
						sprintf&#40;sBuffer, "%s%c",sBuffer,Settings&#91;i&#93;&#41;;
					&#125;

					iLeftRightSpeed = atoi&#40;sBuffer&#41;;
					iLRSpeed = 5 + &#40;iLeftRightSpeed *5&#41;;
					aLast = a+1;
					aTmp += 1;
				&#125;
				else if &#40;aTmp == 2&#41; &#123;
					sprintf&#40;sBuffer, "0"&#41;;
					for &#40;i=aLast;i<a;i++&#41;&#123;
						sprintf&#40;sBuffer, "%s%c",sBuffer,Settings&#91;i&#93;&#41;;
					&#125;
					iStatusSound = atoi&#40;sBuffer&#41;;
					aLast = a+1;
					aTmp += 1;
				&#125;
			&#125;

		&#125;
		return 0;
	&#125;
fdout has value 7
fd2 has value 0

why is this?
moonlight
Posts: 567
Joined: Wed Oct 26, 2005 7:46 pm

Post by moonlight »

Settings strlen(Settings)?

Maybe if you have something like this

char Settings[64];

you should do

fd2 = sceIoRead(fdout, Settings, 64-1);

if (fd2 >= 0)
Settings[fd2] = 0;
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

Thanks it works :)
subbie
Posts: 122
Joined: Thu May 05, 2005 4:14 am

Post by subbie »

fyi, (few pointers)

strlen tells you the length of the string in the varible. So even if your array is say 64 bytes but all it contains is "" the result will be zero.

It's best to just keep track of the size of your array you're storing. Or better yet, get the length of the file using the seek command.
Post Reply