file io->fgets equivalent?

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

Moderators: cheriff, TyRaNiD

Post Reply
User avatar
Stellar
Posts: 48
Joined: Mon Dec 12, 2005 9:13 am

file io->fgets equivalent?

Post by Stellar »

Im porting one of my works from pc in directx to psp. I need to port my file routines, which i usually use FILE* and fread/fgets/.... . It seems now, that we should be using file descriptors and psp specific io functions.

therefore, what is the equivalent for fgets?

thanks,
-stellar
Image
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Re: file io->fgets equivalent?

Post by jimparis »

Stellar wrote:Im porting one of my works from pc in directx to psp. I need to port my file routines, which i usually use FILE* and fread/fgets/.... . It seems now, that we should be using file descriptors and psp specific io functions.
No, unless you have a specific reason you can't use newlib, just go ahead and keep using fopen and fgets. Newlib is linked by default in build.mak.
User avatar
Stellar
Posts: 48
Joined: Mon Dec 12, 2005 9:13 am

Post by Stellar »

I tried using it. it compiles no problem, but it acts just like read does. It wont stop reading at a newline the way fgets on pc does. instead, it reads the size that is passed in :( ?
Image
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

It might not treat newlines the same (ie. your PC might accept just "\r" while the PSP might want "\n", etc). If you post an example of code that has a problem, and upload a zip somewhere with the text file you're trying to read, maybe we can see a problem.
User avatar
Stellar
Posts: 48
Joined: Mon Dec 12, 2005 9:13 am

Post by Stellar »

the text file was converted using ' dos2unix rocket.x3d rocket.x3d '

i have attached the source code, which is in its simplest form, it should just write the file out to the screen.

zip attached here

Code: Select all

void ReadFile()
{
	char szBuffer[128];
	FILE *fd = fopen( "rocket.x3d", "r" );

	if ( fd )
	{
		while ( fgets( szBuffer, sizeof( szBuffer ), fd ) )
		{
			printf( "%s", szBuffer );
		}

		fclose( fd );
	}
	else
	{
		printf( "failed to open (%s)\n", "rocket.x3d" );
	}
}
and thanks for you generosity :)
Image
dbeyer3069
Posts: 81
Joined: Mon Dec 19, 2005 4:09 pm

Post by dbeyer3069 »

Stellar wrote:I tried using it. it compiles no problem, but it acts just like read does. It wont stop reading at a newline the way fgets on pc does. instead, it reads the size that is passed in :( ?
I played with this one day and couldn't get results I was happy with. I was lucky that the data I was working with (array of structures) could be (and is) stored in a structure so I just did a read (sizeof(structure) * number of entries) and did a write for the same thing. It's faster too with only one read and write. I got nothing but general wierdness with the linefeeds (\n) in the files. If your data is setup this way, you may want to try this.

Note: Many string functions are not implemented yet (or not fully) and there's no guarantee things work as they do in standard libs. Look at the source for those libraries and you'll see what I mean.

If you can do your reads/writes in the same way I do (arrays of structures), you may consider converting them on the pc side and moving the data over to be read by the psp app (without linefeeds).

David Beyer
User avatar
Stellar
Posts: 48
Joined: Mon Dec 12, 2005 9:13 am

Post by Stellar »

Ive already started adding a binary option for my exporter. I know ill have to do it eventually just to keep file sizes down and make thinks simpler. in terms of tring functions. I stay exclusive to standard c routines, so im assuming you're not referring to strcat, strcmp, strcpy. thats pretty much all i use :)

anyway, i wanted to read text cause thats the way my pc model class works and I just wanted to see some results on the psp since this is my first program, just anxious i guess.
Image
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

Stellar wrote:i have attached the source code, which is in its simplest form, it should just write the file out to the screen.
OK, now I'm confused. Your program works perfectly. The entire file gets copied to the screen. Where's the problem? I also modified it to make sure fgets() was only returning one line at a time, and it is.
dbeyer3069 wrote:Note: Many string functions are not implemented yet (or not fully) and there's no guarantee things work as they do in standard libs. Look at the source for those libraries and you'll see what I mean.
What string functions are not implemented or are not complete? Newlib is a pretty standard library.
dbeyer3069
Posts: 81
Joined: Mon Dec 19, 2005 4:09 pm

Post by dbeyer3069 »

What string functions are not implemented or are not complete? Newlib is a pretty standard library.
Good question. I reinstalled and updated all my pspdev (cygwin) today and I cannot find the sources I was looking at (must have been much older ones). The source code (stdio) had numerous functions commented out and said they weren't implemented fully (and I tried with no luck). Looks like I have replaced all that.

I had been looking at sscanf() which wasn't implemented into the sources I had. Glad you mentioned this -- I can try it again. Not sure how I got such old stuff.

David Beyer
Post Reply