導航:首頁 > 編程語言 > 用c編程實現txt搜索

用c編程實現txt搜索

發布時間:2025-05-15 20:47:20

❶ C語言如何讀取txt文本裡面的內容

C語言可以使用fopen()函數讀取txt文本里。

示例:

#include <stdio.h>

FILE *stream, *stream2;

void main( void )

{

int numclosed;

/* Open for read (will fail if file "data" does not exist) */

if( (stream = fopen( "data", "r" )) == NULL )

printf( "The file 'data' was not opened " );

else

printf( "The file 'data' was opened " );

/* Open for write */

if( (stream2 = fopen( "data2", "w+" )) == NULL )

printf( "The file 'data2' was not opened " );

else

printf( "The file 'data2' was opened " );

/* Close stream */

if(fclose( stream2 ))

printf( "The file 'data2' was not closed " );

/* All other files are closed: */

numclosed = _fcloseall( );

printf( "Number of files closed by _fcloseall: %u ", numclosed );

}

(1)用c編程實現txt搜索擴展閱讀

使用fgetc函數

#include <stdio.h>

#include <stdlib.h>

void main( void )

{

FILE *stream;

char buffer[81];

int i, ch;

/* Open file to read line from: */

if( (stream = fopen( "fgetc.c", "r" )) == NULL )

exit( 0 );

/* Read in first 80 characters and place them in "buffer": */

ch = fgetc( stream );

for( i=0; (i < 80 ) && ( feof( stream ) == 0 ); i++ )

{

buffer[i] = (char)ch;

ch = fgetc( stream );

}

/* Add null to end string */

buffer[i] = '';

printf( "%s ", buffer );

fclose( stream );

}

❷ 如何用C語言實現dir>abc.txt功能

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

#include <stdio.h>
#include <stdlib.h>
#include <dir.h>

#define RP "f:" /*需要進行查找的目錄*/
#define FA ".Mp3" /*需要查找的文件類型*/
void find(char *c,FILE *fp)
{
struct ffblk fb,fm;
int dr,df;
char dir[255]={0},tmp[255];
strcpy(tmp,c);

dr=findfirst(strcat(tmp,"\\*"),&fb,FA_DIREC);
while (!dr)
{
if (strcmp(fb.ff_name,".")&&strcmp(fb.ff_name ,"..")&&fb.ff_attrib ==FA_DIREC) {
strcpy(dir,c);
strcat(dir,"\\");
strcat(dir,fb.ff_name);
find(dir,fp);
}
dr=findnext(&fb);
}
df=findfirst(strcat(tmp,FA),&fm,0);
while (!df)
{
fprintf(fp,"%s\\%s\n",c,fm.ff_name);
df=findnext(&fm);
}
}

int main(void)
{
FILE *fp;
fp=fopen("c:\\Songlist.ini","w");/*要保存查找結果的文件*/
find(RP,fp);
fclose(fp);
return 0;
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------

❸ MFC怎麼查找當前目錄以及子目錄下的*_DS.TXT文件

在應用程序的開發過程中,會遇到如何查找某一文件以確定此文件路徑的問題。利用CFileFind類可以比較方便地在當前目錄下進行文件查找,但卻不能對其子目錄中的文件進行搜尋。而實際應用中往往需要對某一整個目錄樹,甚至是整個C盤或D盤驅動器進行文件搜尋。通過實踐,我們在Visual C++ 6.0中編程實現了如何遍歷任意目錄樹,以查找某一特定的文件。
在下面的具體陳述中可以看到,在確定要查找的文件名和要進行搜索的目錄的名稱後,將調用函數Search_Directory進行文件的查找。首先依次查找當前目錄下的每一個實體(文件或是子目錄),如果是某一子目錄,則進入該子目錄並遞歸調用函數Search_Dirctory進行查找,查找完畢之後, 再返回上一級目錄;如果不是子目錄而是某一文件,則判斷其是否就是我們要查找的文件,如果是則輸出其完整的文件路徑。這樣,通過Search_Directory函數的反復遞歸調用,就可以實現對整個目錄,包括子目錄的遍歷搜索。下面將舉例詳細講述如何在VC++中編程實現在整個目錄樹中的文件查找。
1. 在Visual C++ 6.0中用默認方式創建了一基於對話框的應用程序Search。在主窗口對話框上放置一命令按鈕,其Caption為「Search File」,ID為ID_BUTTON_SEARCH。單擊此按鈕將完成文件的查找工作。
2. 利用ClassWizard為「Search File」按鈕的BN_CLICKED 事件添加處理函數OnButtonSearch,代碼如下:

#include 〈direct.h〉
#include 〈io.h〉
void CSearchDlg::OnButtonSearch()
{
// TODO: Add your control notification handler code here

char szFilename[80];
// 字元串 szFilename 表示要查找的文件名

strcpy(szFilename,"Mytext.txt");

_chdir("d:\\"); // 進入要查找的路徑(也可為某一具體的目錄)
// 查找文件, 如果查到則顯示文件的路徑全名
Search_Directory(szFilename);
// 為CSearchDlg類的一成員函數
MessageBox(″查找文件完畢!″);
// 顯示查找完畢的信息
}

3. 在CSearchDlg類中增加成員函數Search_Directory,它將完成具體的文件查找工作,代碼如下:
void CSearchDlg::Search_Directory(char* szFilename)
{
long handle;
struct _finddata_t filestruct;
//表示文件(或目錄)的信息
char path_search[_MAX_PATH];
//表示查找到的路徑結果
// 開始查找工作, 找到當前目錄下的第一個實體(文件或子目錄),
// "*"表示查找任何的文件或子目錄, filestruct為查找結果
handle = _findfirst("*", &filestruct);
// 如果handle為-1, 表示當前目錄為空, 則結束查找而返回
if((handle == -1)) return;
// 檢查找到的第一個實體是否是一個目錄(filestruct.name為其名稱)
if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
// 如果是目錄, 則進入該目錄並遞歸調用函數Search_Dirctory進行查找,
// 注意: 如果目錄名的首字元為'.'(即為"."或".."), 則不用進行查找
if( filestruct.name[0] != '.' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
// 查找完畢之後, 返回上一級目錄
_chdir("..");
}
}
else // 如果第一個實體不是目錄, 則檢查是否是要查找的文件
{
// stricmp對兩字元串進行小寫形式的對比, 返回為0表示完全一致
if( !stricmp(filestruct.name, szFilename) )
{
// 先獲得當前工作目錄的全路徑
_getcwd(path_search,_MAX_PATH);
// 再獲得文件的完整的路徑名(包含文件的名稱)
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search); //輸出顯示
}
}
// 繼續對當前目錄中的下一個子目錄或文件進行與上面同樣的查找
while(!(_findnext(handle,&filestruct)))
{
if( ::GetFileAttributes(filestruct.name) & FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '.')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir("..");
}
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search,"\\");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
// 最後結束整個查找工作
}
這樣我們就可以對整個目錄進行遍歷搜索,查找某一特定的文件,並輸出顯示其完整的文件路徑。以上的程序在Visual C++ 6.0中已調試通過。

閱讀全文

與用c編程實現txt搜索相關的資料

熱點內容
linux分區讀取 瀏覽:794
單片機液晶顯示屏出現雪花 瀏覽:890
解壓器用哪個好一點 瀏覽:771
什麼app看小說全免費 瀏覽:503
sha和ras加密 瀏覽:823
韓順平php視頻筆記 瀏覽:636
阿里雲ecs伺服器如何設置自動重啟 瀏覽:596
三星電視怎麼卸掉app 瀏覽:317
如何將pdf轉換成docx文件 瀏覽:32
dos命令批量改名 瀏覽:376
centosphp環境包 瀏覽:601
mfipdf 瀏覽:534
電腦解壓後電腦藍屏 瀏覽:295
外網訪問內網伺服器如何在路由器設置 瀏覽:856
2014統計年鑒pdf 瀏覽:434
linuxoracle用戶密碼 瀏覽:757
股票交易pdf 瀏覽:898
p2papp源碼 瀏覽:308
記錄睡眠軟體app哪個好用 瀏覽:140
液壓助力車壓縮比 瀏覽:217