QJ.NET | Videos | Forums | iPhone | MMORPG | Nintendo DS | Wii | PlayStation 3 | PSP | Xbox 360 | PC | Downloads | Contact Us
Forums | Gaming News | Videos | Downloads | Today's Posts | Mark Forums Read | Chat | FAQ | Members List | Contact

QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides

Go Back   QJ.net Game Discussion - PSP, Xbox, Wii, PS3, PSP Homebrew, and PSP Guides > Developers Corner > PSP Development, Hacks, and Homebrew > PSP Development Forum
The above video goes away if you are a member and logged in, so log in now!

writting simple data to a file.

This is a discussion on writting simple data to a file. within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; I trying to do something very very very simple. I want my program to read a file from the flash01 ...

Reply
 
LinkBack Thread Tools
Old 03-12-2006, 08:28 PM   #1
 
ピッカホ's Avatar
 
Join Date: Jan 2006
Location: In a Dark land
Posts: 176
Trader Feedback: 0
Default writting simple data to a file.

I trying to do something very very very simple. I want my program to read a file from the flash01 (system.dreg) and write it to and memory stick. The problem is that it does not write data to the file I created on the memory stick: "system.dreg". I have trying to get this work for days and I'm really get frustrated because it seems sooooo easy and the code I have seem to be right, yet its not working.

thanks in advance.

oh btw, for somereason O_RDONLY....etc aren't defined for some reason
Code:
#include <pspiofilemgr.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <stdio.h>
#include <psppower.h>
#include "graphics.h"
#include "26calls.h"


PSP_MODULE_INFO("getreg26", 0, 1, 1);

void freemem();


void ProcessKeys(unsigned long xikeys);
 /* Exit callback */
int exit_callback(int arg1, int arg2, void *common) {
          sceKernelExitGame();
          return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp) {
          int cbid;

          cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
          sceKernelRegisterExitCallback(cbid);

          sceKernelSleepThreadCB();

          return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void) {
          int thid = 0;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}

struct SceKernelLoadExecParam* hssa;
int pktime=0;
int selComponent=0;
int theld=0;
int sqHeld=0;
int oHeld=0;
int uheld=0;
int xheld=0;
int dheld=0;
int lheld=0;
int rheld=0;
int stHeld=0;
int y=10;
int doneit=0;
SceOff fcount=0;

char data[255];

SceIoStat stat;

SceUID fd;  
SceUID fd2; 	

#define printf pspDebugScreenPrintf
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))




int main() {
	int i=0;
        
    SetupCallbacks();
    initGraphics();
	initCtrl();
	SceCtrlData pad;
	freemem();
	
	
	
	printTextScreen(10, y, "press circle to dump reg", RGB(255, 0, 0));
	flipScreen();
	while(1)
	{
		sceCtrlReadBufferPositive(&pad, 1);
		WriteDwordDummy(0,22,pad.Buttons);
   		ProcessKeys(pad.Buttons);
		
        flipScreen();
        for(i=0; i<1; i++) {
            sceDisplayWaitVblankStart();
		}
	}

	sceKernelSleepThread();
	 return 0;
}



void ProcessKeys(unsigned long xikeys) {
  
   if (xikeys & PSP_CTRL_LEFT)
    {


    } 	
    if (xikeys & PSP_CTRL_RIGHT)
    {
    }
    if (xikeys & PSP_CTRL_DOWN)
    {
	if (!dheld)
	{

		
		dheld=1;
	} else 
	{
		dheld=0;
	}    
    }

    if (xikeys & PSP_CTRL_CROSS)
    {


    }

    if (xikeys & PSP_CTRL_UP)
    {
	if (!uheld)
	{	
	
		
		uheld=1;
	} else 
	{
		uheld=0;
	}

    }

    if (xikeys & PSP_CTRL_CIRCLE)
    {
		if (!oHeld)
		{
			if (doneit ==0)
			{
				y+=20;
				printTextScreen(10, y, "opening file...", RGB(255, 0, 0));
		
				fd = sceIoOpen("flash1:/registry/system.dreg", 0x0001, 0777);
				
				if (!fd)
				{
					y+=20;
					printTextScreen(10, y, "error can not open file!", RGB(255, 0, 0));
					
				} else {
					y+=20;
					printTextScreen(10, y, "sucess!", RGB(255, 0, 0));
				
					y+=20;
				}
				printTextScreen(10, y, "Processing", RGB(255, 0, 0));
			
				y+=20;
				printTextScreen(10, y, "creating file..", RGB(255, 0, 0));
				
				y+=20;
				fd2 = sceIoOpen("ms0:/system.dreg", 0x0002 | 0x0200 | 0x0400 , 0777);

				if (!fd2)
				{
					printTextScreen(10, y, "Error creating file!", RGB(255, 0, 0));
					flipScreen();
					return;
				}
				printTextScreen(10, y, "File created!", RGB(255, 0, 0));
			
				y+=20;		
				sceIoGetstat("flash1:/registry/system.dreg",&stat);
				printTextScreen(10, y, "writing to file...", RGB(255, 0, 0));
				flipScreen();
				y+=20;
				data[0] = 'h';
				data[1] = 'e';
				data[2] = '\0';
				//while (stat.st_size != fcount)
				//{
				//	fcount++;
					
					//sceIoRead(fd, data, sizeof(int)+100);
				sceIoWrite (fd2, "something", 255);
				//}
		
				printTextScreen(10, y, "done!", RGB(255, 0, 0));
				y+=20;
	
				doneit=1;
				oHeld = 1;
			}
		} else
		{
			oHeld = 0;
		}
    }

    if (xikeys & PSP_CTRL_SQUARE)
    {
	if (!stHeld)
	{
		


		sqHeld=1;	
	} else
	{
		sqHeld=0;
	}
    }
}

void freemem()
{
    int i=0;
	for (i=0;i<100;i++)
	{
		sceKernelFreePartitionMemory(i);
	}
}

Last edited by ピッカホ; 03-13-2006 at 01:46 PM..
ピッカホ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 03-13-2006, 01:47 PM   #2
 
ピッカホ's Avatar
 
Join Date: Jan 2006
Location: In a Dark land
Posts: 176
Trader Feedback: 0
Default

*bump* I still haven't figure this out yet. hmmm I wonder if I could use fopen insteadd.
ピッカホ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 03-13-2006, 02:34 PM   #3

AKA Homer
 
Moonchild's Avatar
 
Join Date: Jan 2006
Location: Sweden
Posts: 1,779
Trader Feedback: 0
Default

Copy directory (and all it's contents) and file function:

Code:
/** Structure to hold the time information for a file */
typedef struct _io_time {
	unsigned short year;
	unsigned short month;
	unsigned short day;
	unsigned short hour;
	unsigned short minute;
	unsigned short sec;
	unsigned short unk[2]; /* Set to 0, possibly just padding */
} io_time_t;

/** Structure to hold the status information about a file */
typedef struct {
	/** Access flags, zero or more of ::IOAccessModes */
	unsigned int access;
	/** Mode flags, zero or more of ::IOFileModes */
	unsigned int mode;
	/** Size of the file in bytes */
	long long size;
	/** Creation time */
	io_time_t ctime; 
	/** Access time (possibly) */
	io_time_t atime; 
	/** Modification time */
	io_time_t mtime; 
	/** Unknown, seem to be all 0xFFFFFFFF */
	unsigned int unk[6]; 
} io_stat_t;

/** Structure to hold a single directory entry */
typedef struct {
	/** File status */
	io_stat_t stat;
	/** File name */
	char name[264];
	/** Unknown */
	unsigned int dummy;
} io_dirent_t;

char line[4][256];

void num2hex(char *dst, int n) {
  int i;
  static char hex[]="0123456789ABCDEF";
  for (i=0; i<8; i++) {
	dst[i]=hex[(n>>((7-i)*4))&15];
  }
}

int pgCopyDir(char *, char *);

int pgCopyFile(char *src,char *dst) {
  int srcfd,dstfd,r,w,copied=0;
  char buf[1024];
  srcfd=sceIoOpen(src,SCE_O_RDONLY,0);
  if (srcfd<0) {
	return pgCopyDir(src,dst);	// if can't open as file, try to copy as dir
  }
  dstfd=sceIoOpen(dst,SCE_O_WRONLY|SCE_O_CREAT|SCE_O_TRUNC,0777);
  if (dstfd<0) { r=dstfd; goto out; }
  while(1) {
    r=sceIoRead(srcfd,buf,1024);
    if (r<=0) goto out;
    w=sceIoWrite(dstfd,buf,r); 
    if (w!=r) { r=0xfbadcafe; goto out; }  
    copied+=r;
    num2hex(line[2],copied);
    line[2][8]=0;
  }
out:
  if (srcfd>=0) sceIoClose(srcfd);
  if (dstfd>=0) sceIoClose(dstfd);
  return r;
}

int pgCopyDir(char *srcdir, char *dstdir) {
  void *dir;
  char src[256],dst[256],code[16]="--------";
  int slen,dlen,ret;
  io_dirent_t de;

  sceIoMkdir(dstdir,0777);
  strcpy(dst,dstdir);
  dlen=strlen(dst);
  dst[dlen]='/';
  strcpy(src,srcdir);
  slen=strlen(src);
  dir=sceIoDopen(src);
  src[slen]='/';
  if (dir==(void *)0) return -1;
  while((ret=sceIoDread(dir,&de))!=0) {
      if (de.name[0]=='.') {
	if (de.name[1]==0) continue;
	if (de.name[1]=='.' && de.name[2]==0) continue;
      }
      strcpy(src+slen+1,de.name);
      src[255]=0;
      strcpy(line[0],src);
      strcpy(dst+dlen+1,de.name);
      dst[255]=0;
      strcpy(line[1],dst);
      strcpy(line[2],"-");
      ret=pgCopyFile(src,dst);
  }
  return 0;
}
Don't worry about the warnings you get, they're not relevant for the code to work.
__________________


Click Here if you want a Winamp Currently Playing Userbar like the one above.
Moonchild is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 03-13-2006, 04:11 PM   #4
 
ピッカホ's Avatar
 
Join Date: Jan 2006
Location: In a Dark land
Posts: 176
Trader Feedback: 0
Default

thanks homer! I'll try it out when I get home! :icon_smil
ピッカホ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 03-13-2006, 06:37 PM   #5

Muppet Magnet
 
Fanjita's Avatar
 
Join Date: Sep 2005
Location: Edinburgh, UK
Posts: 2,388
Trader Feedback: 0
Default

BTW, looks like you aren't closing the file in your original code. If you don't close it, the PSP never flushes the data to the memstick.
Fanjita is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 03-13-2006, 07:10 PM   #6
 
ピッカホ's Avatar
 
Join Date: Jan 2006
Location: In a Dark land
Posts: 176
Trader Feedback: 0
Default

oh that did it.

THANKS YOU homer and fanjita!
ピッカホ is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
data , file , simple , writting

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -8. The time now is 04:32 AM.



Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY
Copyright © 2009, QJ.NET. All Rights Reserved.
Contact Us