I am trying to open a file for appending. ie. if the file exists, open it for appending, if not, create it. I am getting back a NULL pointer, and do not know why. Here is my code excerpt. It is a C file.
FILE *pFile;
char *cFileName;
cFileName=argv[++i];
pFile = fopen(cFileName,"a");
cFileName has a valid filename ('apm.dbg'), and I have full write permissions in the directory. I can step into fopen and when the file is opened, I can see in another window that the file is created with zero bytes. However pFile is returned as NULL.
GetLastError() returns:
- when the file does not exist:
ERROR_NEGATIVE_SEEK // An attempt was made to move a file pointer before the beginning of the file.
- when the file does exist:
ERROR_ALREADY_EXISTS // The system cannot create a file when it already exists.
Seems strange to me since "a" means append if it exists, create it if it doesn't.
Any ideas
Adrian

File open errors returned when trying to open a file for appending
Harrie Nak
My best guess here is that the Fortran code's close-file is not immediately closing the file. Is there anyway to force-close the file from fortran If so, I'd look into that as the next alternative.
TMF
#include <windows.h>
#include <stdlib.h>
#include <math.h>
char *cFileName; /* file to dump output */
FILE *pFile; /* file pointer */
Parameters(int argc, char *argv[])
{
register int i;
int iii;
for(i=1; i<argc; i++)
, "-file")==0) {
if(strcmp(argv
cFileName=argv[++i];
pFile = fopen(cFileName,"a"); // open debug file
iii = GetLastError();
}
}
nstorandt
I created a test app using that code. Unfortunately, I could not reproduce your problem.
I don't see you check for NULL - so is this your actual code Or a quasi version
ocertain
Douglas Lellis
This is a cut down version of the code. But something else I just checked: This code is actually being called from Fortran, and the file is created in Fortran first. I close it in Fortran before I make the C call. If I don't open/close it in Fortran first, it works fine in C - it appears to be something to do with the fact that the Fortran code opens and closes closes this file first.
Adrian
Ian Roof - MSFT
No, I delete the file before I run the program. I can see in Explorer that the file gets created by this program. But the fopen returns the ERROR_NEGATIVE_SEEK error.
If I do it again without deleting the file, I get the ERROR_ALREADY_EXISTS error.
Adrian
XLN Lasse
the lightbulb above should be:
argv [ i ]
Daphne.cheng
If your fortran app is not properly closing the file, I can't help you there, but you might try checking errno to get the specific io error you're encountering.
Ben
Bill_H
Could you post the entire function where you do this please It looks like something else is being done in your code that results in this odd behavior.
Erik Hanson
Actually, this whole thing is a VS.NET debugger aberration. The fopen apparently returns a NULL pointer (and GetLastError() also is non-zero), however when I execute fprintf on the pointer, the file pointer becomes non-zero, and I write quite happily to the file.
Adrian