⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap11.lst

📁 Borland C++ Builder The Complete Reference 例程源代码
💻 LST
📖 第 1 页 / 共 2 页
字号:
listing 1
#include <stdio.h> 
#include <io.h> 
 
int main(void) 
{ 
  if(!access("TEST.TST", 0)) 
    printf("File Present"); 
  else 
    printf("File not Found"); 
 
  return 0; 
}

listing 2
if(_rtl_chmod("TEST.TST", 1, FA_RDONLY)==FA_RDONLY) 
  printf("File set to read-only mode.");

listing 3
if(!chmod("TEST.TST", S_IREAD | S_IWRITE)) 
  printf("File set to read/write access.");

listing 4
/* 
  Assume that a file associated with handle 
  has been opened. 
*/ 
 
if(!chsize(handle, 256)) 
  printf("File size is now 256 bytes.");

listing 5
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[])  /* copy one file to another */ 
{ 
  FILE *in, *out; 
  char ch; 
 
  if(argc!=3) { 
    printf("You forgot to enter a filename\n"); 
    exit(0); 
  } 
 
  if((in=fopen(argv[1], "rb")) == NULL) { 
    printf("Cannot open file.\n"); 
    exit(0); 
  } 
  if((out=fopen(argv[2],"wb")) == NULL) { 
    printf("Cannot open file.\n"); 
    exit(0); 
  } 
 
  while(!feof(in)) { 
    ch = getc(in); 
    if(ferror(in)) { 
      printf("Read Error"); 
      clearerr(in); 
    } else { 
      if(!feof(in)) putc(ch, out); 
      if(ferror(out)) { 
        printf("Write Error"); 
        clearerr(out); 
      } 
    } 
  } 
  fclose(in); 
  fclose(out); 
 
  return 0; 
}

listing 6
#include <stdio.h> 
#include <fcntl.h> 
#include <sys\stat.h> 
#include <io.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  int fd; 
 
  if((fd=open(argv[1], O_RDONLY))==-1) { 
    printf("Cannot open file."); 
    exit(1); 
  } 
 
  printf("File is existent.\n"); 
 
  if(close(fd)) 
    printf("Error in closing file.\n"); 
 
  return 0; 
}

listing 7
#include <stdio.h> 
#include <sys\stat.h> 
#include <io.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  int fd; 
 
  if((fd=_creat("test", S_IWRITE))==-1) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
/* ... */ 
 
  close(fd);  /* close the file */ 
 
  return 0; 
}

listing 8
FILE *fp, *fp2; 
/* ... */ 
fp2 = dup(fp);

listing 9
#include <stdio.h> 
#include <io.h> 
#include <fcntl.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  int fd; 
  char ch; 
 
  if((fd=open(argv[1], O_RDWR))==-1) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  while(!eof(fd)) { 
    read(fd, &ch, 1);  /* read one char at a time */ 
    printf("%c", ch); 
  } 
 
  close(fd); 
 
  return 0; 
}

listing 10
#include <stdio.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  FILE *fp; 
 
  if((fp=fopen("test", "rb"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
/* ... */ 
 
  if(fclose(fp)) 
    printf("File close error.\n"); 
 
  return 0; 
}

listing 11
/* 
  Assume that fp has been opened as a binary file 
  for read operations. 
*/ 
while(!feof(fp)) getc(fp);

listing 12
/* 
  Assume that fp points to a stream opened for write 
  operations. 
*/ 
 
while(!done) { 
  putc(info,fp); 
  if(ferror(fp)) { 
    printf("File Error\n"); 
    exit(1); 
  } 
/* ... */ 
}

listing 13
/* 
  Assume that fp is associated with an output file. 
*/ 
/* ... */ 
fwrite(buf, sizeof(data_type), 1, fp); 
fflush(fp); 
/* ... */

listing 14
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  FILE *fp; 
  char ch; 
 
  if((fp=fopen(argv[1], "r"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  while((ch=fgetc(fp))!=EOF) { 
    printf("%c", ch); 
  } 
  fclose(fp); 
 
  return 0; 
}

listing 15
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  FILE *fp; 
  long l; 
  int  i; 
  fpos_t *pos;  /* fpos_t is defined in stdio.h */ 
  pos = &l; 
 
  if((fp=fopen(argv[1], "w+"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  for (i=0; i<10; i++) 
    fputc('Z', fp);  /* write 10 Z's to the file */ 
  fgetpos(fp, pos); 
 
  printf("We are now at position %ld in the file.", *pos); 
  fclose(fp); 
 
  return 0; 
}

listing 16
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  FILE *fp; 
  char str[128]; 
 
  if((fp=fopen(argv[1], "r"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  while(!feof(fp)) { 
    if(fgets(str, 126, fp)) 
      printf("%s", str); 
  } 
  fclose(fp); 
 
  return 0; 
}

listing 17
printf("The file is %ld bytes long.", filelength(fd));

listing 18
FILE *stream; 
int fd; 
 
if((stream=fopen("TEST", "r"))==NULL) { 
  printf("Cannot open TEST file.\n"); 
  exit(1); 
} 
 
fd = fileno(stream);

listing 19
/* 
  Assume that fp is associated with an output file. 
*/ 
/* ... */ 
fwrite(buf,sizeof(data_type),1,fp); 
_flushall(); 


listing 20
FILE *fp; 
 
if ((fp = fopen("test", "w"))==NULL) { 
  printf("Cannot open file.\n"); 
  exit(1); 
}

listing 21
FILE *fp; 
 
if((fp=fopen("test", "rb+"))==NULL) { 
  printf("Cannot open file.\n"); 
  exit(1); 
}

listing 22
#include <stdio.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  FILE *fp; 
 
  if((fp=fopen("test", "w"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  fprintf(fp, "this is a test %d %f", 10, 20.01); 
 
  fclose(fp); 
 
  return 0; 
}

listing 23
void write_string(char *str, FILE *fp) 
{ 
  while(*str) if(!ferror(fp)) fputc(*str++, fp); 
}

listing 24
void write_string(char *str) 
{ 
  while(*str) if(!ferror(fp)) fputchar(*str++); 
}

listing 25
fputs("this is a test", fp);

listing 26
#include <stdio.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  FILE *fp; 
  float bal[10]; 
 
  if((fp=fopen("test", "rb"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  if(fread(bal, sizeof(float), 10, fp)!=10) { 
    if(feof(fp)) printf("Premature end of file."); 
    else printf("File read error."); 
  } 
 
  fclose(fp); 
 
  return 0; 
}

listing 27
#include <stdio.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  FILE *fp; 
 
  printf("This will display on the screen\n"); 
 
  if((fp=freopen("OUT", "w", stdout))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  printf("This will be written to the file OUT"); 
  fclose(fp); 
 
  return 0; 
}

listing 28
char str[80]; 
float f; 
 
fscanf(fp, "%s%f", str, &f);

listing 29
struct addr { 
  char name[40]; 
  char street[40]; 
  char city[40]; 
  char state[3]; 
  char zip[10]; 
} info; 
 
void find(long client_num) 
{ 
  FILE *fp; 
 
  if((fp=fopen("mail", "rb"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  /* find the proper structure */ 
  fseek(client_num*sizeof(struct addr), 0); 
 
  /* read the data into memory */ 
  fread(&info, sizeof(struct addr), 1, fp); 
  fclose(fp); 
}

listing 30
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  FILE *fp; 
  long l; 
  int  i; 
  fpos_t *pos;  /* fpos_t is defined in stdio.h */ 
  pos = &l; 
 
  if((fp=fopen(argv[1], "w+"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  for (i=0; i<10; i++) 
    fputc('Y', fp);  /* write 10 Y's to the file */ 
  fgetpos(fp, pos); 
 
  for (i=0; i<10; i++) 
    fputc('Z', fp);  /* write 10 Z's to the file */ 
  fsetpos(fp, pos);  /* reset to the end of the Y's */ 
 
  fputc('A', fp);     /* replace first Z with an A. */ 
  fclose(fp); 
 
  return 0; 
}

listing 31
fp=_fsopen("TEST.DAT", "wb", SH_DENYRD);

listing 32
#include <stdio.h> 
#include <sys\stat.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  FILE *fp; 
  struct stat buff; 
 
  if((fp=fopen("test", "rb"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  /* fill the stat structure */ 
  fstat(fileno(fp), &buff); 
 
  printf("Size of the file is: %ld\n", buff.st_size); 
  fclose(fp); 
 
  return 0; 
}

listing 33
long i; 
if((i=ftell(fp))==-1L) printf("A file error has occurred.\n");

listing 34
#include <stdio.h> 
#include <stdlib.h> 
 
int main(void) 
{ 
  FILE *fp; 
  float f=12.23; 
 
  if((fp=fopen("test", "wb"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 
  } 
 
  fwrite(&f, sizeof(float), 1, fp); 
 
  fclose(fp); 
 
  return 0; 
}

listing 35
#include <stdio.h> 
#include <stdlib.h> 
 
int main(int argc, char *argv[]) 
{ 
  FILE *fp; 
  char ch; 
 
  if((fp=fopen(argv[1], "r"))==NULL) { 
    printf("Cannot open file.\n"); 
    exit(1); 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -