/************************************************************************ * * * Copyright Digital Equipment Corporation 1998. All rights reserved. * * * * Restricted Rights: Use, duplication, or disclosure by the U.S. * * Government is subject to restrictions as set forth in subparagraph * * (c) (1) (ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR * * 52.227-14 Alt. III, as applicable. * * * * This software is proprietary to and embodies the confidential * * technology of Digital Equipment Corporation. Possession, use, or * * copying of this software and media is authorized only pursuant to a * * valid written license from Digital or an authorized sublicensor. * * * ************************************************************************/ /* chap_2_file_dis_and_pointer.c */ /* The following example creates a file with variable-length */ /* records (rfm=var) and the carriage-return attribute (rat=cr).*/ /* */ /* The program uses creat to create and open the file, and */ /* fdopen to associate the file descriptor with a file pointer. */ /* After using the fdopen function, the file must be referenced */ /* using the Standard I/O functions. */ #include #include #include #include #define ERROR 0 #define ERROR1 -1 #define BUFFSIZE 132 main() { char buffer[BUFFSIZE]; int fildes; FILE *fp; if ((fildes = creat("data.dat", 0, "rat=cr", "rfm=var")) == ERROR1) { perror("DATA.DAT: creat() failed\n"); exit(EXIT_FAILURE); } if ((fp = fdopen(fildes, "w")) == NULL) { perror("DATA.DAT: fdopen() failed\n"); exit(EXIT_FAILURE); } while (fgets(buffer, BUFFSIZE, stdin) != NULL) if (fwrite(buffer, strlen(buffer), 1, fp) == ERROR) { perror("DATA.DAT: fwrite() failed\n"); exit(EXIT_FAILURE); } if (fclose(fp) == EOF) { perror("DATA.DAT: fclose() failed\n"); exit(EXIT_FAILURE); } }