C語言查詢字串在檔案中的第幾行第幾列

2021-07-10 16:23:45 字數 772 閱讀 8256

面試中遇到的問題,回來後當做練習寫了一下,鞏固一下知識。

現將**貼出來:

#include 

#include

#include

int search_str(char*p_str,int

*row,int

*col,file*fp);

char *p = null;

int i = 0;

while(fgets(line,512,fp))

}return0;}

int main()

get = search_str("hello",&row,&col,fp);

if(get)

else

fclose(fp);

return

0;}

現在編譯:

$ gcc test.c

$

建立乙個檔案test.txt,內容如下:

it is a test line

it is a test line

it is a test line

it is a test line

it is a test line

ok,'hello' is here

現在執行程式:

$ ./a.out

hello is at row 6 col 5

$

Linux在檔案中查詢字串

如果你想在當前目錄下 查詢 hello,world 字串,可以這樣 grep rn hello,world 表示當前目錄所有檔案,也可以是某個檔名 r 是遞迴查詢 n 是顯示行號 r 查詢所有檔案包含子目錄 i 忽略大小寫 下面是一些有意思的命令列引數 grep i pattern files 不區...

linux在檔案中查詢字串

使用命令 cat 檔名 grep 字串 這個是最簡單也是最常用的查詢方式 root jr yfb ng tm 10 94 logs cat catalina.out grep 經銷商銀行卡 info 2019 01 16 20 05 53 com.runlion.supchain.init.init...

C語言查詢字串

使用c語言實現字串查詢子串,返回第乙個子串出現的位置。ubuntu16.04 lts gcc編譯器 include define n 100 int find str char str1,char str2 j 0 return flag int main else return 0 函式原型 in...