![]() |
![]() HP OpenVMS Systemsask the wizard |
![]() |
The Question is: Dear Wizard, I am porting a UNIX program for scanning logfiles, and have run into a few snags. The first I have been told is impossible (opening a file and fseek'ing back and forth and using fgets() to read the data) because of limitations in the C RTL. This I have worked around. But now that the program is completed and I am testing on "real" logfiles I find that I am unable to access these. The fopen() function (which opens the file read-only ) returns with a n error message indicating that the file is locked by another process. Is there no way of opening a file for reading from standard C (that is non-RMS functions) if another process is using that file? I have attempted to set flags for sharing (shr=get looked most promising) but am getting nowhere. If this is not possible without using RMS functions please point me to some sample code, as I have never used these functions before and am more than a littl e intimidated by the apparent amount of code needed to do seemingly simple things ;-) Thank you in advance for your sage advice! PS: If it is relevant I am using DecC v6 The Answer is : The fopen option: "shr=get,upd,put" or similar is commonly used, in conjunction with periodic calls to the routines: fsync(fileno(fp)) and fflush(fp) Note: the more calls to fsync and fflush, the slower things get. See the documentation on the creat call for details, as well as the RMS documentation. One example would be: fopen(file,"w","shr=get","ctx=rec","fop=dfw"); with readers opening the file with "shr=get". A full example is attached below. Also please see "C stdout and log files? message logging?" in topic (2078) here in the Ask The Wizard area. Also see the RMS_EXAMPLES.C module found in SRH_EXAMPLES directory on the OpenVMS Freeware V4.0 and V5.0 distributions -- this example shows native RMS calls and the FAB and RAB structures in the context of a C application program. -- #include <signal.h> #include <ssdef.h> #include <stdio.h> #define LOOPMAX 50 #define LOOPINT 3 main() { FILE *fp; char *fn = "sys$scratch:tmp.tmp"; int i; printf( "Opening scratch file %s for shared access...\n", fn ); printf( "This application writes to the shared file...\n"); fp = fopen( fn, "w", "shr=get", "rat=cr", "rfm=var", "ctx=rec" ); for ( i = 0; i < LOOPMAX; i++ ) { printf( "Iteration count: %d\n", i ); fprintf( fp, "Iteration count: %d\n", i ); fsync( fileno( fp )); sleep( LOOPINT ); } printf( "Done.\n" ); printf( "Please delete scratch file %s ...\n", fn ); return SS$_NORMAL; } -- #include <signal.h> #include <ssdef.h> #include <stdio.h> #define LOOPMAX 50 #define TEXTMAX 100 #define LOOPINT 3 main() { FILE *fp; char *fn = "sys$scratch:tmp.tmp"; char txt[TEXTMAX]; int i; printf( "Opening scratch file %s for shared access...\n", fn ); printf( "This application reads from the shared file...\n"); fp = fopen( fn, "r", "shr=get,put,upd", "rat=cr", "rfm=var", "ctx=rec" ); for ( i = 0; i < LOOPMAX; i++ ) { fgets( txt, TEXTMAX, fp ); printf( "Read: <%s>\n", txt ); sleep( LOOPINT ); } printf( "Done.\n" ); return SS$_NORMAL; }
|