當前位置:首頁 » 編程語言 » c語言從文件查找字元串

c語言從文件查找字元串

發布時間: 2022-09-05 21:47:14

A. c語言在文本中查找字元串

第一空填:argv[1] //文件名以字元指針的形式存在argv[1]中
第二空填:fscanf(fp,"%s\n",str); //讀取一行的字元串,存到str這個字元數組中去
第三空填:fclose(fp); //關閉文件

B. C語言怎麼從文件中查找字元串並列印

#include <stdio.h>
#include <stdlib.h>
void File()
{
FILE *in, *out;
char ch ;
if ((in = fopen("d:\\wenjian\\in.txt","r")) == NULL) //in.txt 和out.txt 都在當前工作目錄下存放
{
printf("canot find the file!\n");
exit(0);
}

if ((out = fopen("d:\\wenjian\\out.txt","w"))==NULL) // 寫入數據的文件
{
printf("canot find the out.txt file !\n");
exit(0);
}

ch = fgetc(in);
while (ch!=EOF)
{
fputc(ch,out);
putchar(ch); //是in.txt 的內容顯示在dos窗口 下
ch = fgetc(in);
}
fclose(in); // 關閉文件
fclose(out);
}

int main()
{
File() ;
puts("");
return 0;
}
我寫的代碼參考下。

C. C語言中如何查找字元串

用strstr這個函數

包含文件:string.h
函數名: strstr
函數原型:extern char *strstr(char *str1, char *str2);
功能:找出str2字元串在str1字元串中第一次出現的位置(不包括str2的串結束符)。
返回值:返回該位置的指針,如找不到,返回空指針。

源代碼:

#include<stdio.h>
#include<string.h>//調用string.h中的strstr函數
void main(){
char ch1[255]="abcde";
char ch2[100]="cd";
char* ch;//用於接受返回值
if((ch=strstr(ch1,ch2))==NULL){//說明沒有要找的字元串
printf("-1\n");
}else{//說明找到了那個字元串
printf("%d\n",ch-ch1+1);//cde的地址減去abcde的地址+1
}
}

D. C語言如何從文件中查找指定的字元,然後替換它

這個很簡單呢,你只要去循環比對
/*檢查data字元串,是否samplestr的一個子串,samplestr的各個字串以|分隔*/
int
CheckStrIn(char
*data,char
*samplstr)
{
int
i,ilen,icount;
char
*sp;
sp=samplstr;
ilen=strlen(samplstr);
for(i=0,icount=1;i<ilen;i++,sp++)
{
if(*sp=='|')
icount++;
}
ilen=strlen(data);
for(i=0;i<icount;i++)
{
if(memcmp(data,searchsplit(samplstr,i),ilen)==0)
return
1;
}
return
0;
}
如果是則替換掉

E. c語言字元串的查找用什麼函數

用strstr這個函數
包含文件:string.h
函數名: strstr
函數原型:extern char *strstr(char *str1, char *str2);
功能:找出str2字元串在str1字元串中第一次出現的位置(不包括str2的串結束符)。
返回值:返回該位置的指針,如找不到,返回空指針。

F. 在C語言里在一個文件里搜索一個字元串並進行修改

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void Substitute(char *pInput, char *pOutput, char *pSrc, char *pDst)
{
char *pi, *po, *p;
int nSrcLen, nDstLen, nLen;
// 指向輸入字元串的游動指針.
pi = pInput;
// 指向輸出字元串的游動指針.
po = pOutput;
// 計算被替換串和替換串的長度.
nSrcLen = strlen(pSrc);
nDstLen = strlen(pDst);
// 查找pi指向字元串中第一次出現替換串的位置,並返回指針(找不到則返回null).
p = strstr(pi, pSrc);
if(p)
{
// 找到.
while(p)
{
//計算被替換串前邊字元串的長度.
nLen = (int)(p - pi);
// 復制到輸出字元串.
memcpy(po, pi, nLen);
memcpy(po + nLen, pDst, nDstLen);
// 跳過被替換串.
pi = p + nSrcLen;
// 調整指向輸出串的指針位置.
po = po + nLen + nDstLen;
// 繼續查找.
p = strstr(pi, pSrc);
}
// 復制剩餘字元串.
strcpy(po, pi);
}
else
{
// 沒有找到則原樣復制.
strcpy(po, pi);
}
}
int main(int ac, char *av[])
{
if (ac!=5) {
printf("程序名 要操作的文件 新文件 查找的字元串 替換的字元串\n");
printf("示例:test.exe 1.txt 2.txt hello love\n");
return 0;
}
const int MAXSIZES = 100;
FILE *fpSrc,*fpDes;
char filename1[20]="1.txt";
char filename2[20]="2.txt";
//要求查找的字元串,替換的字元串;
char ps[]="hello";
char pd[]="love";
//求取所查找和替換的字元串的長度;
int len_src=strlen(av[3]);
int len_des=strlen(av[4]);
//定義存儲字元串緩沖區;很奇怪的一點是,fgets函數不能將字元串寫入動態分配的內存中
/*char* Src_buf=(char*)(sizeof(char)*MAXSIZES);
char* Cpy_buf=(char*)(sizeof(char)*MAXSIZES);
char* Des_buf=(char*)(sizeof(char)*MAXSIZES);*/
char Src_buf[MAXSIZES] = {0};
char Cpy_buf[MAXSIZES] = {0};
char Des_buf[MAXSIZES] = {0};
//打開文件
if((fpSrc=fopen(av[1],"r+"))==NULL)
{
printf("fail to open the file1 !\n");
exit(1);
}
if((fpDes=fopen(av[2],"a+"))==NULL)
{
printf("fail to open the file2 !\n");
exit(1);
}
//進行循環讀取
while(!feof(fpSrc))//判斷文件是否已結束;!feof(fpSrc)
{
fgets(Src_buf,MAXSIZES,fpSrc);
Substitute(Src_buf,Des_buf,av[3],av[4]);
fputs(Des_buf,fpDes);
printf("%s",Des_buf);
}
fclose(fpSrc);
fclose(fpDes);
system("pause");
return 0;
}

G. C語言程序 如何從文件中查找特定的字元

1、打開文件,遍歷文件內容然後一個一個匹配查找並替換,最後再重新寫入文件當中。
2、常式:
#include
int main()
{
file *fp;
char filename[100];
printf("請輸入文件名:\n");
gets(filename);
fp=fopen(filename,"r");
char c,x,flag=0;
printf("請輸入要查找的字元:\n");
scanf("%c",&x);
while(fscanf(fp,"%c",&c)!=eof)
{
if(c==x)
{
flag=1;
break;
}
}
if(flag==1)
printf("文件中含有字元%c\n",x);
else
printf("文件中沒有字元%c\n",x);
return 0;
}

H. c語言查找字元串

字元串在存儲上類似字元數組,所以它每一位的單個元素都是可以提取的,如s=「abcdefghij」,則s[1]=「b」,s[9]="j",而字元串的零位正是它的長度,c語言查找字元串方法為:

1、首先,定義一個字元數組變數,可以這么寫。

注意事項:

盡管形式字元串可以有任意(但有限)的長度,實際語言的字元串的長度經常被限制到一個人工極大值。有兩種類型的字元串數據類型: 「定長字元串」,它有固定的極大長度並且不管是否達到了這個極大值都使用同樣數量的內存。

I. 麻煩誰幫我解釋下 C語言程序 關於文件查找特殊字元串

#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
//三個頭文件,分別表示標准化io,標准化庫文件,字元串類文件的頭文件
int
main()
{
int
line=0;//記錄行數
FILE
*fp;//定義文件指針
char
FILENAME[100],li[256],indata[255],*sust=NULL;//
printf("Input
file
name
please:");
gets(FILENAME);//獲取文件名
if
((fp=fopen(FILENAME,"r"))!=NULL){//如果讀取成功,即fopen函數返回值不為空
printf("Input
a
string
please:");
gets(indata);//輸入要尋找的那個字元串
while
(!feof(fp))//當文件未結束一直執行該操作
{
line++;//行計數加一
fgets(li,255,fp);//讀取255個字元
sust=strstr(li,indata);//從字元串li中尋找indata第一次出現的位置(不比較結束符NULL)。參看//http://ke..com/view/745156.htm?fr=ala0
if
(sust!=NULL)
{//如果未找到
printf("Found
it
in
line
%d\n",line);
}
}
fclose(fp);//關閉文件
}
else
puts("File
not
found!");//文件未找到
return
0;
}

J. c語言從文件中查找字元串

不用自己寫,有一個函數叫strstr,原型是
char
*strstr(char
*str1,
char
*str2),功能是找出str2字元串在str1字元串中第一次出現的位置。
可以這樣寫:
char
*p=strstr(a,b);
if(null
!=
p)
{
//a中不存在b,添加相應代碼
}
else
{
//a中存在b,添加相應代碼
}
返回值p為a中第一次出現b的位置
這個函數要包含頭文件string.h

熱點內容
ios應用上傳 發布:2024-09-08 09:39:41 瀏覽:438
ios儲存密碼哪裡看 發布:2024-09-08 09:30:02 瀏覽:871
opensslcmake編譯 發布:2024-09-08 09:08:48 瀏覽:653
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:744
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:173
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:780
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:101
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:209
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995