Author Topic: FileSeek() + FileRead() questions  (Read 9363 times)

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
FileSeek() + FileRead() questions
« on: July 09, 2012, 10:26:50 pm »
Hello again, I'm still wrapping my head around these character arrays but I have a question about these two file functions.

FileSeek() - this takes two arguments, the file handle and seek. The file handle is the same handle that is used for FileOpen but how is "seek" defined? Is seek defined as an index within the file? If I had a dictionary program with the words saved in a .txt file, how do I use "seek" to find a word (I use \n ) ?


FileRead() - this one takes three arguments. The handle makes sense but I have questions about the character array and length. When FileRead() is called, it starts at the index (seek? - see above) and will continue to read character by character as defined by len? After the data has been copied, it is transferred to a character array as defined as the second argument of the function?



Can anyone who knows about these functions share some light on how they actually work :/

nednones

  • Guest
Re: FileSeek() + FileRead() questions
« Reply #1 on: July 09, 2012, 11:15:44 pm »
Your FileSeek simply places the caret within the file to your liking, e.g. If you have a text file with 5 lines each line being a string say, then by using FileSeek(Filehandle,2) then you can read the 3rd line in that file or write to it at that position.

The fileread() method can be used such that you read the file continuously until you've hit the end if the file defined by the length of the file, e.g. In a while loop whilst file is not greater than length. You can also set up the fileread so that you can loop through and read line by line similar to the C++ .eof() method.

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: FileSeek() + FileRead() questions
« Reply #2 on: July 10, 2012, 08:12:47 am »
FileSeek() sets the current position in the file.  The position is the byte index where you want to perform the next operation (read or write).  Thus:

Code: [Select]
FileSeek(fh,5);
Will set the position in the file to the 6th character:

Code: [Select]
0123456789
     ^

FileRead() will read from the file at the current position into a buffer that you specify the number of bytes you specify.  Its return value is the number of bytes read:

Code: [Select]
FileRead(fh,MyBuffer,10);
Will read 10 bytes from fh into the char array named "MyBuffer".

Hope this helps!
Tim Sommers
DragonFireSDK Team

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Re: FileSeek() + FileRead() questions
« Reply #3 on: July 10, 2012, 10:04:54 am »
If I were to manipulate the file such as adding and subtracting data from it's contents... (my file will be hundred's of strings), is there a method to FileSeek() the end of file, add a new line, and begin writing from there? 

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: FileSeek() + FileRead() questions
« Reply #4 on: July 10, 2012, 12:17:04 pm »
The FileLength() function returns the size of the file in bytes and seeks to the end of the file.

Try calling FileLength() before you want to append stuff to your file...
Tim Sommers
DragonFireSDK Team

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Re: FileSeek() + FileRead() questions
« Reply #5 on: July 10, 2012, 01:38:20 pm »
The FileLength() function returns the size of the file in bytes and seeks to the end of the file.

Try calling FileLength() before you want to append stuff to your file...

Thank you tim!

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Re: FileSeek() + FileRead() questions
« Reply #6 on: July 23, 2012, 04:52:32 pm »
Another quick related question while I am here, how can I properly delete a file? I cannot seem to do this within the function of a touch press on my app...

Code: [Select]
.....


FileClose(savefile);
//FileOpen((char *)"save.txt");
FileDelete((char *)tempString.c_str() );



//recreates save file
if( savefile=FileOpen((char *)"save.txt") == 0) //save.txt file does not exist
{
//Creates file save
savefile = FileCreate((char *)"save.txt");

}
else
{
//
}

......

Does anyone see something wrong with my above code? I debug it and it skips over the creation of the file....

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: FileSeek() + FileRead() questions
« Reply #7 on: July 24, 2012, 08:03:42 am »
Try assigning the result of FileDelete() to an int variable.  Check its result.  If the file was deleted the variable will be zero, otherwise non-zero.
Tim Sommers
DragonFireSDK Team

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Re: FileSeek() + FileRead() questions
« Reply #8 on: July 24, 2012, 08:40:56 am »
Try assigning the result of FileDelete() to an int variable.  Check its result.  If the file was deleted the variable will be zero, otherwise non-zero.

Turns out to equal -1, I don't know if this is a bug or I'm just programming wrong...

Code: [Select]
char TextBuffer[10000];

...


int OnDelete(int id, int event, int x, int y)
{
if( event == 3 )
{
int savefile;
int num = 10;
long int end;
vector < string > tempV;

savefile=FileOpen((char *)"save.txt"); //I open the file named "save.txt"
end = FileLength(savefile); //This stores the length of the bytes within the file

FileSeek(savefile,0); //Set position at 0 of file
FileRead(savefile, TextBuffer, end ); //Read all the bytes in the file

string oops(TextBuffer); //Creates a string from the array of characters

for( int i = 0; i < end; i++) //From the beginning to the end of the file
{
              //blah blah blah put stuff into vectors
}


FileClose(savefile); //Close "save.txt"
num = FileDelete((char *)"save.txt"); //THIS RETURNS -1 ???


              }

return 0;
}

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: FileSeek() + FileRead() questions
« Reply #9 on: July 24, 2012, 09:29:16 am »
So, the non-zero result means that the file couldn't be deleted for some reason.

If you're on Windows, does some other process have the file open when you try to delete it?  Has your app for sure closed the file before you try to delete?
Tim Sommers
DragonFireSDK Team

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Re: FileSeek() + FileRead() questions
« Reply #10 on: July 25, 2012, 03:48:38 pm »
So, the non-zero result means that the file couldn't be deleted for some reason.

If you're on Windows, does some other process have the file open when you try to delete it?  Has your app for sure closed the file before you try to delete?

Nope, I checked my code. I have a total of 7 FileOpen commands and at least 7 FileClose that all follow the commands. The file is never deleted when my app is ran. Possible bug? Maybe

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: FileSeek() + FileRead() questions
« Reply #11 on: July 26, 2012, 01:31:46 pm »
Can you send me a code sample that reproduces the issue?

Thanks!
Tim Sommers
DragonFireSDK Team

zacho

  • Jr. Member
  • **
  • Posts: 81
  • Karma: +2/-0
    • View Profile
Re: FileSeek() + FileRead() questions
« Reply #12 on: July 26, 2012, 09:56:30 pm »
Can you send me a code sample that reproduces the issue?

Thanks!

I'll definitely find a piece of code (hopefully by tomorrow)

octopus

  • Guest
Re: FileSeek() + FileRead() questions
« Reply #13 on: July 28, 2012, 11:09:46 pm »
Is there a way to read a text file line by line. Eg. Read line 1, then read line 6 etc. ?

tim

  • Administrator
  • Hero Member
  • *****
  • Posts: 1595
  • Karma: +11/-1
    • View Profile
    • DragonFireSDK
Re: FileSeek() + FileRead() questions
« Reply #14 on: July 30, 2012, 08:06:35 am »
There's nothing built in, but you could write your own function to do this...  Maybe using the standard C strtok() function to break the string on newline (\n) characters...
Tim Sommers
DragonFireSDK Team