PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Why is this an error?

Ok, here is the code...
Code:
FILE *fp; // file pointer to the configuration file

fp = fopen(nf, "rt");
fseek(fp, 0, SEEK_END);
config_size = ftell(fp);


ok, it is the ftell(fp) that returns the error (i.e. -1). Now, I know that the file opens, since in the full source I check for a null pointer (and visually checked it during dubug. Also I know that the file is not empty, (it is 204 bytes large and is valid when viewd on my desktop. So, any ideas?
through further investigation, I feel as though the file may be opening as the wrong type. Is this a possibility?
BurningSheep · 03 Jun 2002, 23:45 UTC

The file positions reported by fgetpos() or ftell() may be incorrect for files that are open in text mode in the case that the file is being read, or the case where the file is being written to and there is buffered data which has not yet been written to the underlying file. In the case that the file is being written to, fflush() can be used prior to calling fgetpos() or ftell() to ensure the accuracy of fgetpos() or ftell(). Text mode files with no buffering do not have this problem, however performance can be significantly degraded with buffering turned off.
fzammetti · 03 Jun 2002, 23:46 UTC — Suggestion
If successful, the ftell() function returns the current file position for the stream, measured in bytes from the beginning of the file. On failure, it returns -1 and and errno is set to one of the following values:

EBADF
The file descriptor underlying stream is not valid.

ESPIPE
The file descriptor underlying stream is associated with a pipe or FIFO.

Use ferror to get the error code. In addition, you might try fopen with type "rb" instead. The example I found does that:

/* ftell example : getting size of a file */
#include <stdio.h>

int main ()
{
FILE * pFile;
long size;

pFile = fopen ("myfile.txt","rb");
if (pFile==NULL) perror ("Error opening file");
else
{
fseek (pFile, 0, SEEK_END);
size=ftell (pFile);
fclose (pFile);
printf ("Size of myfile.txt: %ld bytes.\n",size);
}
return 0;
}
Yeah, you need to open the file in binary mode.

Dan East
it's always something stupid like that. I wonder how the original authers got away with opening it in text mode? Stupid windows ce and it's strict programming rules.

BTW, thanks for your help. There is always something new to learn, and thank God for such great teachers.
ok, no good. I modified it slightly, and here is what we have
Code:
FILE *fp; // file pointer to the configuration file

fp = fopen("/FPSE/FPSE.INI", "rb");
if (fp == NULL) {
printf("INI_Load(): Error reading configuration file \"%s\"\n", nf);
return FPSE_ERR;
}

fseek(fp, 0, SEEK_END);
config_size = ftell(fp);


now, congig_size is declared as a global long. I changed the way it got the file name from a var to hard coded just to ensure that it was using the correct file. any more sugestions?
ok, I called ferror right after ftell and I got 32 as the error code, but I can't find a refrence to it anywhere.
ok, to test some things out, I changed the seek command into this
Code:
while (!feof( fp ))
{
fread( list, sizeof( char ), 1, fp );
}


And, it was a never ending loop. In fact, it is still going on as we speek. What could be the problem? any sugestions?
First off, I would check the return code of fseek to make sure it isn't failing first. ferror returns the current error code, even if it wasn't set in the last function call (the only things that clear the error code is clearerr, fclose and the unsupported rewind function). The Windows system error number 32 is "The process cannot access the file because it is being used by another process." I don't know if that corresponds to the errors as returned by ferror or not. Whatever creates fpse.ini is not writing to it while you're trying to read, is it?

Also, remember that it is possible for the filesystem on your Pocket PC / Storage Card to become corrupted. It is just a standard FAT filesystem. If the file is corrupted errors could certainly be encountered when trying to read it. Try pulling it up in Pocket Word or copying it over to your PC.

Dan East
Ok, I have tried copying and pasting the info into a new file, and I am now using a *.txt (fpse.txt) in the rare case the windows is saving the ini in a wierd format. I am now opening it as rb, as sugested, but it is still giving me the error #32 (which I am checking directly after I call fseek). I just tried changing it to rt (just to check once more), but still no good. I have also tried moving the file (fpse.txt) to other locations, and still no good. I am beginning to wonder if it is a problem with the os maybe? Or even with pocketpc2002 sdk. I may just be delerious from mowing the grass though. Man is it hot out there.
Ok, as I pound a book into my head, I think I fixed the problem (knock on wood). It appears as if another file included in the program defined fopen, fseek,... with it's own set of comands. Now, I have fixed that, and here is hoping that it will work :arrow:
Yep, that was it. I wasted a day of programing because a simple fopen was declared incorrectly. Wow, am I pissed right now.
Usually I'm glad after such an event...glad that I didn't waste an other day...
Good call
it's always something stupid like that. I wonder how the original authers got away with opening it in text mode? Stupid windows ce and it's strict programming rules.


If you are reading it as text (e.g. fscanf), you need "rt". Especially if Windows CE uses CR LFs (which I'm guessing it does?)
If you are using fread, you can get away with binary "rb";

Actually why is the ftell failing on Windows CE when it worked okay in FPSE?

Maybe the best thing to do would be open is as binary (rb) get the file size, then reopen in text (rt) before you start reading as text.

What is the file size used for?
Sorry dave, I already fixed the problem.
I wasted around 20 minutes yesterday. I created a new function in a straight C app and kept having linker errors because the implementation for my new function could not be found. Well, I found my function protype name did not match the implementation name (dan_playSound vs danPlaySound). Duh!

Dan East