Linux编写C语言源程序文件通过关键字查找文件

编写display.c和search.c文件共同完成该任务

编写search.h作为同名头文件声明全局变量和查找文件函数

search.h源代码:

#include <stdio.h>
int file_search(char *path, char *gjz); //声明文件查找函数
extern char filename[256][256];
extern int len;
//在*.h文件中使用extern来声明变量,可以避免后续编译出现一些错误

*.h文件中写函数声明,*.c文件中实现,这样分离之后,如果其他c文件需要调用其中的函数只要包含同名的h头文件即可:#include "*.h",注意必须用双引号,不能用尖括号,使用双引号会优先调用当前目录下的头文件。
把具体相同功能的函数放置在一个h头文件中声明也有助于分类。

search.c源代码:

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include "search.h" //使用search.h中声明的变量,定义其中声明的函数
char filename[256][256]; //存放返回的文件名
int len;
int file_search(char *path, char *gjz)
{
    DIR *d; //声明一个句柄
    len = 0;
    struct dirent *file; //readdir函数的返回值就存放在这个结构体中
    if(!(d = opendir(path))) //检查该目录是否存在
    {
        printf("error opendir %s!\n",path);
        return -1;
    }
    while((file = readdir(d)) != NULL)  //遍历目录
    {
        //把当前目录.,上一级目录..及隐藏文件都去掉
        if(strncmp(file->d_name, ".", 1) == 0) //通过ASCⅡ码比较两个字符串是否相同,相同则返                    
                                                 回0,参数"1"表示只比较1个字符
            continue;
        if(strstr(file->d_name,gjz)) //检查右边字符串是否为左边字符串的子串()
        {		
            strcpy(filename[len++], file->d_name); //保存遍历到的文件名
        }
    }
    closedir(d);
    return 0;
}

display.c源代码

#include <stdio.h>
#include "search.h"
int main()
{
    int i;
    file_search("./text", "d"); //"./text"为查找的目录,"d"为文件名关键字
    for(i = 0; i < len; i++)
    {
        printf("%s\n", filename[i]);
    }
    printf("\n");
    return 0;
}

编译命令如下:

gcc display.c search.c -o display_searchfile   //编译生成可执行文件

编写makefile文件:

display_searchfile:display.c search.c //注意冒号要用英文冒号
	gcc display.c search.c -o display_searchfile  //开头用Tab缩进
clean:
	rm -f display_searchfile  //开头用Tab缩进

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在C语言文件中查找关键字的位置,可以按照以下步骤进行: 1. 打开文件,读取文件内容。 2. 使用字符串搜索函数(如strstr)在文件内容中查找关键字出现的位置。 3. 如果找到了关键字,可以使用文件指针的偏移量计算出关键字文件中的位置。 以下是一个简单的示例代码,用于在文件中查找关键字的位置: ```c #include <stdio.h> #include <string.h> int main() { char keyword[100] = "int"; // 要查找的关键字 char filename[100] = "test.c"; // 文件名 FILE* fp = fopen(filename, "r"); // 打开文件 char line[1000]; // 用于存储每行文件内容 int line_num = 0; // 记录当前行数 int found = 0; // 标记是否找到了关键字 if (fp == NULL) { printf("Failed to open file %s\n", filename); return 1; } // 逐行读取文件内容 while (fgets(line, sizeof(line), fp)) { line_num++; if (strstr(line, keyword) != NULL) { // 如果找到了关键字 found = 1; int offset = ftell(fp) - strlen(line); // 计算关键字文件中的位置 printf("Keyword \"%s\" found in line %d, offset %d\n", keyword, line_num, offset); } } if (!found) { printf("Keyword \"%s\" not found in file %s\n", keyword, filename); } fclose(fp); // 关闭文件 return 0; } ``` 该代码使用fgets函数逐行读取文件内容,然后使用strstr函数在每行内容中查找关键字。如果找到了关键字,则使用ftell函数计算出关键字文件中的位置。注意,该代码仅适用于在ASCII编码的文本文件中查找关键字,对于其他类型的文件可能需要采用不同的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

混沌浮世

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值