C PRIMER PLUS第六版 第十三章编程练习

复习题第五题代码

#include <stdio.h>
#include <stdlib.h>
#define BUF 256

int has_ch(char, char *);

int main(int argc, char * argv [])
{
    char ch;
    FILE * fp;
    char line[BUF];

    if(argc != 3)
    {
        fprintf(stderr,"Could not open %s file", argv[0]);
        exit(EXIT_FAILURE);
    }
    if((fp = fopen(argv[2], "r")) == NULL)
    {
        fprintf(stderr,"Could not open %s file", argv[2]);
        exit(EXIT_FAILURE);
    }
    else
    {
        ch = argv[1][0];
        while(fgets(line, BUF, fp) != NULL)
        {
            if(has_ch(ch, line))
                fputs(line, stdout);
        }
    }
    fclose(fp);

    return 0;
}

int has_ch(char ch, char * ar)
{
    while(*ar)
    {
        if(ch == *ar++)
            return 1;
    }
    return 0;
}

 

编程练习:

1.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 64

int main(void)
{
    int ch;
    FILE *fp;
    unsigned long count = 0;
    char name[SIZE];

    printf("Enter the name of the file : ");
    scanf("%s",name);

    if((fp = fopen(name, "r")) == NULL)
    {
        printf("Can't open %s\n",name);
        exit(EXIT_FAILURE);
    }
    while((ch = getc(fp)) != EOF)
    {
        putc(ch, stdout);
        count++;
    }
    fclose(fp);
    printf("\n");
    printf("File %s has %lu characters.\n",name,count);

    return 0;
}

 

2.

#include <stdio.h>
#include <stdlib.h>
#define COPY_SIZE 1024

void copy(FILE * , FILE *);

int main(int argc, char * argv [])
{
    FILE * sou, * tar;

    if(argc != 3)
    {
        fprintf(stderr, "Missing file or file out of boundary");
        exit(EXIT_FAILURE);
    }
    else
    {
        if((sou = fopen(argv[1], "rb")) == NULL || (tar = fopen(argv[2], "wb")) == NULL)
        {
            fprintf(stderr, "File open failed.");
            exit(EXIT_FAILURE);
        }
        else
        {
            copy(sou, tar);
        }

    }
    fclose(sou);
    fclose(tar);
    puts("Done.\n");

    return 0;
}

void copy(FILE * sou, FILE * tar)
{
    int temp[COPY_SIZE];

    fread(temp, sizeof(int), COPY_SIZE, sou);
    fwrite(temp, sizeof(int), COPY_SIZE, tar);
}

 

3.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define SIZE 1024
#define NAME_SIZE 64

int main(void)
{
    char temp[SIZE];
    char name[NAME_SIZE];
    char ch;
    int i = 0;
    FILE * sou;

    printf("Enter the name of the file : ");
    scanf("%s",name);
    if((sou = fopen(name, "r")) == NULL)
    {
        fprintf(stderr, "Could not open %s file.\n",name);
        exit(EXIT_FAILURE);
    }
    while((ch = getc(sou)) != EOF) //在读到文件结尾前把所有字母改为大写
    {
        ch = toupper(ch);
        temp[i] = ch;
        i++;
    }
    fclose(sou);  //先以读的方式把文件改为大写并拷贝到字符串数组中临时储存,关闭文件
    
    if((sou = fopen(name, "w")) == NULL) //再以写的方式打开文件,文件原本内容被清空,但内容副本被保存在字符串数组中
    {
        fprintf(stderr, "Could not open %s file.\n",name);
        exit(EXIT_FAILURE);
    }
    fprintf(sou, "%s", temp);  //再把字符串数组中的内容拷贝到原文件中
    fclose(sou);
    puts("Done, your string has been updated.");

    return 0;
}

 

4.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 256

int main(int argc, char * argv [])
{
    FILE *fp;
    int i = 1;
    char temp[SIZE];

    while(i < argc)
    {
        if((fp = fopen(argv[i], "r")) == NULL)
        {
            fprintf(stderr, "Could not open the file %s", argv[i]);
            exit(EXIT_FAILURE);
        }
        printf("%s : \n", argv[i]);
        while(fgets(temp, SIZE, fp) != NULL); //fgets在遇到EOF时返回NULL,否则返回第一个参数地址
        {
            fputs(temp, stdout);
        }
        fclose(fp);
        i++;
        printf("\n");
    }
    printf("***Done!This is the end!***\n");

    return 0;
}

 

5.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 4096

void append(FILE *source, FILE *dest);

int main(int argc, char * argv [])
{
    FILE *fa, *fs;
    int files = 0;
    int ch;
    int i = 2;

    if((fa = fopen(argv[1], "a+")) == NULL)
    {
        fprintf(stderr, "Can't open %s\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    if(setvbuf(fa, NULL, _IOFBF, BUFFSIZE) != 0)
    {
        fputs("Can't create output buffer\n",stderr);
        exit(EXIT_FAILURE);
    }
    while(i < argc)
    {
        if(strcmp(argv[1], argv[i]) == 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
        {
            if(setvbuf(fa, NULL, _IOFBF, BUFFSIZE) != 0)
            {
                fputs("Can't create input buffer\n", stderr);
                i++;
                continue;
            }
            append(fs, fa);
            if(ferror(fs) != 0)
                fprintf(stderr,"Error in reading file %s", argv[i]);
            if(ferror(fa) != 0)
                fprintf(stderr,"Error in writing file %s", argv[1]);
            fclose(fs);
            files++;
            printf("File %s appended.\n", argv[i]);
        }
        i++;
    }
    printf("Done appending. %d files appended.\n", files);
    rewind(fa);
    printf("%s contents:\n", argv[1]);
    while ((ch = getc(fa)) != EOF)
        putchar(ch);
    putchar('\n');
    puts("Done displaying!\n");
    fclose(fa);

    return 0;
}

void append(FILE *source, FILE *dest)
{
    static char temp[BUFFSIZE];

    while (fgets(temp, BUFFSIZE, source) != NULL)
    {
        fputs(temp, dest);
    }
}

 

6.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 40

int main(void)
{
    FILE *in, *out;
    int ch;
    char sou_name[LEN] = {0};
    char tar_name[LEN] = {0};
    int count = 0;

    printf("Enter the name of the source file : ");
    scanf("%s", sou_name);
    if((in = fopen(sou_name, "r")) == NULL)
    {
        fprintf(stderr, "I couldn't open the file \"%s\"\n");
        exit(EXIT_FAILURE);
    }
    //设置输出
    strncpy(tar_name, sou_name, LEN-5);
    tar_name[LEN-5] = '\0';
    strcat(tar_name, ".red");
    if((out = fopen(tar_name, "w")) == NULL)
    {
        fprintf(stderr, "Can't create output file.\n");
        exit(3);
    }
    //拷贝数据
    while((ch = getc(in)) != EOF)
    {
        if(count % 3 == 0)
            putc(ch, out);
        count++;
    }
    if(fclose(in) != 0 || fclose(out) != 0)
        fprintf(stderr, "Error in closing files.\n");

    return 0;
}

 

7.

a.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 256

int main(int argc, char * argv [])
{
    FILE * file1, *file2;
    int i = 1;

    if(argc != 3)
    {
        fprintf(stderr,"Wrong number of files.\n");
        exit(EXIT_FAILURE);
    }
    if((file1 = fopen(argv[1], "r")) == NULL)
    {
        fprintf(stderr,"Can't open file1.\n");
        exit(EXIT_FAILURE);
    }
    if((file2 = fopen(argv[2], "r")) == NULL)
    {
        fprintf(stderr,"Can't open file2.\n");
        exit(EXIT_FAILURE);
    }

    while(feof(file1) == 0 || feof(file2) == 0)
    {
        printf("This is the %d circle\n",i);
        char line_temp1[LEN];
        char line_temp2[LEN];  //定义两个自动变量,在while循环一次后,自动释放,然后再重新分配内存
        if((fgets(line_temp1, LEN, file1)) != NULL)
            fputs(line_temp1, stdout);
        if((fgets(line_temp2, LEN, file2)) != NULL)
            fputs(line_temp2, stdout);
        i++;
    }
    fclose(file1);
    fclose(file2);

    return 0;
}

 

b.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 256

int main(int argc, char * argv [])
{
    FILE * file1, *file2;
    int i = 1;

    if(argc != 3)
    {
        fprintf(stderr,"Wrong number of files.\n");
        exit(EXIT_FAILURE);
    }
    if((file1 = fopen(argv[1], "r")) == NULL)
    {
        fprintf(stderr,"Can't open file1.\n");
        exit(EXIT_FAILURE);
    }
    if((file2 = fopen(argv[2], "r")) == NULL)
    {
        fprintf(stderr,"Can't open file2.\n");
        exit(EXIT_FAILURE);
    }

    while(feof(file1) == 0 || feof(file2) == 0)
    {
        int len;
        printf("This is the %d circle\n",i);
        char line_temp1[LEN];
        char line_temp2[LEN];  //定义两个自动变量,在while循环一次后,自动释放,然后再重新分配内存
        if((fgets(line_temp1, LEN, file1)) != NULL)
        {
            len = strlen(line_temp1);
            line_temp1[len - 1] = '\0';
            fputs(line_temp1, stdout);
        }
        if((fgets(line_temp2, LEN, file2)) != NULL)
            fputs(line_temp2, stdout);
        i++;
    }
    fclose(file1);
    fclose(file2);

    return 0;
}

 

8.

#include <stdio.h>
#include <stdlib.h>

int find_ch(char, FILE *);

int main(int argc, char * argv [])
{
    char ch = argv[1][0];
    int count = 0;
    int i = 2;
    FILE *fp;

    if(argc < 2)
    {
        fprintf(stderr, "Missing arguments");
        exit(EXIT_FAILURE);
    }
    else if(argc == 2)
    {
        count = find_ch(ch, stdin); //这种情况下,需要在stdin输入后手动Control + Z来结束输入,也可以用字符串数组来接手外部输入
        printf("In your input, there are %d %c%c.\n", count, ch, (count > 1)?'s':'\0');
    }
    else
    {
        while(i < argc)
        {
            if((fp = fopen(argv[i], "r")) == NULL)
            {
                fprintf(stderr, "Can't open the file.\n");
                exit(EXIT_FAILURE);
            }
            count += find_ch(ch, fp);
            i++;
        }
        printf("In the %d file%c, there are totally %d %c%c.\n", argc-2, (argc-2 > 1)?'s':'\0', count, ch,(count > 1)?'s':'\0' );
        fclose(fp);
    }
    printf("Done.\n");

    return 0;
}

int find_ch(char ch, FILE * fp)
{
    char file_ch;
    int ct = 0;

    while((file_ch = getc(fp)) != EOF)
    {
        if(file_ch == ch)
        {
            ct++;
        }
    }

    return ct;
}

 

9.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 41

int main(void)
{
    FILE *fp;
    char words[MAX];
    int num = 1;


    if((fp = fopen("wordy", "a+")) == NULL) //以在末尾添加内容模式打开wordy
    {
        fprintf(stdout, "Can't open \"wordy\" file.\n");
        exit(EXIT_FAILURE);
    }

    rewind(fp);
    while(fgets(words, MAX, fp) != NULL)
        num++;

    puts("Enter words to add to the file; press the #");
    puts("Key at the beginning of a line to terminate.");
    while((fscanf(stdin, "%40s", words)) == 1 && (words[0] != '#'))
        fprintf(fp, "%d : %s\n", num++, words);

    puts("File contents:");
    rewind(fp);
    while(fgets(words, MAX, fp) != NULL)
        fputs(words, stdout);
    puts("Done!");
    if(fclose(fp) != 0)
        fprintf(stderr,"Error closing file\n");

    return 0;
}

 

10.

#include <stdio.h>
#include <stdlib.h>
#define SIZE 64

int main(void)
{
    long int dis;
    char ch;
    char name[SIZE];
    FILE *fp;

    printf("Enter the name of the file you want to open : ");
    scanf("%s",name);

    if((fp = fopen(name, "r")) == NULL)
    {
        fprintf(stderr, "Can't open this file.\n");
        exit(EXIT_FAILURE);
    }
    printf("Enter the location of file, we will print from that location :");
    scanf("%ld",&dis);

    fseek(fp, dis, SEEK_SET);
    while((ch = getc(fp)) != '\n')
    {
        putchar(ch);
    }

    return 0;
}

 

11.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256

int main(int argc, char * argv [])
{
    char compare[SIZE];
    FILE *fp;

    if(argc != 3)
    {
        fprintf(stderr, "Wrong cmd parameters.\n");
        exit(EXIT_FAILURE);
    }
    if((fp = fopen(argv[2], "r")) == NULL)
    {
        fprintf(stderr, "Can't open the file.\n");
        exit(EXIT_FAILURE);
    }
    while(fgets(compare, SIZE, fp) != NULL)
    {
        if(strstr(compare, argv[1])) //strstr(str1,str2)是在str1中查找str2出现的位置,如果存在,返回第一次出现的位置,否则返回NULL
        {
            fputs(compare, stdout);
        }
    }
    fclose(fp);

    return 0;
}

 

12.

文档不好输入,太多了,先写一个程序生成随机数的20*30的文件,如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    FILE *fp;
    int sou_ar[60];
    int i;
    int j = 0;

    srand((unsigned int)time(0));
    fp = fopen("wordy", "w");
    while(j < 20)
    {
        for(i = 0; i < 59; i = i+2)
        {
            sou_ar[i] = rand() % 10;
            sou_ar[i + 1] = ' ';
        }
        sou_ar[59] = '\0';
        for(i = 0; i < 59; i += 2)
        {
            fprintf(fp, "%d ", sou_ar[i]);
        }
        fprintf(fp,"\n");
        j++;
    }
    fclose(fp);

    return 0;
}

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值