home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / c / 16085 < prev    next >
Encoding:
Text File  |  1992-11-07  |  3.1 KB  |  115 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!stanford.edu!rock!concert!rti!jbs
  3. From: jbs@rti.rti.org
  4. Subject: Re: DEC VMS VERSION OF C - HOW TO ASSIGN FILE NAME?
  5. Message-ID: <1992Nov6.152550.20704@rti.rti.org>
  6. Organization: Research Triangle Institute, RTP, NC
  7. References: <92310.16240634R33O7@CMUVM.CSV.CMICH.EDU>
  8. Date: Fri, 6 Nov 92 15:25:50 GMT
  9. Lines: 104
  10.  
  11. In article <92310.16240634R33O7@CMUVM.CSV.CMICH.EDU> Robert A Rawlinson <34R33O7@CMUVM.CSV.CMICH.EDU> writes:
  12. >I HAVE A COUPLE OF STUDENTS DOING SOME C PROGRAMING FOR ME. I AM NOT A
  13. >GOOD C PROGRAMER SO I COULD NOT SEEM TO FIND THE ANSWER IN THE MANUALS.
  14. >IN COBOL I CAN CODE A FILE NAME AND THEN WHEN I RUN THE PROGRAM I CAN
  15. >ASSIGN A FILE NAME AND DIRECTORY. MY STUDENTS CODE IN C AND INDICATED THE
  16. >FILE AND DIRECTORY HAVE TO BE HARD CODED. I SUSPECT THERE IS A WAY TO
  17. >USE A NAME THAT I COULD ASSIGN A FILE AND DIRECTORY TO LATER AS IN COBOL.
  18. >COULD SOMEONE POINT ME IN THE CORRECT DIRECTION TO SOLVE THIS?
  19.  
  20. Your suspicion is correct. There are at least two approches to solving this
  21. problem.
  22.  
  23. First, you can use a logical name instead of an actual file name in the
  24. program, and then assign the logical name to point to a physical file before
  25. you execute the program:
  26.  
  27. /* contents of file "testopen.c" */
  28. #include <stdio.h>
  29. main()
  30. {
  31.     FILE *infile;
  32.     if ((infile = (fopen("file1","r")) == NULL)
  33.     {
  34.         printf("Couldn't open input file.\n");
  35.         exit();
  36.     }
  37.     else
  38.     {
  39.         printf("File opened successfully.\n");
  40.         close(infile);
  41.     }
  42. }
  43. /* end of file testopen.c */
  44.  
  45.  
  46. $ cc testopen
  47. $ link testopen
  48. $ assign mynode::disk1:[mydir]test.file;1 file1
  49. $! { or, alternatively, $ define file1 mynode::disk1:[mydir]test.file;1 }
  50. $ run testopen
  51. File opened successfully.
  52.  
  53. $ deassign file1
  54.  
  55.  
  56. -----------------------------------------------------------------------
  57. Approach #2: use command-line arguments and feed in the filenames in the
  58. program invocation:
  59.  
  60. /* Contents of testopen.c */
  61. #include <stdio.h>
  62. main(argc,argv)
  63. int argc;    /* Number of arguments passed to program, including program
  64.                 name */
  65. char *argv[];    /* Array of pointers to arguments */
  66. {
  67.     FILE *infile;
  68.     int i;
  69.  
  70.     printf("Number of arguments passed to testopen was %d.\n",argc);
  71.     printf("Argument list follows:\n\n:");
  72.     for (i=0; i < argc; i++)
  73.         printf("%s\n",argv[i]);
  74.     printf("\n");
  75.  
  76.     if ((infile = fopen(argv[1],"r") == NULL)
  77.     {
  78.         printf("Can't open file %s for input.\n",argv[1]);
  79.         exit();
  80.     }
  81.     else {
  82.         printf("Succesfully opened %s for input.\n",argv[1])
  83.         close(infile);
  84.     }
  85. }
  86. /* end of file testopen.c */
  87.  
  88.  
  89. $ cc testopen
  90. $ link testopen
  91. $! { next line creates a global symbol that allows you to execute the
  92. $!   program with arguments }
  93. $ testopen == "$disk1:[mydir]testopen.exe"
  94. $ testopen mynode::disk1:[mydir]test.file "dummy argument here" and three more
  95. Number of arguments passed to testopen was 6.
  96. Argument list follows:
  97.  
  98. DISK1:[MYDIR]TESTOPEN.EXE
  99. MYNODE::DISK1:[MYDIR]TEST.FILE
  100. dummy argument here
  101. AND
  102. THREE
  103. MORE
  104.  
  105. Successfully opened TEST.FILE for input.
  106.  
  107. $ delete/symbol/global testopen
  108.  
  109. ----------------------------------------------------------------------
  110.  
  111. Hope this helps.  Hope it's correct, as well. I didn't test any of it ;-)
  112. Corrections are welcome.
  113.  
  114.   -joe
  115.