C语言第16周作业(文件打开、关闭,读写)

1.

#include 
#include 
#include 
#define FILE_NAME "name.txt"

int main(){
     
	FILE *fp=fopen(FILE_NAME,"r");
	if(NULL==fp){
     
		printf("Failed to read the file %s \n",FILE_NAME);
		exit(-1);
	}
	int ch=fgetc(fp);
	int ch1;
	printf("Name:");
    while(ch!=EOF){
     
		ch1 = ch;
		if(ch1=='\n'){
     
			printf("\nName:");
		}
		ch = fgetc(fp);
		//printf("%d\n",ch1);
		if(ch1==9&&isdigit(ch)) printf("\nAddress:");
		if(ch1!='\n') putchar(ch1);
		if(ch1=='.'&&ch==9){
     
			printf("\nCity,State:");
		}
	}
	fclose(fp);
	return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第1张图片

2.

#include 
#include 
#include 
#include 
#define FILE_NAME "wage.txt"

int main(){
     
    FILE *fp = fopen(FILE_NAME,"r");
    if(fp == NULL){
     
        printf("Can't open %s\n",FILE_NAME);
        exit(EXIT_FAILURE);
    }
    printf("Name\t   Social Security Number   Total revernue\n");
    int ch1,ch=fgetc(fp);
    while(ch!=EOF){
     
        ch1 = ch;
        ch = fgetc(fp);
        if(isdigit(ch1)&&ch=='.'){
     
            int sum = 0;
            while (ch!='\n'){
     
                if(isdigit(ch)){
     
                    sum = sum*10 +(ch-48);
                    //printf("sum = %d\n",sum);
                }
                ch = fgetc(fp);
            }
            double para = ch1-48+(sum/100)/100.0;
            printf("\t%.1lf",para*(sum%100));
        }
        putchar(ch1);
    }
    fclose(fp);

    return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第2张图片

3.

#include 
#include 
#define FILE_NAME "text.dat"

int main(){
     
    FILE *fp = fopen(FILE_NAME,"r");
    if(fp == NULL){
     
        printf("Can't open %s\n",FILE_NAME);
        exit(EXIT_FAILURE);
    }
    char ch = fgetc(fp);
    int cnt = 0;
    while(ch!=EOF){
     
        if(cnt%2==0) putchar(ch);
        ch = fgetc(fp);
        cnt++;
    }
    fclose(fp);

    return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第3张图片

//text.dat
hello, everyday! Today I will give a presentation about covid-19.

4.

//输入之前数据
30 60 40 80 90 120 150 130 160 170
#include 
#include 
#define FILE_NAME "Pollen.dat"

int main(){
     
    int num[10];
    int number;
    printf("enter a new number:");
    scanf("%d",&number);
    FILE *fp = fopen(FILE_NAME,"r");
    if(fp == NULL){
     
        printf("Can't open %s\n",FILE_NAME);
        exit(EXIT_FAILURE);
    }
    int i = 0,total = 0;
    while(!feof(fp)){
     
        fscanf(fp,"%d",&num[i]);
        //printf("%d\n",total);
        total +=num[i];
        i++;
    }
    //printf("%d\n",total);
    fclose(fp);

    for(i=0;i<9;i++){
     
        num[i] = num[i+1];
    }
    num[9] = number;
    for(i=0;i<10;i++){
     
        printf("%d\n",num[i]);
    }
    
    fp = fopen(FILE_NAME,"w");
    if(fp == NULL){
     
        printf("Can't open %s\n",FILE_NAME);
        exit(EXIT_FAILURE);
    }
    printf("The average for the past ten days is %d\n",total/10);
    total = total -num[0]+num[9];
    printf("The average for the last ten days is %d\n",total/10);
    for(i=0;i<10;i++){
     
        fprintf(fp,"%d ",num[i]);
    }
    fclose(fp);

    return 0;
}
//输入190之后的数据
60 40 80 90 120 150 130 160 170 190 

C语言第16周作业(文件打开、关闭,读写)_第4张图片

5.

#include
#include

int main(int argc, char* argv[])
{
     
	if(argc!=2){
     
		printf("Usage: ./fs filename\n");
		exit(-1);
	}
	FILE *rf=fopen(argv[1],"r");
	if(NULL==rf){
     
		printf("Failed to read the file %s \n",argv[1]);
		exit(-1);
	}
	int ch;
	int i=0;
	long offset, last;
	fseek(rf,0L,SEEK_END);
	last=ftell(rf);
	for(offset=0;offset<=last;offset++){
     
		fseek(rf,-offset,SEEK_END);
		ch=fgetc(rf);
		switch(ch){
     
			case '\n':break;
			case EOF: break;
			default:printf("%c",ch);break;
		}
	}
	printf("\n");
	fclose(rf);
	return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第5张图片

6.

3.
#include 
#include 

int main(int argc,char *argv[]){
     
    char str[100];
    int num;
    printf("the number of files is:");
    scanf("%d",&num);
    FILE *fp;
    if(argc!=num+1){
     
        printf("usage:canopen filename\n");
        exit(EXIT_FAILURE);
    }
    for(int i=1;i<num+1;i++){
     
        if((fp = fopen(argv[i],"r"))==NULL){
     
            printf("%s can't be opened\n",argv[1]);
            exit(EXIT_FAILURE);
        }
        int ch=fgetc(fp);
        while(ch!=EOF){
     
            putchar(ch);
            ch = fgetc(fp);
        }
        fclose(fp);
    }

    return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第6张图片
命令行下输入3个文件的名字,依次打开并打印之后关闭。
C语言第16周作业(文件打开、关闭,读写)_第7张图片
如果给出的文件数目与所要的文件数目不对应,程序会停止。

4.
#include 
#include 
#include 

int main(int argc,char *argv[]){
     
    FILE *fp = fopen(argv[1],"r");
    if(fp == NULL){
     
        printf("Can't open %s\n",argv[1]);
        exit(EXIT_FAILURE);
    }
    int cnt = 0,cnt_line=0,cnt_word=0;
    char ch=fgetc(fp);
    while(ch!=EOF){
     
        if(isalpha(ch)){
     
            cnt++;
        }
        if(ch=='\n'){
     
            cnt_line++;
        }
        if(ch==' '){
     
            cnt_word++;
        }
        putchar(ch);
        ch = fgetc(fp);
    }
    fclose(fp);
    printf("\n\nthe number of characters are %d\n",cnt);
    printf("the number of lines are %d\n",cnt_line+1);
    printf("the number of words are %d\n",cnt_word+1);

    return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第8张图片

7.

//txt文件 "telephone.txt"
404.817.6900
(215) 686-1776
312-746-6000
877 275 5273
6173434200
#include 
#include 
#include 
#define FILE_NAME "telephone.txt"

int main(){
     
    FILE *fp = fopen(FILE_NAME,"r");
    if(fp == NULL){
     
        printf("Can't open %s\n",FILE_NAME);
        exit(EXIT_FAILURE);
    }
    int cnt_line = 0;
   char ch=fgetc(fp);
    while(ch!=EOF){
     
        if(ch=='\n'){
     
            cnt_line++;
        }
        //putchar(ch);
        ch = fgetc(fp);
    }
    //fclose(fp);
    //fp = fopen(FILE_NAME,"r");
    rewind(fp);

    for(int k=0;k<cnt_line+1;k++){
     
        char str[100];
        int i = 0,j = 0;
        fgets(str,sizeof(str),fp);
        printf("(");
        while(i<10){
     
            if(isdigit(str[j])){
     
                printf("%c",str[j]);
                i++;
                if(i==3) printf(") ");
                if(i==6) printf("-");
            }
            j++;
        }
        printf("\n");
    }
    //printf("%s",str);
    fclose(fp);
    return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第9张图片

8.

#include
#include
#include

#define L 10

int main(int argc, char*argv[])
{
     
	if(argc!=2)
	{
     
		printf("Usage: ./br filename\n");
		exit(EXIT_FAILURE);
	}
	FILE *rf=fopen(argv[1],"rb");//rb是必需的
	if(NULL==rf)
	{
     
		printf("Failed to open the file %s.\n",argv[1]);
		exit(EXIT_FAILURE);
	}
	unsigned char buffer[L];//unsigned是必需的
	int j=0;
	printf("offset");
	printf("\t");
	printf("            Bytes            ");
	printf("\t");
	printf("Characters\n");
	printf("------");
	printf("\t");
	printf("-----------------------------");
	printf("\t");
	printf("----------\n");
	while(1)
	{
     
		int n=fread(buffer,sizeof(unsigned char),L,rf);
		//printf("n=%d\n",n);
		if(n<=L)
		{
     
			printf("%6d\t",j);
			j+=L;
			int i;
			for(i=0;i<n;i++)
			{
     
				printf("%02X",buffer[i]);
				if(i<n-1)
					printf(" ");
			}
			if(n<L)
			{
     
				for(i=n;i<L-1;i++)
					printf("   ");
				printf("  ");
			}
			printf("\t");
			for(i=0;i<n;i++)
			{
     
				if(isprint(buffer[i]))
					printf("%c",buffer[i]);
				else
					printf(".");
			}
			printf("\n");
			if(n<L)
			{
     
				break;
			}

		}
	}
	fclose(rf);
	return 0;
}

C语言第16周作业(文件打开、关闭,读写)_第10张图片

你可能感兴趣的:(笔记,templates,onvif,stream)