一道c语言编程?

编写程序,在一个C语言的源文件中的第一行寻找字符串“#include "file1.h"”,若没有找到,给出提示,否则,将该字符串替换成file1.h中的全部文本另存起来。(注:源文件和文件file1.h请使用记事本先创建好,源文件中至多只有一个“#include "file1.h"”,file1.h文件内容请自定义)如图求教

根据你写得要求:

一、头文件file1.h中放了一个函数申明语句。源文件中放了函数。

二、根据头文件的include语句解析字符串获取头文件名,与头文件默认路径(常量)组成文件路径。同字符串的匹配找到在语句在源文件的位置,并读取头文件内容替换到源文件对应位置。

三、合并后的内容,我是写入新的文件中,如你想要覆盖同文件,自行修改路径常量。

#include <stdio.h>

#include <malloc.h>

#include <string.h>

#define NN "C:\\newFile.c"//合并后的文件完整路径

#define HPH "C:\\"//头文件的默认路径

int getFSize(char *Path);//获取文件字节数,参数文件路径

char *readHFile(char *hPath);//获取头文件内容

char *checkCFile(char *cPath,char *hcon);// 检查源文件,找到对应includ语句,替换成对应头文件内容,并保存到新的文件中

int *findStr(char *str1,char *str2);//在str1中查找str2,返回str2在str1中对应的首尾字符下标

int main()

{

    char *newFile=NULL;

    newFile=checkCFile("C:\\file1.c","#include\"file1.h\"");

    if(newFile)

        printf("合并成功,合并后的文件内容为(路径:%s):\n%s\n",NN,newFile);

    else

        printf("合并失败,无匹配内容\n");

    free(newFile);

    return 0;

}

int getFSize(char *Path)

{

    int fSize=0;

    if(!Path) return 0;

    FILE *fp=NULL;

    fp=fopen(Path,"r");

    if(!fp) return 0;

    fseek(fp,0,SEEK_END);

    fSize=ftell(fp);

    fclose(fp);

    return fSize;

}

char *checkCFile(char *cPath,char *hcom)

{

    int i=0,*inx=NULL,fSize=0,hfnLen=0,hPLen=0,len=0;

    char *fcon=NULL,*hcon=NULL,*newfcon=NULL,*hPath=NULL,*pb=hcom,*pe=pb,*hfn=NULL;

    if(!cPath || !hcom) return NULL;

    FILE *fp=NULL;

    fp=fopen(cPath,"rb+");//这里不要要用r,避免下面计算大小误差

    if(!fp) return 0;

    if(!(fSize=getFSize(cPath))) return NULL;

    printf("源文件大小:%d字节\n",fSize);

    fcon=(char *)malloc(sizeof(char)*(fSize+1));

    if(!fcon) return 0;

    fcon[fSize]=0;

    //fseek(fp,0,SEEK_SET);

    if(fread(fcon,1,fSize,fp)!=fSize) return NULL;

    printf("源文件内容为:\n%s\n",fcon);

    if(!(inx=findStr(fcon,hcom))) return NULL;//将找到的匹配内容所在下标保存在inx指向地址

    fclose(fp);

    //----通过include语句获取头文件文件名---

    while(*pb!='"')pb++;

    pb++,pe=pb;

    while(*pe!='"')pe++;

    hfnLen=pe-pb;

    hfn=(char *)malloc(sizeof(char)*(hfnLen+1));

    if(!hfn)return NULL;

    hfn[hfnLen]=0;

    while(pb<pe)hfn[i++]=*pb,pb++;

    //----------------------------------------

    hPLen=hfnLen+strlen(HPH);

    hPath=(char *)malloc(sizeof(char)*(hPLen+1));

    strcpy(hPath,HPH);

    strcat(hPath,hfn);

    hPath[hPLen]=0;

    hcon=readHFile(hPath);//获得头文件对应内容

    //----合并两个文件内容到新的数组,释放原数组---

    len=fSize-strlen(hcom)+strlen(hcon);

    newfcon=(char *)malloc(sizeof(char)*(len+1));

    if(!newfcon) return NULL;

    newfcon[0]=0;

    fcon[inx[0]]=0;

    pb=fcon;

    strcpy(newfcon,pb);

    pb=hcon;

    strcat(newfcon,pb);

    pb=&fcon[inx[1]+1];

    strcat(newfcon,pb);

    newfcon[len]=0;

    free(fcon);

    free(hcon);

    free(hfn);

    //----------------------------------------------

    //-------------写入新文件-----------------------

    fp=fopen(NN,"w");

    if(!fp) return NULL;

    fwrite(newfcon,1,strlen(newfcon),fp);

    fclose(fp);

    //---------------------------------------------

    return newfcon;

}

char *readHFile(char *hPath)

{

    int fSize=0;

    char *hcon=NULL;

    FILE *fp=NULL;

    fp=fopen(hPath,"rb+");

    if(!fp)return NULL;

    if(!(fSize=getFSize(hPath)))return NULL;

    printf("头文件大小:%d字节\n",fSize);

    hcon=(char *)malloc(sizeof(char)*(fSize+1));

    if(!hcon) return NULL;

    hcon[fSize]=0;

    if(fread(hcon,1,fSize,fp)!=fSize) return NULL;

    printf("头文件内容为:\n%s\n",hcon);

    fclose(fp);

    return hcon;

}

int *findStr(char *str1,char *str2)

{

    char *p0=NULL,*p1=NULL;

    int i,cnt=0,len1,len2;

    static int inx[2];//str2首尾字符在str1中的对应下标

    if(!str1 || !str2) return NULL;

    inx[0]=inx[1]=0;

    len1=strlen(str1);

    len2=strlen(str2);

    for(i=0;i<len1;i++)

    {


        cnt=0,p0=&str1[i],p1=p0+len2-1;

        //每次搜索,先用p0,p1定位str2的在str1中的首尾位置,进行字符对比,再两指针向内收缩比较

        if(*p0!=str2[cnt] || *p1!=str2[len2-1-cnt]) continue;

        cnt++,p0++,p1--;

        while(p0<=p1)

        {

            if(*p0!=str2[cnt] || *p1!=str2[len2-1-cnt]) break;//只要一对字符不匹配,跳过本次对比

            cnt++,p0++,p1--;

        }

        if(p0>p1)//没有跳过说明本次完全匹配,记录首尾位置

        {

            inx[0]=i,inx[1]=i+len2-1;

            return inx;

        }

    }

    return NULL;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2021-05-29
用C语言的f开头的函数,可以操作文件。
题目里的操作,相当于编译器功能的一部分。
因为编译遇到“#include ”的时候,就是把对应的 .h文件,替换到该位置。
属于预编译过程,当然预编译还有别的操作,比如宏展开。
第2个回答  2021-05-30
你这字符串写错了
1. " ''"
2.#为编译器预处理指令,不能乱用
头文件#include是文本形式
""里面的#in clude是字符串
如果编译器不能识别字符串的#include指令,也不能识别头文件的#include指令,由此追答

用查找字符串函数

相似回答