/************************************************************************ * * * 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_wc_io.c */ /* This program establishes a file pointer, writes lines from */ /* a buffer to the file using wide character codes, moves the */ /* file pointer to the second record, copies the record to the */ /* wide character buffer, and then prints the buffer to the */ /* screen. */ #include #include #include main() { char flat_buffer[32]; wchar_t wide_buffer[32]; wchar_t format[32]; int i, pos; FILE *fptr; /* Set file pointer. */ fptr = fopen("data.dat", "w+"); if (fptr == NULL) { perror("fopen"); exit(EXIT_FAILURE); } for (i = 1; i < 5; i++) { if (i == 2) /* Get position of record 2. */ pos = ftell(fptr); /* Print a line to the buffer. */ sprintf(flat_buffer, "test data line %d\n", i); if (mbstowcs(wide_buffer, flat_buffer, 32) == -1) { perror("mbstowcs"); exit(EXIT_FAILURE); } /* Print buffer to the record. */ fputws(wide_buffer, fptr); } /* Go to record number 2. */ if (fseek(fptr, pos, 0) < 0) { perror("fseek"); /* Exit on fseek error. */ exit(EXIT_FAILURE); } /* Put record 2 in the buffer. */ if (fgetws(wide_buffer, 32, fptr) == NULL) { perror("fgetws"); /* Exit on fgets error. */ exit(EXIT_FAILURE); } /* Print the buffer. */ printf("Data in record 2 is: %S", wide_buffer); fclose(fptr); /* Close the file. */ }