📄 chap11.lst
字号:
}
while((ch=getc(fp))!=EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
listing 36
do {
printf("1: Check spelling\n");
printf("2: Correct spelling\n");
printf("3: Look up a word in the dictionary\n");
printf("4: Quit\n");
printf("\nEnter your selection: ");
choice = getch();
} while(!strchr("1234", choice));
listing 37
#include <stdio.h>
int main(void)
{
char s[256], *p;
p = s;
while((*p++=getchar())!='\n') ;
*p = '\0'; /* add null terminator */
printf(s);
return 0;
}
listing 38
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
char fname[128];
printf("Enter filename: ");
gets(fname);
if((fp=fopen(fname, "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
/* ...*/
fclose(fp);
return 0;
}
listing 39
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
int sum = 0;
if((fp=fopen("inttest", "rb"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}
while(!feof(fp))
sum = getw(fp)+sum;
printf("The sum is %d", sum);
fclose(fp);
return 0;
}
listing 40
if(isatty(fd)) printf("is a character device");
else printf("is not a character device");
listing 41
lock(fd, 0, 128);
listing 42
if(locking(fd, LK_UNLOCK, 10)) {
// process error
}
listing 43
#include <stdio.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#include <stdlib.h>
#define BUF_SIZE 128
/* read buffers using lseek() */
int main(int argc, char *argv[])
{
char buf[BUF_SIZE+1], s[10];
int fd, sector;
buf[BUF_SIZE+1] = '\0'; /* null terminate buffer for printf */
if((fd=open(argv[1], O_RDONLY | O_BINARY))==-1) { /* open for write */
printf("Cannot open file.\n");
exit(0);
}
do {
printf("Buffer: ");
gets(s);
sector = atoi(s); /* get the sector to read */
if(lseek(fd, (long)sector*BUF_SIZE,0)==-1L)
printf("Seek Error\n");
if(read(fd, buf, BUF_SIZE)==0) {
printf("Read Error\n");
}
else {
printf("%s\n", buf);
}
} while(sector > 0);
close(fd);
return 0;
}
listing 44
if((fd=open(filename, mode)) == -1) {
printf("Cannot open file.\n");
exit(1);
}
listing 45
#include <stdio.h>
#include <math.h>
#include <errno.h> /* contains declaration for errno */
int main(void)
{
/* this will generate a domain error */
asin(10.0);
if(errno==EDOM)
perror("Program Error Test");
return 0;
}
listing 46
printf("Hi %c %d %s", 'c', 10, "there!");
listing 47
int i;
printf("this is a test %n", &i);
printf("%d", i);
listing 48
#include <stdio.h>
int main(void)
{
/* This prints "this is a test" left-justified
in a 20-character field.
*/
printf("%-20s", "this is a test");
/* This prints a float with 3 decimal places in a
10-character field. The output will be " 12.235".
*/
printf("%10.3f", 12.234657);
return 0;
}
listing 49
for(; *str; str++) putc(*str, fp);
listing 50
putch('X');
listing 51
for(; *str; str++) putchar(*str);
listing 52
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[80];
strcpy(str, "this is an example");
puts(str);
return 0;
}
listing 53
putw(100, fp);
listing 54
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
int main(void)
{
int fd;
char buffer[100];
if((fd=open("TEST.TST", O_RDONLY))==-1) {
printf("Cannot open file.\n");
exit(1);
}
if(read(fd, buffer, 100)!=100)
printf("Possible read error.\n");
return 0;
}
listing 55
#include <stdio.h>
int main(int argc, char *argv[])
{
if(remove(argv[1])==-1)
printf("Remove Error\n");
return 0;
}
listing 56
#include <stdio.h>
int main(int argc, char *argv[])
{
if(rename(argv[1], argv[2])!=0)
printf("Rename Error\n");
return 0;
}
listing 57
void re_read(FILE *fp)
{
/* read once */
while(!feof(fp)) putchar(getc(fp));
rewind(fp);
/* read twice */
while(!feof(fp)) putchar(getc(fp));
}
listing 58
scanf("%d", &count);
listing 59
scanf("%s", address);
listing 60
scanf("%d%d", &r, &c);
listing 61
scanf("%d%*c%d", &x, &y);
listing 62
scanf("%20s", address);
listing 63
scanf("%c%c%c", &a, &b, &c);
listing 64
scanf("%st%s", &x, &y);
listing 65
%[ABC]
listing 66
%[A-Z]
listing 67
char str[80];
int i;
/* read a string and an integer */
scanf("%s%d", str, &i);
/* read up to 79 chars into str */
scanf("%79s", str);
/* skip the integer between the two strings */
scanf("%s%*d%s", str, &i, str);
listing 68
char buffer[BUFSIZ];
/* ... */
setbuf(fp,buffer);
listing 69
setmode(fd, O_TEXT)
listing 70
#include <stdio.h>
char buffer[128];
/* ... */
setvbuf(fp, buffer, _IOLBF, 128);
listing 71
if((fd=sopen(filename, access, shflag, mode)) ==-1) {
printf("Cannot open file.\n");
exit(1);
}
listing 72
char str[80];
sprintf(str, "%s %d %c", "one", 2, '3');
listing 73
#include <stdio.h>
int main(void)
{
char str[80];
int i;
sscanf("hello 1 2 3 4 5", "%s%d", str, &i);
printf("%s %d", str, i);
return 0;
}
listing 74
#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 */
stat("test", &buff);
printf("Size of the file is: %ld\n", buff.st_size);
fclose(fp);
return 0;
}
listing 75
long pos;
/* ... */
pos = tell(fd);
printf("Position indicator is %ld bytes from the start", pos);
listing 76
FILE *temp;
if(!(temp=tmpfile())) {
printf("Cannot open temporary work file.\n");
exit(1);
}
listing 77
#include <stdio.h>
int main(void)
{
char name[40];
int i;
for(i=0; i<3; i++) {
tmpnam(name);
printf("%s ", name);
}
return 0;
}
listing 78
void read_word(FILE *fp, char *token)
{
while(isalpha(*token=getc(fp))) token++;
ungetc(fp, *token);
}
listing 79
#include <stdio.h>
#include <conio.h>
int main(void)
{
char ch;
ch = getch(); // get keypress
putch(ch); // show the key
ungetch(ch); // return to buffer
ch = getch(); // get same key again
putch(ch); // show the key again
return 0;
}
listing 80
#include <stdio.h>
#include <dos.h>
int main(int argc, char *argv[])
{
if(unlink(argv[1])==-1)
printf("Cannot remove file.");
return 0;
}
listing 81
unlock(fd, 0, 128);
listing 82
#include <stdio.h>
#include <stdarg.h>
void print_message(char *, ...);
int main(void)
{
print_message("Cannot open file %s","test");
return 0;
}
void print_message( char *format, ...)
{
va_list ptr; /* get an arg ptr */
/* initialize ptr to point to the first argument after the
format string
*/
va_start(ptr, format);
/* print out message */
vprintf(format, ptr);
va_end(ptr);
}
listing 83
#include <stdio.h>
#include <stdarg.h>
void read_int(int num, ...);
int main(void)
{
int a, b;
read_int(2, &a, &b);
printf("%d %d", a, b);
return 0;
}
void read_int(int num, ...)
{
va_list ptr; /* get an arg ptr */
/* initialize ptr to point to the first argument after the
format string
*/
va_start(ptr, num);
printf("Enter %d integers: ", num);
/* read ints */
vscanf("%d %d", ptr);
va_end(ptr);
}
listing 84
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
int main(void)
{
int fd;
char buffer[100];
if((fd=open("test", O_WRONLY))==-1) {
printf("Cannot open file.\n");
exit(1);
}
gets(buffer);
if(write(fd, buffer, 100)!=100)
printf("Write Error");
close(fd);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -