#135737 - spinal_cord - Wed Jul 25, 2007 11:28 pm
I'm trying to find info on reading only part of a file, not the whole thing, and not from the beginning. So how would I read say a 10 byte chunck from around 30 bytes into a file, without having to read the first 30 bytes before it?
doesn't matter, fseek() was what i was looking for.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#135742 - Dwedit - Wed Jul 25, 2007 11:55 pm
Looks like you got your post edited just as I was going to post "Fseek is your friend"
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."
#135749 - spinal_cord - Thu Jul 26, 2007 12:18 am
although i seem not to be fread()ing properly...
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#135915 - spinal_cord - Fri Jul 27, 2007 12:53 pm
Can someone tell me what I'm doing wrong here?
Code: |
void readheader(const char *filename)
{
int offset=0x068; //0x068
FILE* testRead = fopen (filename, "rb"); //rb = read
u16 *filetext=0; // 32bit int? (4 bytes)
fseek(testRead,0x068,SEEK_SET);
fread(&filetext, sizeof(filetext), 2, testRead);
fclose(testRead);
PA_OutputText(0,1,1, "%d", &filetext);
}
|
filetext seems to always be 184564612.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#135917 - Diddl - Fri Jul 27, 2007 1:12 pm
first I'm not sure if mode "rb" works in this case.
then you use a pointer to NULL as a buffer for fread()?? you read in a pointer from this file?
next problem is you read in 2 items of length sizeof(filetext). you only have defined one item??
the output always prints the address of your variable (filetext). surely it is always same address ...
#135919 - spinal_cord - Fri Jul 27, 2007 1:31 pm
Code: |
void readheader(const char *filename)
{
FILE* testRead = fopen (filename, "r");
u32 filetext; // 32bit int? (4 bytes)
fseek(testRead,0x068,SEEK_SET);
fread(&filetext, sizeof(filetext), 1, testRead);
fclose(testRead);
PA_OutputText(0,1,1, "%d", filetext);
}
|
Is that better?
I have never tried to read only part of a file before.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#135942 - Miked0801 - Fri Jul 27, 2007 6:39 pm
You do need the 'rb' for a binary read. The default in text mode which is not what you want. Here's how I think it would work.
Code: |
void readheader(const char *filename)
{
FILE *testRead;
u32 fileInt;
if(fopen(filename, "rb") == NULL)
{
// File could not be opened, report error and exit
return;
}
fseek(testRead, 0x068, SEEK_SET)
fread(&fileInt, sizeof(filetext), 1, testRead);
fclose(testRead);
PA_OutputText(0,1,1, "%d", fileInt);
}
|
The above code should read 4 bytes from offset 0x68 bytes from the beginning of the file and place it in fileInt, close the file, then display the result. If your fopen fails (always needs to be checked), your passing in a garbage file string.
Also, I'd double check the file length to make sure I wasn't reading off the end of the file. ftell() will give you the current file position. Combine that with a seek_end (or whatever the heck the correct seek from back command is) to get that size and compare against 0x068.
#135943 - Lick - Fri Jul 27, 2007 6:54 pm
Miked, you don't seem to assign the handle returned by fopen().
Code: |
void readheader(const char *filename) {
u32 word = 0;
FILE *handle = fopen(filename, "rb");
if (handle) {
fseek(handle, 0x68, SEEK_SET);
fread(&word, sizeof(u32), 1, handle);
fclose(handle);
}
PA_OutputText(0,1,1, "%d", word);
} |
_________________
http://licklick.wordpress.com
#135949 - spinal_cord - Fri Jul 27, 2007 8:10 pm
Thanks guys. I'm never going to get the hang of C.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage
#135981 - sgeos - Sat Jul 28, 2007 5:42 am
Languages like perl and ruby are much better than C at text processing.
-Brendan
#136009 - tepples - Sat Jul 28, 2007 2:23 pm
sgeos wrote: |
Languages like perl and ruby are much better than C at text processing. |
You are correct that the standard libraries of languages such as Perl, Ruby, and Python have richer text processing than libc. But does Perl, Ruby, or Python run on a GBA or even a DS? Or are you talking about precompiling data files on a PC, turning some input format into .s to be assembled and linked into a C program?
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.
#136062 - Miked0801 - Sat Jul 28, 2007 8:39 pm
Doh - good catch. It's been a few years since I had to write fopen code. Good catch...
#136095 - sgeos - Sun Jul 29, 2007 1:35 am
Precompile if you can, muck around with C's lexical manipulation facilities if you must.
EDIT: regex++ looks like a nice library that can be used with C or C++.
-Brendan
#136130 - kusma - Sun Jul 29, 2007 3:19 pm
tepples wrote: |
But does Perl, Ruby, or Python run on a GBA or even a DS? |
Yes.
#136138 - dantheman - Sun Jul 29, 2007 4:53 pm
There's also a port of MicroPerl to DSLinux. I've used it in the past to run a Perl script that wrapped text files to the character width you specify for use with Moonshell (before it did word wrapping automatically).