LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Run-Time Library Errors when Writing/Closing a text file.

Using OpenFile() to get a file handle. When using WriteFile() or CloseFile() I keep getting a Run-Time Error (value = -1 [0xffffffff]), BAd File Handle. I also tried Ansi C routines fopen(),fwrite(), fclose() with similiar results.
0 Kudos
Message 1 of 5
(3,592 Views)
Can you send a snippet of your code that performs these actions.

Regards

Chris
0 Kudos
Message 2 of 5
(3,592 Views)
Hi BPoyner,

Are you sure that your file path in OpenFile or fopen is correct, as in you are specifying a path that exists and with the proper escape characters for backslashes (e.g. "C:\\Windows\\myfile.txt")? Are you using the access modes correctly and are you sure that the OS file system permissions are set correctly for the particular file/directory you are writing too?

Here is an example you can try that just creates a new file with fopen, writes to it with fprintf, closes it with fclose, reopens it with OpenFile, reads it with ScanFile, and closes it again with CloseFile that you can use to assure yourself that the functions work properly. I have tested it and it works fine. (note: the only reason I mixed all of these function calls is so that you can
see the different families of functions work) Just give this code a try and let us know how it turns out.

#include
#include
#include

static int hmyfile2;
static FILE *hmyfile;

int main (int argc, char *argv[])
{
int numitemsformatted;
char buffer[100];
int status;
int numchars;

fopen ("myfile.txt", "w+");
hmyfile = fopen ("myfile.txt", "w+");
numchars = fprintf (hmyfile, "%s", "Hello World!");
status = fclose (hmyfile);

hmyfile2 = OpenFile ("myfile.txt", VAL_READ_ONLY, VAL_OPEN_AS_IS,
VAL_ASCII);
numitemsformatted = ScanFile (hmyfile2, "%s>%s[t0]", buffer);
status = CloseFile (hmyfile2);

numitemsformatted = FmtOut ("%s", buffer);
getchar();

return 0;
}

Jason F.
Applications Engineer
National Instruments
www.ni.com/ask
0 Kudos
Message 3 of 5
(3,592 Views)
In your example code, you use either one method (library) or the other to get either a fileptr or a file handle. What if you want to have the option, once it's opened, to use either fwrite or WriteFile? You will need to know both the file ptr and its handle. For instance, in your example, why can't you use hmyfile->handle in a WriteFile() function instead of fprintf (it gives a BadFileHandle error)?

I found that there's really only one way to get both a file ptr and handle and not get runtime errors. Namely, I need to open using OpenFile() to get my file handle, and then a fdopen() to get the fileptr. This works fine. I wonder, though, why I can't just do an fopen() to get the fileptr, and then use the fileptr->handle in ReadFile() and WriteFil
e() functions. It gives me runtime errors. Is there another function that I can get the correct handle and file ptr?

This is of interest to me since I recently wanted to use the CreateAndOpenTemporaryFile() function, which gives me a fileptr, and then write to it using WriteFile() and the fileptr->handle. This gave a BadFileHandle error.

Thanks.
0 Kudos
Message 4 of 5
(3,592 Views)
A couple of things here...
When you open the file, what mode are you opening it in? For example, if you open it as a read only file, and subsequently try and write to it, then you will get a -1 error (not necessarily a bad file handle...)
Similarly, are you sure that the openfile completes succesfully?
If you get a -1 return from the write & close function, you need to use the GetFmtIOError() function to find out exactly what the error was... This may go a long way to helping you figure out what the problem is
Another posible one is that you are opening the file somewhere, and trying to access the file from another function with a file handle, that while it appears valid, is not the correct one for the file... it's worth tracing the code through to ensure that th
e same file handle as was obtained by the openfile is being used at the writefile and closefile calls...
One final suggestion... make sure that nothing else is using the file that you ar trying to write to... Some programs, like notepad are quite happy about this, but others, like excel lock the file for use by anything else, which would produce errors when tring to access it..

Cheers
JB
Message 5 of 5
(3,592 Views)