2019.4.1《C Primer Plus》第十三章部分编程练习,一个不想填的坑……

从第十四章滚回来看第十三章,做几道课后题熟悉熟悉……

13.11.1

/* count.c -- using standard I/O */
#include <stdio.h>
#include <stdlib.h> // exit() prototype
#include<string.h>
char *s_gets(char *st,int n);
int main(void)
{
	char file[20];
    int ch;         // place to store each character as read
    FILE *fp;       // "file pointer"
    unsigned long count = 0;
    s_gets(file,20);
//    if ()
//    {
//        printf("Usage: %s filename\n", argv[0]);
//        exit(EXIT_FAILURE);
//    }
    if ((fp = fopen(file, "r")) == NULL)
    {
        printf("Can't open %s\n", file);
        exit(EXIT_FAILURE);
    }
    while ((ch = getc(fp)) != EOF)
    {
        putc(ch,stdout);  // same as putchar(ch);
        count++;
    }
    fclose(fp);
    printf("File %s has %lu characters\n", file, count);
    
    return 0;
}

char *s_gets(char *st,int n)
{
	char *ret_val;
	char *find;
	
	ret_val=fgets(st,n,stdin);
	if(ret_val)
	{
		find=strchr(st,'\n');
		if(find)
			*find='\0';
		else
			while(getchar()!='\n')
				continue;
		return ret_val;
	}
}

13.11.2

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char *argv[])
{
	char ch;
	FILE *fp1,*fp2;
	if((fp1=fopen(argv[1],"rb"))==NULL)//fp1指向源文件
	{
		printf("Can't open %s.\n",argv[0]);
		exit(EXIT_FAILURE);
	}
	if((fp2=fopen(argv[2],"ab+"))==NULL)//fp2指向目标文件
	{
		fprintf(stderr,"Can't open %s.\n",argv[2]);
		exit(EXIT_FAILURE);
	}
	while(ch=getc(fp1)!=EOF){
		putc(ch,fp2);
	}
	fclose(fp1);
	fclose(fp2);
	printf("%s copy completed.\n");
	
	return 0;
}

13.11.3

/*要对同一个文件进行操作……缓冲区可能不够大…… 不会删除临时文件和改名。
所以操作方法为读入一个字符,转换,用fseek()使指针退一步,写入*/ 
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define SLEN 81
char * s_gets(char *st,int n);
int main(void)
{
	FILE *fp;
	char file[SLEN];
	int ch;
	
	puts("Enter the name of the file you want to convert:");
	s_gets(file,SLEN);
	if((fp=fopen(file,"r+"))==NULL)
	{
		fprintf(stderr,"Can't open %s.\n",file);
		exit(EXIT_FAILURE);
	}
	fseek(fp,0L,SEEK_SET);
	while((ch=getc(fp))!=EOF){//字符与整数比较,ch被带符号(signed)扩展为0xFFFFFFFF,https://blog.csdn.net/flyyyri/article/details/5084981
		fseek(fp,-1L,SEEK_CUR);//
		putc(toupper(ch),fp);
		fseek(fp,0L,SEEK_CUR);//fseek可以用来清除_IOREADING与_IOWRITING,读文件时文件在写状态的话为错误,http://bbs.chinaunix.net/thread-670120-1-1.html 
	}
//	while(!feof(fp))//内部指针指向结尾时,还要执行一次读操作,文件结束标记才置位
//	{
//		ch = fgetc(fp);
//		if(ch >= 97 && ch <= 122)
//		{
//		    fseek(fp,-1,SEEK_CUR); //读入一个需要改变的字符,位置是在其后. 所以需要将位置从当前位向前走一位
//		    fputc(ch - 32,fp);
//		    fseek(fp,0,SEEK_CUR);  //在改写完一个字符后,需要使用fseek来改变读写状态,这句没有做位置的移动
//		}
//	}
	if(fclose(fp)!=0)
	{
		printf("关闭文件错误\n");
		exit(EXIT_FAILURE);
	}
	puts("Done.");
	system("pause"); 
	return 0;
}
char * s_gets(char *st,int n)
{
	char *ret_val;
	char *find;
	ret_val=fgets(st,n,stdin);
	if(ret_val)
	{
		find=strchr(st,'\n');
		if(find)
			*find='\0';
		else
			while(getchar()!='\n')
				continue;
	}
	return ret_val;
}

这道题满奇怪的,翻了些博客……
fclose()与fgetc()对于EOF的讲解,但0xFF有对应的ASCII码嘛?
fseek()消除读/写的影响的讨论

13.11.4

#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[])
{
	FILE *fp;
	char ch;
	int i;
	for(i=0;i<argc-1;i++){
		if((fp=fopen(argv[i+1],"r"))==NULL){
			puts("Can't open.");
			exit(EXIT_FAILURE);
		}
		printf("The content of the No.%d as follows...\n",i+1);
		while((ch=fgetc(fp))!=EOF)
			putchar(ch);
		putchar('\n');
		putchar('\n');
	}
	puts("Done.");
	return 0;
}

13.11.5

/* append.c -- appends files to a file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 4096
#define SLEN 81
#define file_app argv[1]
#define file_src argv[2]
void append(FILE *source, FILE *dest);
char * s_gets(char * st, int n);

int main(int argc,char* argv[])
{
    FILE *fa, *fs;	// fa for append file, fs for source file
    int files = 0;  // number of files appended
//    char file_app[SLEN];  // name of append file
//    char file_src[SLEN];  // name of source file
    int ch;
    
//    puts("Enter name of destination file:");
//    s_gets(file_app, SLEN);
    if ((fa = fopen(file_app, "a+")) == NULL)
    {
        fprintf(stderr, "Can't open %s\n", file_app);
        exit(EXIT_FAILURE);
    }
    if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)
    {
        fputs("Can't create output buffer\n", stderr);
        exit(EXIT_FAILURE);
    }
//    puts("Enter name of first source file (empty line to quit):");
	int i;
    for(i=2;i<=argc;i++)
    {
        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
        {
            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 appending. %d files appended.\n", files);
    rewind(fa);
    printf("%s contents:\n", file_app);
    while ((ch = getc(fa)) != EOF)
        putchar(ch);
    puts("Done displaying.");
    fclose(fa);
    
    return 0;
}

void append(FILE *source, FILE *dest)
{
    size_t bytes;
    static char temp[BUFSIZE]; // allocate once
    
    while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) > 0)
        fwrite(temp, sizeof (char), bytes, dest);
}

char * s_gets(char * st, int n)
{
    char * ret_val;
    char * find;
    
    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');   // look for newline
        if (find)                  // if the address is not NULL,
            *find = '\0';          // place a null character there
        else
            while (getchar() != '\n')
                continue;
    }
    return ret_val;
}

这么些好像有点取巧……

13.11.6

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define LEN 40
char* s_gets(char *st,int n);
int main(void)
{
	FILE *in, *out;
	int ch;
	int count=0;
	char name1[LEN],name2[LEN];
	puts("Enter the name of the file you want toreduce.");
	s_gets(name1,LEN);
	if((in=fopen(name1,"r"))==NULL){
		puts("Can't open.");
		exit(EXIT_FAILURE);
	}
	strncpy(name2,name1,LEN -5);
	name2[LEN-5]='\0';//该空字符被strcat覆盖
	strcat(name2,".red");
	if((out=fopen(name2,"w"))==NULL){
		fprintf(stderr,"Can't creat output file.\n");
		exit(3);
	}
	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;
}
char* s_gets(char *st,int n)
{
	char *ret;
	char *find;
	ret=fgets(st,n,stdin);
	if(ret)
	{
		if((find=strchr(st,'\n'))!=NULL)
			*find='\0';
		else
			while(getchar()!='\n')
				continue;
	}
	return ret;
}

2019年4月14日10点47分
今天写到这里吧,洗澡去了
文件可以告一段落了……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值