【数据结构与算法分析——C语言描述】练习1.4

思路:

1.函数printHeadfile()接受一个文件路径,并打开该路径文件。

2.成功打开后,不断读入文件内一行数据buf。如果该行是一个“#include ”指令行,则由printHeadfile()打开并输出该子文件内容后,输出当前数据行buf(“#include ”指令行)。

3.读入下一行数据。直到读取不到,关闭文件。结束。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define SIZE 1024
 
void printHeadfile(char * filePath);
 
int main(void)
{
    printHeadfile("d:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include\\stdio.h");
    system("pause");
    return 0;
}
 
/*递归打印头文件*/
void printHeadfile(char * filePath)
{
    FILE * fp;
    char subName[SIZE], buf[SIZE], subPath[SIZE] = "d:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include\\";
    char * pos;
    int size;
 
    if (fopen_s(&fp, filePath, "r"))
    {
        fprintf(stderr, "Open file failed: %s", filePath);
        return;
    }
 
    while (fgets(buf, SIZE, fp) != NULL)
    {
        if (strstr(buf, "#include "))
        {
            if (strchr(buf, '<'))
            {
                pos = strchr(buf, '<') + 1;
                size = strchr(buf, '>') - strchr(buf, '<') - 1;
            }
            else if (strchr(buf, '\"'))
            {
                pos = strchr(buf, '\"') + 1;
                size = strrchr(buf, '\"') - strchr(buf, '\"') - 1;
            }
            strncpy_s(subName, SIZE, pos, size);
            strcat_s(subPath, SIZE, subName);
            printHeadfile(subPath);
            memset(subName, 0, SIZE);
            strcpy_s(subPath, SIZE, "d:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include\\");
        }
        fputs(buf, stdout);
    }
    fclose(fp);
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值