书中的一个例子,我也是想了半天了!!!有点难度!!!
/* 把多个文件的内容追加到一个文件中 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main(void)
{
FILE *fa, *fs; // fa指向追加的目的文件, fs指向源文件
int files = 0; // 追加文件的个数
char file_app[SLEN]; // 被追加文件的名称
char file_src[SLEN]; // 源文件的名称
puts("Enter name of destination file: ");
gets(file_app);
// "a":打开一个文本文件,可以写入文件,向已有文件的尾部追加内容,如果
// 该文件不存在则先创建之
if((fa = fopen(file_app, "a")) == NULL)
{
fprintf(stderr, "Can't open %s\n", file_app);
exit(2);
}
/*
创建一个输出缓冲区
NULL---函数会自动为自己分配一个缓冲区;
BUFSIZE---其大小为1024byte;
_IOFBF---完全缓冲(缓冲区满的时候刷新)
*/
if(setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\n", stderr);
exit(3);
}
puts("Enter name of first source file (empty line to quit): ");
while(gets(file_src) && file_src[0] != '\0')
{
if(strcmp(file_src, file_app) == 0)
fputs("Can't append file to itself\n", stderr);
else if((fs = fopen(file_src, "r")) == NULL)
fprintf(stderr, "Can't open %s\n", file_src);
else
{
/*
创建一个输入缓冲区
NULL---函数会自动为自己分配一个缓冲区;
BUFSIZE---其大小为1024byte;
_IOFBF---完全缓冲(缓冲区满的时候刷新)
*/
if(setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create input buffer\n", stderr);
continue;
}
append(fs, fa);
// 发生读取错误
if(ferror(fs) != 0)
fprintf(stderr, "Error in reading file %s.\n", file_src);
// 发生写入错误
if(ferror(fa) != 0)
fprintf(stderr, "Error in writing file %s.\n", file_app);
fclose(fs);
files++;
printf("File %s appended.\n", file_src);
puts("Next file(empty line to quit): ");
}
}
printf("Done. %d files appended.\n", files);
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0)
fwrite(temp, sizeof(char), bytes, dest);
}
我的运行结果:
确实追加成功!!!
复习题1、下面的程序有什么问题?
int main(void)
{
int * fp;
int k;
fp = fopen("gelatin");
for(k = 0; k < 30; k++)
fputs(fp, "Nanette eats gelatin.");
fclose("gelatin");
return 0;
}
答:(参考课后答案)
因为程序中有文件定义,所以应该有#include<stdio.h>。应该把fp声明为文件指针:FILE *fp;。函数fopen()需要一种模式:fopen("gelatin", "w");或"a"模式。fputs()函数中参数的次序应该反过来。为了清除起见,输出字符串应该具有一个换行符,因为fputs()并不自动添加它。fclose()函数需要一个文件指针而不是文件名:fclose(fp);。下面是一个正确的版本:
#include <stdio.h>
int main(void)
{
FILE * fp;
int k;
fp = fopen("gelatin", "w");
for(k = 0; k < 30; k++)
fputs("Nanette eats gelatin.\n", fp);
fclose(fp);
return 0;
}
2、下面程序的作用是什么?(Macintosh用户可以假设程序正确地使用了console.h和ccommand()函数。)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
int ch;
FILE * fp;
if(argc < 2)
exit(2);
if((fp = fopen(argv[1], "r")) == NULL)
exit(1);
while((ch = getc(fp)) != EOF)
if(isdigit(ch))
putchar(ch);
fclose(fp);
return 0;
}
答:
如果可能的话,它会打开名字与第一个命令行参数相同的文件,并在屏幕上显示文件中的每个数字字符。
3、假设在程序中有这样一些语句:
#include <stdio.h>
FILE * fp1, * fp2;
char ch;
fp1 = fopen("terky", "r");
fp2 = fopen("jerky", "w");
并且,假设两个文件都已被成功地打开了。为下面的函数调用提供缺少的参数:
a.ch = getc();
b.fprintf(, "%c\n",);
c.putc(,);
d.fclose(); /* 关闭terky文件 */
答:
a.ch = getc(fp1);
b.fprintf(fp2, "%c\n", ch);
c.putc(ch, fp2);
d.fclose(fp1); /* 关闭terky文件 */
4、编写一段程序。它不读取任何命令行参数或者读取一个命令行参数。如果有一个参数,程序将它作为一个输入文件名。如果没有参数,使用标准输入(stdin)作为输入。假设输入完全由浮点数组成。让程序计算并且报告输入数字的算数平均值。
答:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int ch;
FILE * fp;
double n, sum = 0.0;
int ct = 0;
if(argc == 1)
fp = stdin;
else if(argc == 2)
{
if((fp = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
}
else
{
fprintf(stderr, "Usage: %s [filename]\n", argv[0]);
exit(EXIT_FAILURE);
}
while(fscanf(fp, "%lf", &n) == 1)
{
sum += n;
++ct;
}
if(ct > 0)
printf("Average of %d values = %f\n", ct, sum / ct);
else
printf("No valid data.\n");
fclose(fp);
return 0;
}
5、编写一段程序。它接受两个命令行参数,第一个是一个字符,第二个是一个文件名。要求程序只打印文件中包含给定字符的哪些行。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE * fp;
char input[1001];
if(argc < 3)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if((fp = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
while(fgets(input, 256, fp) != NULL)
{
if(strchr(input, argv[1][0]) != NULL)
fputs(input, stdout);
}
fclose(fp);
return 0;
}
6、对于二进制流而言,二进制文件和文本文件有什么区别?对于文本流呢?
答:(参考课后答案)
二进制文件和文本文件之间的不同在于这两种文件格式对
系统的依赖性不同。二进制流和文本流之间的不同则包括在读写流时由程序执行的转换(
二进制流不进行转换,而文本流可能会转换换行符和其他字符)。
7、下面两者之间的区别是什么?
a.通过使用fprintf()和使用fwrite()保存8238201.
b.通过使用putc()和使用fwrite()保存字符S.
答:
a.使用fprintf()存储8238201时,把它作为在7个字节中的7个字符来保存;而使用fwrite()来存储时,使用该数值的二进制表示把它保存为一个4字节的整数。
b.没有不同,每种情况下都保存为一个单字节的二进制码。
8、下列语句的区别是什么?
printf("Hello, %s\n", name);
fprintf(stdout, "Hello, %s\n", name);
fprintf(stderr, "Hello, %s\n", name);
答:
第一个只是第二个的速记表示;第三个写到标准错误上。通常标准错误被定向到与标准输出同样的位置,但是标准错误不受标准输出重定向的影响。
9、以"a+"、"r+"和"w+"模式打开的文件都是可读可写的。哪种模式更适合用来改变文件中已有的内容?
答:"r+"模式使您可以在文件中的任何位置读写,所以它是最合适的。"a+"只允许您在文件的末尾添加内容,而"w+"给您提供一个空文件,它丢弃以前的文件内容。
编程练习1、
#include <stdio.h>
#include <stdlib.h>
#define SLEN 50
int main(void)
{
int ch;
FILE * fp;
char file[SLEN];
long count = 0;
puts("Enter the name of the file to be processed: ");
gets(file);
if((fp = fopen(file, "r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", file);
exit(EXIT_FAILURE);
}
while((ch = getc(fp)) != EOF)
{
putc(ch, stdout);
count++;
}
fclose(fp);
printf("File %s has %ld characters\n", file, count);
return 0;
}
2、
#include <stdio.h>
#include <stdlib.h>
#define SLEN 50
int main(int argc, char *argv[])
{
FILE *in, *out;
int ch;
char file[SLEN];
if(argc < 3)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if((in = fopen(argv[1], "rb")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if((out = fopen(argv[2], "wb")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
// 复制
while((ch = getc(in)) != EOF)
putc(ch, out);
if(fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closing files\n");
return 0;
}
3、
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SLEN 50
int main(void)
{
FILE *in, *out;
int ch;
char file_source[SLEN], file_target[SLEN];
printf("Enter the source file name and the output file name: \n");
scanf("%s %s", file_source, file_target);
if((in = fopen(file_source, "r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", file_source);
exit(EXIT_FAILURE);
}
if((out = fopen(file_target, "w")) == NULL)
{
fprintf(stderr, "Can't open %s\n", file_target);
exit(EXIT_FAILURE);
}
// 复制
while((ch = getc(in)) != EOF)
putc(toupper(ch), out);
if(fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closing files\n");
return 0;
}
4、
#include <stdio.h>
#include <stdlib.h>
#define SLEN 50
int main(int argc, char *argv[])
{
FILE *fp;
int ch;
int i;
// 命令行参数只有程序名时
if(argc < 2)
{
printf("Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
// 命令行参数中有文件名时
for(i = 1; i < argc; i++)
{
if((fp = fopen(argv[i], "r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[i]);
exit(EXIT_FAILURE);
}
printf("-------------------------file %s content-------------------------\n", argv[i]);
while((ch = getc(fp)) != EOF)
putc(ch, stdout);
putchar('\n');
if(fclose(fp) != 0)
fprintf(stderr, "Error in closing files\n");
}
return 0;
}
5、
/* 把多个文件的内容追加到一个文件中 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main(int argc, char *argv[])
{
FILE *fa, *fs; // fa指向追加的目的文件, fs指向源文件
int files = 0; // 追加文件的个数
int i = 2;
//char file_app[SLEN]; // 被追加文件的名称
//char file_src[SLEN]; // 源文件的名称
//puts("Enter name of destination file: ");
//gets(file_app);
// "a":打开一个文本文件,可以写入文件,向已有文件的尾部追加内容,如果
// 该文件不存在则先创建之
if((fa = fopen(argv[1], "a")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(2);
}
/*
创建一个输出缓冲区
NULL---函数会自动为自己分配一个缓冲区;
BUFSIZE---其大小为1024byte;
_IOFBF---完全缓冲(缓冲区满的时候刷新)
*/
if(setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\n", stderr);
exit(3);
}
//puts("Enter name of first source file (empty line to quit): ");
while(i < argc)
{
if(strcmp(argv[i], argv[1]) == 0)
fputs("Can't append file to itself\n", stderr);
else if((fs = fopen(argv[i], "r")) == NULL)
fprintf(stderr, "Can't open %s\n", argv[i]);
else
{
/*
创建一个输入缓冲区
NULL---函数会自动为自己分配一个缓冲区;
BUFSIZE---其大小为1024byte;
_IOFBF---完全缓冲(缓冲区满的时候刷新)
*/
if(setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create input buffer\n", stderr);
continue;
}
append(fs, fa);
// 发生读取错误
if(ferror(fs) != 0)
fprintf(stderr, "Error in reading file %s.\n", argv[i]);
// 发生写入错误
if(ferror(fa) != 0)
fprintf(stderr, "Error in writing file %s.\n", argv[1]);
fclose(fs);
files++;
printf("File %s appended.\n", argv[i]);
i++;
}
}
printf("Done. %d files appended.\n", files);
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0)
fwrite(temp, sizeof(char), bytes, dest);
}
我觉得没必要用循环吧!使用命令行参数不像交互式界面一样可以用循环表达的那么清楚, 这是我的第二次修改:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main(int argc, char *argv[])
{
FILE *fa, *fs;
int files = 0;
if(argc != 3)
{
printf("Usage: %s filename filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if((fa = fopen(argv[1], "a")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[1]);
exit(EXIT_FAILURE);
}
if(setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create output buffer\n", stderr);
exit(EXIT_FAILURE);
}
if(strcmp(argv[1], argv[2]) == 0)
{
fputs("Can't append file to itself\n", stderr);
exit(EXIT_FAILURE);
}
else if((fs = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "Can't open %s\n", argv[2]);
exit(EXIT_FAILURE);
}
else
{
if(setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)
{
fputs("Can't create input buffer\n", stderr);
exit(EXIT_FAILURE);
}
append(fs, fa);
if(ferror(fs) != 0)
fprintf(stderr, "Error in reading file %s.\n", argv[2]);
if(ferror(fa) != 0)
fprintf(stderr, "Error in writing file %s.\n", argv[1]);
fclose(fs);
files++;
printf("File %s appended.\n", argv[2]);
}
printf("Done. %d files appended.\n", files);
fclose(fa);
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes = fread(temp, sizeof(char), BUFSIZE, source)) > 0)
fwrite(temp, sizeof(char), bytes, dest);
}
6、
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
int main(void)
{
FILE *in, *out;
int ch;
char name1[LEN];
char name2[LEN];
int count = 0;
puts("Enter the name of the file to be processed: ");
gets(name1);
if((in = fopen(name1, "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", name1);
exit(2);
}
strcpy(name2, name1);
strcat(name2, ".red");
if((out = fopen(name2, "w")) == NULL)
{
fprintf(stderr, "Can't create output file.\n");
exit(3);
}
while((ch = getc(in)) != EOF)
if(count++ % 3 == 0)
putc(ch, out); // 打印每3个字符中的1个
if(fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closing files.\n");
return 0;
}
声明两个字符数组,没必要!
经过第二次修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
int main(void)
{
FILE *in, *out;
int ch;
char name[LEN];
int count = 0;
puts("请您输入要读取的文件名:");
gets(name);
if((in = fopen(name, "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", name);
exit(EXIT_FAILURE);
}
strcat(name, ".red");
if((out = fopen(name, "w")) == NULL)
{
fprintf(stderr, "Can't create output file.\n");
exit(EXIT_FAILURE);
}
while((ch = getc(in)) != EOF)
if(count++ % 3 == 0)
putc(ch, out);
if(fclose(in) != 0 || fclose(out) != 0)
fprintf(stderr, "Error in closing files\n");
return 0;
}
7、(参考
http://xiongyi85.blog.51cto.com)
//pe13-7a
#include <stdio.h>
#include <stdlib.h>
#define SIZE 256
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char ch1, ch2;
if(argc < 3)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if((fp1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[1]);
exit(1);
}
if((fp2 = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[2]);
exit(2);
}
// 不应该用fgets()/fputs()函数
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
while(ch1 != EOF || ch2 != EOF)
{
if(ch1 == EOF || ch2 == EOF)
putchar('\n');
while(ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = fgetc(fp1);
}
if(ch1 != EOF)
{
putchar('\n');
ch1 = fgetc(fp1);
}
while(ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = fgetc(fp2);
}
if(ch2 != EOF)
{
putchar('\n');
ch2 = fgetc(fp2);
}
}
/*while(fgets(line1, SIZE, fp1) != NULL || fgets(line2, SIZE, fp2) != NULL)
{
while(*line1 != '\n')
putchar(*line1++);
putchar('\n');
while(*line2 != '\n')
putchar(*line2++);
}*/
if(fclose(fp1) != 0 || fclose(fp2) != 0)
fprintf(stderr, "Error in closing files.\n");
return 0;
}
//pe13-7b
#include <stdio.h>
#include <stdlib.h>
#define SIZE 256
int main(int argc, char *argv[])
{
FILE *fp1, *fp2;
char ch1, ch2;
if(argc < 3)
{
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if((fp1 = fopen(argv[1], "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[1]);
exit(1);
}
if((fp2 = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[2]);
exit(2);
}
// 不应该用fgets()/fputs()函数
ch1 = fgetc(fp1);
ch2 = fgetc(fp2);
while(ch1 != EOF || ch2 != EOF)
{
if(ch1 == EOF || ch2 == EOF)
putchar('\n');
while(ch1 != EOF && ch1 != '\n')
{
putchar(ch1);
ch1 = fgetc(fp1);
}
// 不打印换行符,改为打印一个空格
if(ch1 != EOF)
{
putchar(' ');
ch1 = fgetc(fp1);
}
while(ch2 != EOF && ch2 != '\n')
{
putchar(ch2);
ch2 = fgetc(fp2);
}
if(ch2 != EOF)
{
putchar('\n');
ch2 = fgetc(fp2);
}
}
if(fclose(fp1) != 0 || fclose(fp2) != 0)
fprintf(stderr, "Error in closing files.\n");
return 0;
}
8、
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
char ch;
int i, count = 0;
if(argc == 2)
fp = stdin;
else
{
for(i = 2; i < argc; i++)
{
if((fp = fopen(argv[i], "r")) == NULL)
{
fprintf(stderr, "I couldn't open the file \"%s\"\n", argv[i]);
exit(1);
}
while((ch = fgetc(fp)) != EOF)
if(argv[1][0] == ch)
count++;
if(fclose(fp) != 0)
fprintf(stderr, "Error in closing files.\n");
printf("The number of characters %c in file %s: %d\n", argv[1][0], argv[i], count);
}
}
return 0;
}
没有fgetc()函数,只有fgets()函数,注意区分getc()、fgets()函数!!!
经第二次修改如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40
int main(int argc, char *argv[])
{
int ch;
int count = 0;
FILE *fp;
if(argc < 2)
{
fprintf(stderr, "Usage: %s char(or filename): \n", argv[0]);
exit(EXIT_FAILURE);
}
if(argc == 2)
{
printf("请您输入一些字符:\n");
while((ch = getchar()) != '\n')
{
if(ch == argv[1][0])
count++;
}
printf("字符%c出现的次数为:%d\n", argv[1][0], count);
}
else
{
for(int i = 2; i < argc; i++)
{
count = 0;
if((fp = fopen(argv[i], "r")) == NULL)
{
fprintf(stderr, "Can't open the file %s.\n", argv[i]);
exit(EXIT_FAILURE);
}
while((ch = getc(fp)) != EOF)
{
if(ch == argv[1][0])
count++;
}
if(fclose(fp) != 0)
printf("Error in closing files.\n");
printf("字符%c在文件%s中出现的次数为:%d\n", argv[1][0], argv[i], count);
}
}
return 0;
}
9、(妈的,理解错题意了,我还以为是在一个已经存在的文件继续追加单词,我说怎么列表顺序不是从1开始的,浪费太多时间了!参考,
http://ptgmedia.pearsoncmg.com/images/9780321928429/downloads/9780321928429_ProgrammingExerciseAnswers_Selected.pdf)注意审题!!!#include <stdio.h>
#include <stdlib.h>
#define MAX 40
int main(void)
{
FILE *fp;
char words[MAX];
int word_count = 0;
if((fp = fopen("wordy", "a+")) == NULL)
{
fprintf(stdout, "Can't open \"wordy\" file.\n");
exit(1);
}
/* 确定文件当前行数 */
rewind(fp); // 使程序回到文件开始处
while((fgets(words, MAX, fp)) != NULL)
word_count++;
rewind(fp); // 使程序回到文件开始处
puts("Enter words to add to the file; press the Enter");
puts("key at the beginning of a line to terminate.");
while(gets(words) != NULL && words[0] != '\0')
fprintf(fp, "%d:%s\n", ++word_count, words);
puts("File contents: ");
rewind(fp);
while(fgets(words, MAX, fp) != NULL)
fputs(words, stdout);
if(fclose(fp) != 0)
fprintf(stderr, "Error closing file\n");
return 0;
}
10、(做的想吐血,我真是受不了了!!!为什么我写的就不对呢?借鉴
http://xiongyi85.blog.51cto.com做法)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 40
int main(void)
{
FILE *fp;
char file[MAX];
long location, last;
char ch;
puts("Enter the name of the file to be processed: ");
gets(file);
// 以文本模式打开文件
if((fp = fopen(file, "r")) == NULL)
{
printf("reverse can't open %s\n", file);
exit(EXIT_FAILURE);
}
fseek(fp, 0L, SEEK_END);
last = ftell(fp);
printf("Please enter a file location (q to quit): \n");
while(scanf("%ld", &location) == 1)
{
if(location < last && location > 0)
{
fseek(fp, location - 1, SEEK_SET); // 找到文件的第location个字节处
while((ch = fgetc(fp)) != '\n') // 适用范围:只是对于第一行有效,而不能扩展至任意行
putchar(ch);
putchar('\n');
}
else
printf("out of range!\n");
printf("Please enter a file location (q to quit): \n");
}
// 为什么不能像下面这样写呢???
/*fseek(fp, location, SEEK_SET); // 找到文件的第location个字节处
for(count = 0L; count < last; count++)
{
fseek(fp, count, SEEK_CUR);
ch = getc(fp);
if(ch != '\n')
putchar(ch);
}*/
if(fclose(fp) != 0)
fprintf(stderr, "Error closing file\n");
return 0;
}
11、
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 40
int main(int argc, char *argv[])
{
FILE *fp;
char line[SIZE];
if(argc < 3)
{
fprintf(stderr, "Usage: %s string filename\n", argv[0]);
exit(EXIT_FAILURE);
}
if((fp = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "Can't open the file \"%s\"\n", argv[2]);
exit(EXIT_FAILURE);
}
while(fgets(line, SIZE, fp) != NULL)
if(strstr(line, argv[1]) != NULL)
fputs(line, stdout);
if(fclose(fp) != 0)
fprintf(stderr, "Error closing file\n");
return 0;
}
12、(实在做不出来,遂参考
http://ptgmedia.pearsoncmg.com/images/9780321928429/downloads/9780321928429_ProgrammingExerciseAnswers_Selected.pdf)#include <stdio.h>
#include <stdlib.h>
#define ROWS 20
#define COLS 30
#define LEVELS 10
const char trans[LEVELS + 1] = " .':~*=&%@";
// 初始化字符数组
void init(char arr[][COLS], char ch); // arr指向一个包含30个字符的数组
void makePic(char pic[][COLS], int data[][COLS], int row);
int main(void)
{
int row, col;
FILE *fp;
char ch[ROWS][COLS]; // 创建一个20*30的字符数组
int image[ROWS][COLS];
if((fp = fopen("wordy", "r")) == NULL)
{
fprintf(stderr, "Can't open the file \"wordy\"\n");
exit(EXIT_FAILURE);
}
// 将文件的内容读入到一个20*30的int数组中
for(row = 0; row < ROWS; row++)
for(col = 0; col < COLS; col++)
fscanf(fp, "%d", &image[row][col]); // scanf()函数忽略空格和换行符
// 如果发生读取错误
if(ferror(fp) != 0)
{
fprintf(stderr, "Error in reading file wordy.\n");
exit(EXIT_FAILURE);
}
makePic(ch, image, ROWS);
// 向屏幕输出图片
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
putchar(ch[row][col]);
putchar('\n');
}
if(fclose(fp) != 0)
fprintf(stderr, "Error closing file\n");
return 0;
}
void init(char arr[][COLS], char ch)
{
int r, c;
for(r = 0; r < ROWS; r++)
for(c = 0; c < COLS; c++)
arr[r][c] = ch;
}
//将data数组的值转换成trans中的符号后录入pic
void makePic(char pic[][COLS], int data[][COLS], int row)
{
int r, c;
for(r = 0; r < row; r++)
for(c = 0; c < COLS; c++)
pic[r][c] = trans[data[r][c]]; // 惊呆小伙伴了!!!
}
我的文件:
运行结果:
13、(参考
csdn---vs9841前辈的做法,但做的方法太笨了,应该还有简便方法的,不用考虑这9种情况的!因为太繁琐了!)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define ROWS 20
#define COLS 30
#define LEVELS 10
const char trans[LEVELS + 1] = " .':~*=&%@";
// 初始化字符数组
void init(char arr[][COLS], char ch); // arr指向一个包含30个字符的数组
// 消除尖峰脉冲
void eliminate(int row, int col, int data[][COLS]);
// 判断该值与它周围每个值的差都是否大于1
bool check(int n, int te[n]);
void makePic(char pic[][COLS], int data[][COLS], int row);
int main(void)
{
int row, col;
FILE *fp;
char ch[ROWS][COLS]; // 创建一个20*30的字符数组
int image[ROWS][COLS];
if((fp = fopen("wordy", "r")) == NULL)
{
fprintf(stderr, "Can't open the file \"wordy\"\n");
exit(EXIT_FAILURE);
}
// 将文件的内容读入到一个20*30的int数组中
for(row = 0; row < ROWS; row++)
for(col = 0; col < COLS; col++)
fscanf(fp, "%d", &image[row][col]); // scanf()函数忽略空格和换行符
// 如果发生读取错误
if(ferror(fp) != 0)
{
fprintf(stderr, "Error in reading file wordy.\n");
exit(EXIT_FAILURE);
}
eliminate(ROWS, COLS, image);
makePic(ch, image, ROWS);
// 向屏幕输出图片
for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
putchar(ch[row][col]);
putchar('\n');
}
if(fclose(fp) != 0)
fprintf(stderr, "Error closing file\n");
return 0;
}
void init(char arr[][COLS], char ch)
{
int r, c;
for(r = 0; r < ROWS; r++)
for(c = 0; c < COLS; c++)
arr[r][c] = ch;
}
void eliminate(int row, int col, int data[][COLS])
{
int r, c;
int temp[4] = {[1] = 0}; // 存放与上下左右相减的绝对值
for(r = 0; r < row; r++)
for(c = 0; c < col; c++)
{
if(r == 0)
{
if(c == 0) // 矩阵的左上角
{
temp[0] = abs(data[r][c] - data[r+1][c]);
temp[1] = abs(data[r][c] - data[r][c+1]);
if(check(2, temp))
data[r][c] = (data[r+1][c] + data[r][c+1]) / 2;
}
else if(c == col - 1) // 矩阵的右上角
{
temp[0] = abs(data[r][c] - data[r][c-1]);
temp[1] = abs(data[r][c] - data[r+1][c]);
if(check(2, temp))
data[r][c] = (data[r][c-1] + data[r+1][c]) / 2;
}
else // 矩阵的上边
{
temp[0] = abs(data[r][c] - data[r][c-1]);
temp[1] = abs(data[r][c] - data[r][c+1]);
temp[2] = abs(data[r][c] - data[r+1][c]);
if(check(3, temp))
data[r][c] = (data[r][c-1] + data[r][c+1] + data[r+1][c]) / 3;
}
}
else if(r == row - 1)
{
if(c == 0) // 矩阵的左下角
{
temp[0] = abs(data[r][c] - data[r-1][c]);
temp[1] = abs(data[r][c] - data[r][c+1]);
if(check(2, temp))
data[r][c] = (data[r-1][c] + data[r][c+1]) / 2;
}
else if(c == col - 1) // 矩阵的右下角
{
temp[0] = abs(data[r][c] - data[r-1][c]);
temp[1] = abs(data[r][c] - data[r][c-1]);
if(check(2, temp))
data[r][c] = (data[r-1][c] + data[r][c-1]) / 2;
}
else // 矩阵的下边
{
temp[0] = abs(data[r][c] - data[r-1][c]);
temp[1] = abs(data[r][c] - data[r][c-1]);
temp[2] = abs(data[r][c] - data[r][c+1]);
if(check(3, temp))
data[r][c] = (data[r-1][c] + data[r][c-1] + data[r][c+1]) / 3;
}
}
else if(c == 0) // 矩阵的左边
{
temp[0] = abs(data[r][c] - data[r-1][c]);
temp[1] = abs(data[r][c] - data[r][c+1]);
temp[2] = abs(data[r][c] - data[r+1][c]);
if(check(3, temp))
data[r][c] = (data[r-1][c] + data[r][c+1] + data[r+1][c]) / 3;
}
else if(c == col - 1) // 矩阵的右边
{
temp[0] = abs(data[r][c] - data[r-1][c]);
temp[1] = abs(data[r][c] - data[r][c-1]);
temp[2] = abs(data[r][c] - data[r+1][c]);
if(check(3, temp))
data[r][c] = (data[r-1][c] + data[r][c-1] + data[r+1][c]) / 3;
}
else // 矩阵的内部
{
temp[0] = abs(data[r][c] - data[r-1][c]);
temp[1] = abs(data[r][c] - data[r][c-1]);
temp[2] = abs(data[r][c] - data[r+1][c]);
temp[3] = abs(data[r][c] - data[r][c+1]);
if(check(4, temp))
data[r][c] = (data[r-1][c] + data[r][c-1] + data[r+1][c] + data[r][c+1]) / 4;
}
}
}
bool check(int n, int te[])
{
int i;
for(i = 0; i < n; i++)
if(te[i] <= 1)
return false;
return true;
}
//将data数组的值转换成trans中的符号后录入pic
void makePic(char pic[][COLS], int data[][COLS], int row)
{
int r, c;
for(r = 0; r < row; r++)
for(c = 0; c < COLS; c++)
pic[r][c] = trans[data[r][c]]; // 惊呆小伙伴了!!!
}
我的文件:
运行结果为:
至此,终于完成这章编程练习,真是不容易!!!