Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!nbires!hao!gatech!rutgers!labrea!jade!ucbvax!KL.SRI.COM!STEINBERGER From: STEINBERGER@KL.SRI.COM (Richard Steinberger) Newsgroups: comp.os.vms Subject: 2 C questions Message-ID: <12334342671.18.STEINBERGER@KL.SRI.Com> Date: Sun, 13-Sep-87 14:47:54 EDT Article-I.D.: KL.12334342671.18.STEINBERGER Posted: Sun Sep 13 14:47:54 1987 Date-Received: Mon, 14-Sep-87 03:51:51 EDT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The ARPA Internet Lines: 49 I have 2 questions concerning VAX C. Thanks in advance to anyone who can help. Question 1: I wrote a short main routine (see below) to test a function. One of the inputs is an integer number, and the next input is a filename. The problem is that the scanf that reads the number apparently leaves a LF character in a buffer that is then read by the code that is expecting the filename. Because it's a LF, I never get a chance to enter a filename (see code). My "kludgy" solution was to put the line "i = getchar()" after the scanf to remove the LF character; when this is done the following lines that get a filename work fine. Am I missing something fundamental? I've tried using gets after the scanf and the result is the same, i.e. unless the "i = getchar()" is there to remove the LF, gets doesn't "work" either. Why does scanf leave a LF character, or am I misinterpreting what's going on? Are there solutions other than using a getchar() call after a scanf that preceeds a gets or getchar call? The relevant code section is below: printf("\nEnter a string size: "); scanf("%d",&n_chars); /* clear NEWLINE char left after scanf */ i = getchar(); /* WHY DO I NEED THIS ? */ printf("\nEnter a file to use: "); for (i=0; (input_file[i] = getchar()) != '\n'; i++); input_file[i] = '\0'; ______________________________________________________________________________ Question 2: I am trying to read and write binary files similar to ones created by FORTRAN open statements (that produce sequential, unformatted, fixed recordsize files). Specifying "rb" in an fopen statement seems to be enough to access existing files (using an fread call). If I want to create a new binary file with fixed-length records, which if any, of the keywords on p 4-5 of the C RTL Ref Man are appropriate? Is "rfm=fix" enough? Do I need "mrs=size" as well, and if so, is size in units of bytes, or longwords (like Fortran)? Also, is there any reason to use the chapter 4 open and read or write functions instead of the chapter 2 functions (fopen, fread and fwrite)? ______________________________________________________________________________ Ric Steinberger steinberger@kl.sri.com -------