samit_ray
Registered: 11/05/12 Posts: 52
|
|
Posted 19/07/12
|
#1
|
|
i am trying to save a file using dynamically generated names in an array. when i pass static names the file gets created, but when i pass the same name generated dynamically, file does not get created (instead file "." is created with 0 size and the operation is reported as success).
checked and double checked the dynamically generated value, null terminator etc everything looks good
here is the code below
var Disk,filenames;
func main() var fileID; fileID := 358;
init_Drive();
repeat gfx_Cls(); gfx_MoveTo(10,10); mem_Free(filenames); //just in case if there is some previous allocation
filenames := mem_Alloc(13);// max filename+null terminator
if(filenames)// allocation sucessfull mem_Set(filenames,0,13); //set all bytes to NULL // convert the fileID number as a valid file name by appending "-" till eight position //and then append ".TXT\0" to get a full file name
convertToFileName(filenames,&fileID); //we will get 358-----.TXT with null terminator endif saveFile(filenames); //dynamically generated name fails //saveFile("358-----.txt"); //static name works forever
endfunc
func saveFile(var nfile)
var fileHandle,status; print("File Name ",[STR]nfile,"\n");
//removed stuff for checking file mounted
//chek if file exists if(file_Exists(nfile)) //if(!file_Erase(nfile)) //delete print("Save Failed, file exist\n"); pause(5000); return; //endif endif //after successfull delete fileHandle := file_Open(nfile, 'w'); //for dynamic names this works !!!!! //removed stuff for checking file open error print("File handle ",fileHandle,"\n");
file_PutS("test-test", fileHandle); //removed stuff for checking PutS success status := file_Close(fileHandle); print("File closed ",status,"\n"); //removed stuff for checking file close error
print("File ",[STR]nfile," creation ",file_Exists(nfile),"\n"); //get yes for dynamic name but no file on disk pause(5000); //status := file_Exists(nfile);//remove the file so that the infinite loop in main remains OK
endfunc
//take some number (assumes to be less than 9999) //and create a string that makes a valid file name // by appending "-" till eight position and then ".TXT\0" till 13 th position func convertToFileName(var linePtr,var valuesPtr) var iIndex, strptr; to(linePtr); print(*valuesPtr); strptr := str_Ptr(linePtr); //check each item for > 47 (48 is 0) //if not replace by '-' 45 for(iIndex := 0; iIndex < 9; iIndex++) if(str_GetByte(strptr+iIndex)<48) str_PutByte((strptr+iIndex), 45); next //replace index 8-12 with ".TXT\0" str_Copy(strptr+8, ".TXT\0"); endfunc
//init file system func init_Drive() var retry := 10; if(!(Disk := file_Mount())) while(retry--) if((Disk := file_Mount())) break; wend //if (retry) showMessage("File Mount Failed",ON); endif //showMessage("File Mount Success",OFF); endfunc
|
|
Loading...
|
|
meldavia
Registered: 18/03/07 Posts: 900
|
|
Posted 19/07/12
|
#2
|
|
if a pointer is used for filename, it must be a standard word aligned buffer pointer.
eg:-
filename[7];
to(filename); str_Printf(&p, "%s.txt"); // copy filename and extension from string fileHandle := file_Open(filename, 'w');
__________________ Regards,
Dave
|
|
Loading...
|
|
samit_ray
Registered: 11/05/12 Posts: 52
|
|
Posted 20/07/12
|
#3
|
|
Nope, dynamic text does not seem to work. File name has to be fixed at compile time else file_Open fails.... (which is a big pain)
I got a simple program below just to test the dynamic name,
If you run the code below directly it loads the file as the name is available at compile time,
however uncomment **1**, **2**,**3** and comment out lines below **2**,**3** this causes name to be copied dynamically file_open fails.
Alternate test case where "PROFILE.TXT" string is generated at run time **4** is sent to load file, this also fails as expected even if you copy to word aligned buffer or not
sample file used for the test is attached, or u could create any file named "PROFILE.TXT" and dump few chars (<30) each line
#CONST CMD_PACKET_BUFFER_SIZE 30 #END
var Disk,filenames,cmd_packet[CMD_PACKET_BUFFER_SIZE]; var StaticText[1]; // static strings StaticText[0] := "PROFILE.TXT";
func main() init_Drive(); filenames := mem_AllocZ(13);
to(filenames); print("PROFILE.TXT");//generate file name dynamically
repeat gfx_Cls();
loadFile(StaticText[0]);//send compile time static file name //**4**loadFile(filenames);//send dynamically generated filename
forever
endfunc
func loadFile(var fileName) var fileHandle, localFileName[13]; //**1** to(localFileName); str_Printf(&fileName, "%s");
if(Disk) //**2** if(file_Exists(localFileName)) if(file_Exists(fileName)) //**3** fileHandle := file_Open(localFileName, 'r'); fileHandle := file_Open(fileName, 'r'); if(!file_Error()) print("File Opened\n"); pause(2000); while(file_GetS(cmd_packet, CMD_PACKET_BUFFER_SIZE, fileHandle)) print([STR]cmd_packet,"\n"); pause(500); //initilize the buffer before read mem_Set(cmd_packet, 0, CMD_PACKET_BUFFER_SIZE); wend //end loop if 0 or less bytes were read //close file file_Close(fileHandle); mem_Set(cmd_packet, 0, CMD_PACKET_BUFFER_SIZE); endif//file opened without error else print("File NOT found"); pause(2000); endif else print("File Mount Failed"); pause(2000); endif
endfunc
Attached Files
PROFILE.TXT (146 Bytes, 0 views)
|
|
Loading...
|
|
meldavia
Registered: 18/03/07 Posts: 900
|
|
Posted 21/07/12
|
#4
|
|
|
I will look at your code a bit closer later - at a quick glance I cant fathom the problem, but I'm sure there are many examples that use similar principals that do work.
After thinking a bit further, I think I am wrong in saying that filenames only work on even boundaries. I recalled this piece of code from an assembler I started to write... //================================================== ============ // open an include file, swapping hadle with current file // include files are only read in first pass //================================================== ============ func openIncludeFile() var private tmpHandle; // 0, or holds main source handle if(tmpHandle) // error, includes can only be 1 level putstr("Error, only 1 include level allowed"); else if((tmpHandle := file_Open(str_Ptr(incfname) ,'r'))) // open include file SWAP(&hFile, &tmpHandle); // swap the handles else putstr("Cant open include file"); endif endif endfunc
this indicates that string pointers are indeed ok for filenames.
I have included the entire project for you to pick through, remember , its just a WIP but does however do a fair bit of work towards the final goal, and I'm quite sure it will unravel your problem.
Attached Files
Pic10F_Assembler_Incomplete.zip (25.17 KB, 8 views)
__________________ Regards,
Dave
|
|
Loading...
|
|
samit_ray
Registered: 11/05/12 Posts: 52
|
|
Posted 26/07/12
|
#5
|
|
Dave,
Got dynamic file names to work, I guess passing pointer that were created through mem_alloc are the ones that fail. so the idea is to copy over the file name into an array that is size constant at compile time.
What kind of pointer does mem_alloc return, considering the pointer types availablei n 4dg they have to be word alligned or byte alligned, since the "default" seems to be word alligned, i expect that mem_alloc would return word aligned pointer, so this should have worked in the first place hmmm..
will kick around that piece of code in about a weeks time, when i am done with this project.
|
|
Loading...
|
|
meldavia
Registered: 18/03/07 Posts: 900
|
|
Posted 26/07/12
|
#6
|
|
Hi, mem_Alloc returns a pointer in the heap to an even aligned memory address. So, really it is a word array pointer, and if you need to use it for the string class, you need to 'double it' with the str_Ptr function.
__________________ Regards,
Dave
|
|
Loading...
|
|