Skip to content

Latest commit

 

History

History
304 lines (254 loc) · 6.18 KB

[题解]十、文件.md

File metadata and controls

304 lines (254 loc) · 6.18 KB

十、文件

编程题

将短句“Programming is fun!”写入文件f1.txt.

【问题描述】将短句“Programming is fun!”写入文件f1.txt. 【输入形式】无 【输出形式】文件f1.txt,内容是:Programming is fun! 【样例输入】 【样例输出】

Programming is fun!

【样例说明】注意空格。 【评分标准】无

#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE *fp;
    fp=fopen("f1.txt","w");
    fprintf(fp,"Programming is fun!");
    fclose(fp);
}

从键盘输入一行字符,写到文件f3.txt中。

【问题描述】从键盘输入一行字符,写到文件f3.txt中。 【输入形式】键盘输入一行字符 【输出形式】文件f3.txt 【样例输入】

I love programming!

【样例输出】

文件f3.txt内容:I love programming!

【样例说明】请注意是一行字符。 【评分标准】无

#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE *fp;
    fp=fopen("f3.txt","w");
    char s[100];
	int i=0;
	while((s[i]=getchar())!='\n') 
        i++;
    s[i]='\0';
    fprintf(fp,"%s",s);
    fclose(fp);
}

统计文件的行数

【问题描述】读lx12_3.txt文件,显示在屏幕上,如果有大写字母,则改成小写字母再输出,并统计行数。根据回车符统计文件的行数。 【输入形式】读文件 【输出形式】输出到屏幕 【样例输入】文件内容如

A
bC

【样例输出】

a
bc
line is 2

【样例说明】 【评分标准】

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
int main()
{
    FILE *fp;
    int line=0;
    char ch;
    fp=fopen("lx12_3.txt","r");
    while (!feof(fp))
    {
        ch=fgetc(fp);
        if (ch!=EOF)
        {
            if (isupper(ch))    printf("%c", tolower(ch));
            else if (islower(ch))    printf("%c",ch);
            if (ch=='\n')
            {
                line++;
                printf("\n");
            }
        }
    }
    printf("Line is %d",line);
    fclose(fp);
    
}

字符串替换(新)

【问题描述】 编写程序将一个指定文件中某一字符串替换为另一个字符串。要求:(1)被替换字符串若有多个,均要被替换;(2)指定的被替换字符串,大小写无关。 【输入形式】 给定文件名为filein.txt。从控制台输入两行字符串(不含空格,行末尾都有回车换行符),分别表示被替换的字符串和替换字符串。 【输出形式】 将替换后的结果输出到文件fileout.txt中。 【样例输入】 从控制台输入两行字符串:

in
out

文件filein.txt的内容为:

#include <stdio.h>
void main()
{
    FILE * IN;
    if((IN=fopen("in.txt","r"))==NULL)
    {
       printf("Can’t open in.txt!");
       return;
    }
    fclose(IN);
}

【样例输出】 文件fileout.txt的内容应为:

#outclude <stdio.h>
void maout()
{
    FILE * out;
    if((out=fopen("out.txt","r"))==NULL)
    {
       prouttf("Can’t open out.txt!");
       return;
    }
    fclose(out);
}

【样例说明】 输入的被替换字符串为in,替换字符串为out,即将文件filein.txt中的所有in字符串(包括iN、In、IN字符串)全部替换为out字符串,并输出保存到文件fileout.txt中。 【评分标准】 该题要求得到替换后的文件内容,共有5个测试点。上传C语言文件名为replace.c。

#include<stdio.h>
#include<string.h>
void str_replace(char *cp, int n, char *str);
char *Strstr(char *a,const char *b);
int main()
{
    char string_1[10240],string_2[10],string_3[10];
		scanf("%s",string_2);
		scanf("%s",string_3);
    int i=0;
    char c;
   	char *p;
    FILE *fp;											//定义文件指针
	fp=fopen("filein.txt","r+");						//打开文本文件进行读/写
	while(!feof(fp)){
		c=fgetc(fp);
        string_1[i] = c;
		i++;
	}
	string_1[i]='\0';									//将文本文件写入string_1
	p=Strstr(string_1,string_2);						//开始查找字符串
		while(p){
			str_replace(p,strlen(string_2),string_3);	//每找到一个字符串,就替换
			p=p+strlen(string_3);
			p=Strstr(string_1,string_2);
		}
    string_1[strlen(string_1)-1]=0;						//将字符串string_1末尾字符修改为结束符
	FILE *fp_out;
	fp_out=fopen("fileout.txt","w");
	fprintf(fp_out,"%s",string_1);							//将修改后的string_1写入文件
	fclose(fp_out);
	fclose(fp);
}
void str_replace(char *cp, int n, char *str)
{
	int len_of_str;
	char * tmp;
	len_of_str=strlen(str);
	//str3比str2短,往前移动
	if(len_of_str<n){
		tmp=cp+n;
		while(*tmp){
			*(tmp-(n-len_of_str))=*tmp; //n-len_of_str是移动的距离
			tmp++;
		}
		*(tmp-(n-len_of_str))=*tmp; //move '\0'
	}
	else
		//str3比str2长,往后移动
		if(len_of_str>n){
			tmp=cp;
			while(*tmp)tmp++;
			while(tmp>=cp+n){
				*(tmp+(len_of_str-n))=*tmp;
				tmp--;
			}
		}
	strncpy(cp,str,len_of_str);
}
char *Strstr(char *a,const char *b)
{
    int len_b=strlen(b);
    while(*a)
    {
        if(strncasecmp(a,b,len_b)==0)
            return a;
        a++;
    }
    return NULL;
}

将文本文件test.txt中所有包含字符串 for 的行输出

【问题描述】编写一程序,用于将文本文件test.txt中所有包含字符串 for 的行输出 【输入形式】文件 【输出形式】屏幕,包含字符串 for 的行输出 【样例输入】文件test.txt内容:

I love programming! 
I love my country !
Thank you very much for your favor!

【样例输出】

The row including 'for' is :3
Thank you very much for your favor!

【样例说明】注意输出中的空格 【评分标准】无

#include <stdio.h>
#include <string.h>
int main(){
	char str[200],str1[4]="for";
	FILE *fp;
	int i=0;
	fp=fopen("test.txt","r");
	while (fgets(str,200,fp)){
		i++;
		if(strstr(str,str1)){
			printf("The row including 'for' is : %d\n",i);
			printf("%s",str);
		}
	}
	fclose(fp);
}