Hardware Reference
In-Depth Information
To change the name of a file called HELLO.DOC to GOODBYE.DOC the
command would be:
RNAME HELLO.DOC GOODBYE.DOC
Disk files
File handling is quite straightforward using streams in C. Files must be opened
before they can be used using a statement of the form:
stream = fopen(filename, mode);
The filename can be a file specification or the name of a logical device. The
mode can be ' r ' for read, ' w ' for write, and ' u ' for update. If the file cannot be
opened (e.g. it is not present on the disk) fopen() returns 0 otherwise fopen
returns the stream number to be used in conjunction with subsequent read or
write operations.
After use, files must be closed using statement of the form:
fclose(stream);
where stream is the channel number returned by a previous fopen statement.
As a further example of file handling in C, the following Borland C ++ 4.5
program converts the case of an ASCII file to upper case:
/* Name:
ucase.c
*/
/* Language: Borland C++ 4.5
*/
/* Output:
Menu routine
*/
/* Note:
Program runs in a DOS console window
*/
#include<stdio.h>
#include<io.h>
FILE *fp, *fq;
int pc;
int main(argc, argv)
int argc;
char *argv[];
{
printf("CONVERTING FILE TO UPPER CASE n");
fp = fopen(argv[1], "r");
if (fp == 0)
{
printf("Unable to open input file: %s \n" , argv[1]);
return(0);
}
fq = fopen(argv[2], "w");
if (fq == 0)
{
printf("Unable to create output file: %s n", argv[2]);
return(0);
}
printf("\nInput file: %s \n", argv[1]);
printf("\nOutput file: %s \n", argv[2]);
while((pc = getc(fp)) != EOF)
{
pc = toupper(pc);
putc(pc,fq);
Search WWH ::




Custom Search