home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!stanford.edu!rock!concert!rti!jbs
- From: jbs@rti.rti.org
- Subject: Re: DEC VMS VERSION OF C - HOW TO ASSIGN FILE NAME?
- Message-ID: <1992Nov6.152550.20704@rti.rti.org>
- Organization: Research Triangle Institute, RTP, NC
- References: <92310.16240634R33O7@CMUVM.CSV.CMICH.EDU>
- Date: Fri, 6 Nov 92 15:25:50 GMT
- Lines: 104
-
- In article <92310.16240634R33O7@CMUVM.CSV.CMICH.EDU> Robert A Rawlinson <34R33O7@CMUVM.CSV.CMICH.EDU> writes:
- >I HAVE A COUPLE OF STUDENTS DOING SOME C PROGRAMING FOR ME. I AM NOT A
- >GOOD C PROGRAMER SO I COULD NOT SEEM TO FIND THE ANSWER IN THE MANUALS.
- >IN COBOL I CAN CODE A FILE NAME AND THEN WHEN I RUN THE PROGRAM I CAN
- >ASSIGN A FILE NAME AND DIRECTORY. MY STUDENTS CODE IN C AND INDICATED THE
- >FILE AND DIRECTORY HAVE TO BE HARD CODED. I SUSPECT THERE IS A WAY TO
- >USE A NAME THAT I COULD ASSIGN A FILE AND DIRECTORY TO LATER AS IN COBOL.
- >COULD SOMEONE POINT ME IN THE CORRECT DIRECTION TO SOLVE THIS?
-
- Your suspicion is correct. There are at least two approches to solving this
- problem.
-
- First, you can use a logical name instead of an actual file name in the
- program, and then assign the logical name to point to a physical file before
- you execute the program:
-
- /* contents of file "testopen.c" */
- #include <stdio.h>
- main()
- {
- FILE *infile;
- if ((infile = (fopen("file1","r")) == NULL)
- {
- printf("Couldn't open input file.\n");
- exit();
- }
- else
- {
- printf("File opened successfully.\n");
- close(infile);
- }
- }
- /* end of file testopen.c */
-
-
- $ cc testopen
- $ link testopen
- $ assign mynode::disk1:[mydir]test.file;1 file1
- $! { or, alternatively, $ define file1 mynode::disk1:[mydir]test.file;1 }
- $ run testopen
- File opened successfully.
-
- $ deassign file1
-
-
- -----------------------------------------------------------------------
- Approach #2: use command-line arguments and feed in the filenames in the
- program invocation:
-
- /* Contents of testopen.c */
- #include <stdio.h>
- main(argc,argv)
- int argc; /* Number of arguments passed to program, including program
- name */
- char *argv[]; /* Array of pointers to arguments */
- {
- FILE *infile;
- int i;
-
- printf("Number of arguments passed to testopen was %d.\n",argc);
- printf("Argument list follows:\n\n:");
- for (i=0; i < argc; i++)
- printf("%s\n",argv[i]);
- printf("\n");
-
- if ((infile = fopen(argv[1],"r") == NULL)
- {
- printf("Can't open file %s for input.\n",argv[1]);
- exit();
- }
- else {
- printf("Succesfully opened %s for input.\n",argv[1])
- close(infile);
- }
- }
- /* end of file testopen.c */
-
-
- $ cc testopen
- $ link testopen
- $! { next line creates a global symbol that allows you to execute the
- $! program with arguments }
- $ testopen == "$disk1:[mydir]testopen.exe"
- $ testopen mynode::disk1:[mydir]test.file "dummy argument here" and three more
- Number of arguments passed to testopen was 6.
- Argument list follows:
-
- DISK1:[MYDIR]TESTOPEN.EXE
- MYNODE::DISK1:[MYDIR]TEST.FILE
- dummy argument here
- AND
- THREE
- MORE
-
- Successfully opened TEST.FILE for input.
-
- $ delete/symbol/global testopen
-
- ----------------------------------------------------------------------
-
- Hope this helps. Hope it's correct, as well. I didn't test any of it ;-)
- Corrections are welcome.
-
- -joe
-