C Primer Plus 第六版 第13章 编程答案
1.
#include
#include
#include
#define SIZE 100
int main(void)
{
int ch;
FILE *fp;
int count = 0;
char * find;
char file[SIZE];
puts("请输入文件名: ");
fgets(file, SIZE, stdin);
if (find = strchr(file, '\n'))
*find = '\0';
if ((fp = fopen(file, "r")) == NULL)
{
puts("打开失败");
exit(EXIT_FAILURE);
}
while ((ch = getc(fp)) != EOF)
{
putc(ch, stdout);
count++;
}
fclose(fp);
printf("File %s has %d characters\n", file, count);
return 0;
}
2.
#include
#include
#define SIZE 500
int main(int argc, char *argv [])
{
FILE * fp1, * fp2;
if (argc < 3)
{
fprintf(stderr, "参数不全\n");
return -1;
}
if ((fp1 = fopen(argv[1], "rb")) == NULL)
{
fprintf(stderr, "%s 打开失败\n", argv[1]);
return -1;
}
if ((fp2 = fopen(argv[2], "wb")) == NULL)
{
fprintf(stderr, "%s 打开失败\n", argv[2]);
fclose(fp1);
return -1;
}
char strbuf[SIZE];
int nread = 0;
while (1)
{
memset(strbuf, 0, sizeof(strbuf));
if ((nread = fread(strbuf, sizeof(char), SIZE, fp1)) == 0)
break;
printf("%d\n", nread);
fwrite(strbuf, sizeof(char), nread, fp2);
}
fclose(fp1); fclose(fp2);
puts("文件复制成功");
return 0;
}
3.
#include
#include
#include
#include
#define SIZE 100
int main(void)
{
FILE * fp;
char str[SIZE];
char * find;
puts("请输入文件名");
fgets(str, SIZE, stdin);
if (find = strchr(str, '\n'))
*find = '\0';
if ((fp = fopen(str, "r")) == NULL)
{
printf("文件 %s 打开失败\n", str);
return -1;
}
long count;
fseek(fp, 0L, SEEK_END);
count = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char * st = (char *)malloc(sizeof(char) * count);
int ch;
int i = 0;
while ((ch = getc(fp)) != EOF)
{
ch = toupper(ch);
st[i] = ch;
i++;
}
fclose(fp);
if ((fp = fopen(str, "w")) == NULL)
{
printf("文件 %s 打开失败\n", str);
return -1;
}
for (i = 0; i < count; i++)
putc(st[i], fp);
fclose(fp);
free(st);
printf("拷贝成功 %d\n", i);
return 0;
}
4.
#include
int main(int argc, char **argv)
{
FILE * fp;
int i;
int ch;
if (argc < 2)
{
puts("参数不齐");
return -1;
}
for (i = 1; i < argc; i++)
{
if ((fp = fopen(argv[i], "rt")) == NULL)
{
printf("打开 %s 失败\n", argv[i]);
return -1;
}
printf("%s 文件: \n", argv[i]);
while ((ch = getc(fp)) != EOF)
putc(ch, stdout);
fclose(fp);
}
return 0;
}
5.
#include
#include
#include
#define BUFSIZE 4096
#define SLEN 81
int main(int argc, char *argv [])
{
FILE * fp1, * fp2;
if (argc < 3)
{
puts("参数不全");
return -1;
}
if ((fp1 = fopen(argv[1], "a+")) == NULL)
{
fprintf(stderr, "打开文件 %s 失败\n", argv[1]);
return -1;
}
if (setvbuf(fp1, NULL, _IOFBF, BUFSIZE) != 0)
{
puts("申请失败");
return -1;
}
if ((fp2 = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "打开文件 %s 失败\n", argv[2]);
return -1;
}
if (setvbuf(fp2, NULL, _IOFBF, BUFSIZE) != 0)
{
puts("申请失败");
return -1;
}
size_t bytes;
char temp[BUFSIZE];
while ((bytes = fread(temp, sizeof(char), BUFSIZE, fp2)) > 0)
fwrite(temp, sizeof(char), bytes, fp1);
if (ferror(fp1) != 0)
fprintf(stderr, "Error in reading file %s.\n", argv[1]);
if (ferror(fp2) != 0)
fprintf(stderr, "Error in writing file %s.\n", argv[2]);
rewind(fp1);
int ch;
printf("%s contents: \n", argv[1]);
while ((ch = getc(fp1)) != EOF)
putchar(ch);
fclose(fp1); fclose(fp2);
puts("附加成功");
return 0;
}
6.
#include
#include
#include
#define LEN 40
int main(void)
{
FILE * fp1, * fp2;
char name1[LEN], name2[LEN];
char *find;
int ch;
int count = 0;
puts("请输入文件名: ");
fgets(name1, LEN, stdin);
if (find = strchr(name1, '\n'))
*find = '\0';
if ((fp1 = fopen(name1, "r")) == NULL)
{
fprintf(stderr, "文件 %s 打开失败\n", name1);
return -1;
}
strncpy(name2, name1, LEN-5);
name2[LEN - 5] = '\0';
strcat(name2, ".red");
if ((fp2 = fopen(name2, "w")) == NULL)
{
fprintf(stderr, "文件 %s 打开失败\n", name2);
return -1;
}
while ((ch = getc(fp1)) != EOF)
if (count++ % 3 == 0)
putc(ch, fp2);
if (fclose(fp1) != 0 || fclose(fp2) != 0)
fprintf(stderr, "Error in closing files\n");
return 0;
}
7.a
#include
int main(int argc, char *argv [])
{
FILE * fp1, * fp2;
int ch1, ch2;
if (argc != 3)
{
puts("参数不全");
return -1;
}
if ((fp1 =fopen(argv[1], "r")) == NULL)
{
printf("%s 打开失败\n", argv[1]);
return -1;
}
if ((fp2 =fopen(argv[2], "r")) == NULL)
{
fclose(fp1);
printf("%s 打开失败\n", argv[2]);
return -1;
}
ch1 = getc(fp1);
ch2 = getc(fp2);
while (ch1 != EOF || ch2 != EOF)
{
while (ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = getc(fp1);
}
if (ch1 != EOF)
{
putchar('\n');
ch1 = getc(fp1);
}
while (ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
if (ch2 != EOF)
{
putchar('\n');
ch2 = getc(fp2);
}
}
if (fclose(fp1) || fclose(fp2))
printf("关闭文件失败");
return 0;
}
7.b
#include
int main(int argc, char *argv [])
{
FILE * fp1, * fp2;
int ch1, ch2;
if (argc != 3)
{
puts("参数不全");
return -1;
}
if ((fp1 =fopen(argv[1], "r")) == NULL)
{
printf("%s 打开失败\n", argv[1]);
return -1;
}
if ((fp2 =fopen(argv[2], "r")) == NULL)
{
fclose(fp1);
printf("%s 打开失败\n", argv[2]);
return -1;
}
ch1 = getc(fp1);
ch2 = getc(fp2);
while (ch1 != EOF || ch2 != EOF)
{
while (ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = getc(fp1);
}
if (ch1 != EOF)
{
putchar(' ');
ch1 = getc(fp1);
}
while (ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = getc(fp2);
}
if (ch2 != EOF)
{
ch2 = getc(fp2);
}
putchar('\n');
}
if (fclose(fp1) || fclose(fp2))
printf("关闭文件失败");
return 0;
}
8.
#include
int main(int argc, char *argv [])
{
FILE * fp;
int count = 0;
int ch;
int i;
if (argc < 2)
{
puts("参数不全");
return -1;
}
if (argc == 2)
{
puts("请输入文本 ");
while ((ch = getchar()) != EOF)
{
if (ch == argv[1][0])
count++;
}
printf("%s 在文本出现了 %d\n",argv[1], count);
return 0;
}
for (i = 2; i < argc; i++, count = 0)
{
if ((fp = fopen(argv[i], "r")) == NULL)
{
printf("%s 打开失败\n", argv[i]);
return -1;
}
while ((ch = getc(fp)) != EOF)
{
if (ch == argv[1][0])
count++;
}
printf("%s 在 %s 出现了 %d\n",argv[1], argv[i], count);
fclose(fp);
}
return 0;
}
9.
#include
#include
#include
#define MAX 41
int main(void)
{
FILE * fp;
char words[MAX];
int count = 0;
if ((fp = fopen("wordy", "a+")) == NULL)
{
puts("打开失败");
return -1;
}
while ((fgets(words, MAX, fp)) != NULL)
count++;
rewind(fp);
puts("请输入 #结束");
while ((fscanf(stdin, "%40s", words) == 1) && (words[0] != '#'))
fprintf(fp, "%03d: %s\n", ++count, words);
rewind(fp);
while (fgets(words, MAX, fp) != NULL)
fputs(words, stdout);
fclose(fp);
return 0;
}
10.
#include
#include
#define LEN 1000
char * s_gets(char *st, int n);
int main(void)
{
FILE * fp;
long i;
char file[LEN];
puts("请输入文件: ");
s_gets(file, LEN);
if ((fp = fopen(file, "r")) == NULL)
{
puts("打开失败");
return -1;
}
puts("输入开始处: ");
while (scanf("%ld", &i) == 1 && i >= 0)
{
fseek(fp, i, SEEK_SET);
memset(file , 0, sizeof(file));
fgets(file, LEN, fp);
fputs(file, stdout);
}
return 0;
}
char * s_gets(char *st, int n)
{
char * ret_val;
char * find;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
if (find = strchr(st, '\n'))
*find = '\0';
else
while (getchar() != '\n');
}
return ret_val;
}
11.
#include
#include
#define LEN 255
int main(int argc, char **argv)
{
FILE * fp;
char len[LEN];
if (argc != 3)
{
puts("参数错误");
return -1;
}
if ((fp = fopen(argv[2], "r")) == NULL)
{
puts("打开文件失败");
return -1;
}
while (fgets(len, LEN, fp) != NULL)
if (strstr(len, argv[1]))
fputs(len, stdout);
fclose(fp);
return 0;
}
12.
#include
#include
#define ROWS 20
#define COLS 30
#define LEVELS 10
const char trans[LEVELS + 1] = " .':~*= %#";
void MakePic(int data[][COLS], char pic[][COLS], int rows);
int main(void)
{
int row, col;
int picIn[ROWS][COLS];
char picOut[ROWS][COLS];
char fileName[81];
FILE * infile;
printf("Enter name of file: ");
scanf("%80s", fileName);
if ((infile = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Could not open data file.\n");
exit(EXIT_FAILURE);
}
for (row = 0; row < ROWS; row++)
for (col = 0; col < COLS; col++)
fscanf(infile, "%d", &picIn[row][col]);
MakePic(picIn, picOut, ROWS);
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
putchar(picOut[row][col]);
putchar('\n');
}
fclose(infile);
return 0;
}
void MakePic(int data[][COLS], char pic[][COLS], int rows)
{
int row, col;
for (row = 0; row < rows; row++)
for (col = 0; col < COLS; col++)
pic[row][col] = trans[ data[row][col]];
}
13.
#include
#include
#define LEVELS 10
const char trans[LEVELS + 1] = " .':~*= %#";
void MakePic(int rows, int cols, int data[rows][cols], char pic[rows][cols]);
int main(void)
{
int row, col;
int rows = 20, cols = 30;
int picIn[rows][cols];
char picOut[rows][cols];
char fileName[81];
FILE * infile;
printf("Enter name of file: ");
scanf("%80s", fileName);
if ((infile = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Could not open data file.\n");
exit(EXIT_FAILURE);
}
for (row = 0; row < rows; row++)
for (col = 0; col < cols; col++)
fscanf(infile, "%d", &picIn[row][col]);
MakePic(rows, cols, picIn, picOut);
for (row = 0; row < rows; row++)
{
for (col = 0; col < cols; col++)
putchar(picOut[row][col]);
putchar('\n');
}
fclose(infile);
return 0;
}
void MakePic(int rows, int cols, int data[rows][cols], char pic[rows][cols])
{
int row, col;
for (row = 0; row < rows; row++)
for (col = 0; col < cols; col++)
pic[row][col] = trans[ data[row][col]];
}
14.
#include
#include
#define ROWS 20
#define COLS 30
#define LEVELS 10
const char trans[LEVELS + 1] = " .':~*= %#";
void MakePic(int data[][COLS], char pic[][COLS], int rows);
int shizhen(int (*ar)[COLS], int x, int y);
int main(void)
{
int row, col;
int picIn[ROWS][COLS];
char picOut[ROWS][COLS];
char fileName[81];
FILE * infile;
printf("Enter name of file: ");
scanf("%80s", fileName);
if ((infile = fopen(fileName, "r")) == NULL)
{
fprintf(stderr, "Could not open data file.\n");
exit(EXIT_FAILURE);
}
for (row = 0; row < ROWS; row++)
for (col = 0; col < COLS; col++)
fscanf(infile, "%d", &picIn[row][col]);
for (row = 0; row < ROWS; row++)
for (col = 0; col < COLS; col++)
picIn[row][col] = shizhen(picIn, row, col);
MakePic(picIn, picOut, ROWS);
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
putchar(picOut[row][col]);
}
putchar('\n');
}
fclose(infile);
return 0;
}
void MakePic(int data[][COLS], char pic[][COLS], int rows)
{
int row, col;
for (row = 0; row < rows; row++)
for (col = 0; col < COLS; col++)
pic[row][col] = trans[ data[row][col]];
}
int shizhen(int (*ar)[COLS], int x, int y)
{
int sum;
if (x == 0)
{
if (y == 0)
{
if (ar[x][y] - ar[x+1][y] > 1 && ar[x][y] - ar[x][y+1] > 1)
{
sum = (ar[x+1][y] + ar[x][y+1]) / 2.0 + 0.5;
return sum;
}
}
if (y == COLS - 1)
{
if (ar[x][y] - ar[x][y-1] > 1 && ar[x][y] - ar[x+1][y] >1)
{
sum = (ar[x][y-1] + ar[x+1][y]) / 2.0 + 0.5;
return sum;
}
}
if (ar[x][y] - ar[x][y-1] > 1 &&
ar[x][y] - ar[x-1][y] > 1 &&
ar[x][y] - ar[x][y+1] > 1)
{
sum = (ar[x][y-1] + ar[x-1][y] + ar[x][y+1]) / 3.0 + 0.5;
return sum;
}
}
if (x == ROWS - 1)
{
if (y == 0)
{
if (ar[x][y] - ar[x-1][y] > 1 && ar[x][y] - ar[x][y+1] > 1)
{
sum = (ar[x-1][y] + ar[x][y+1]) / 2.0 + 0.5;
return sum;
}
}
if (y == COLS - 1)
{
if (ar[x][y] - ar[x][y-1] > 1 && ar[x][y] - ar[x-1][y] > 1)
{
sum = (ar[x][y-1] + ar[x-1][y]) / 2.0 + 0.5;
return sum;
}
}
if (ar[x][y] - ar[x][y-1] > 1 &&
ar[x][y] - ar[x-1][y] > 1 &&
ar[x][y] - ar[x][y+1] > 1)
{
sum = (ar[x][y-1] + ar[x-1][y] + ar[x][y-1]) / 3.0 + 0.5;
return sum;
}
}
if (y == 0)
{
if (ar[x][y] - ar[x-1][y] > 1 &&
ar[x][y] - ar[x][y+1] > 1 &&
ar[x][y] - ar[x+1][y] > 1)
{
sum = (ar[x-1][y] + ar[x][y+1] + ar[x+1][y]) / 3.0 + 0.5;
return sum;
}
}
if (y == COLS - 1)
{
if (ar[x][y] - ar[x-1][y] > 1 &&
ar[x][y] - ar[x][y-1] > 1 &&
ar[x][y] - ar[x+1][y] > 1)
{
sum = (ar[x-1][y] + ar[x][y-1] + ar[x+1][y]) / 3.0 + 0.5;
return sum;
}
}
if (ar[x][y] - ar[x-1][y] > 1 &&
ar[x][y] - ar[x+1][y] > 1 &&
ar[x][y] - ar[x][y-1] > 1 &&
ar[x][y] - ar[x][y+1] > 1)
{
sum = (ar[x-1][y] + ar[x+1][y] + ar[x][y-1] + ar[x][y+1]) / 4.0 + 0.5;
return sum;
}
return ar[x][y];
}
文本
0 0 9 0 0 0 0 0 0 0 0 0 5 8 9 9 8 5 2 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 9 0 0 0 0 0 0 0 5 8 9 9 8 5 5 2 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 5 8 1 9 8 5 4 5 2 0 0 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