首先,指針默認只有一個地址的長度的空間,存不了那么多的字符。

在當(dāng)陽等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站制作、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計,成都全網(wǎng)營銷推廣,外貿(mào)網(wǎng)站建設(shè),當(dāng)陽網(wǎng)站建設(shè)費用合理。
其次,傳值和傳地址一塌糊涂,完全就是亂寫么。
scanf的第二個參數(shù)是地址,如果本身不是指針則需要取址符。
我給你重寫了算了
#includestdio.h
int Retrieve(int array[],int length,int x);/*形參x直接傳值就可以了*/
int main()
{
int *a,x;
int length,i;
scanf("%d",length);
a = (int)malloc(sizeof(int)*length); /*申請一個內(nèi)存空間用來存放數(shù)據(jù),或者直接用一個大數(shù)組也可以*/
C語言本身沒有提供象dir_list()這樣的函數(shù)來列出某個目錄下所有的文件。不過,利用C語言的幾個目錄函數(shù),你可以自己編寫一個dir_list()函數(shù)。
首先,頭文件dos.h定義了一個find_t結(jié)構(gòu),它可以描述DOS下的文件信息,包括文件名、時間、日期、大小和屬性。其次,C編譯程序庫中有_dos_findfirst()和_dos_findnext()這樣兩個函數(shù),利用它們可以找到某個目錄下符合查找要求的第一個或下一個文件。
dos_findfirst()函數(shù)有三個參數(shù),第一個參數(shù)指明要查找的文件名,例如你可以用“*.*”指明要查找某個目錄下的所有文件。第二個參數(shù)指明要查找的文件屬性,例如你可以指明只查找隱含文件或子目錄。第三個參數(shù)是指向一個find_t變量的指針,查找到的文件的有關(guān)信息將存放到該變量中。
dos_findnext()函數(shù)在相應(yīng)的目錄中繼續(xù)查找由_dos_findfirst()函數(shù)的第一個參數(shù)指明的文件。_dos_findnext()函數(shù)只有一個參數(shù),它同樣是指向一個find_t變量的指針,查找到剛文件的有關(guān)信息同樣將存放到該變量中。
利用上述兩個函數(shù)和find_t結(jié)構(gòu),你就可以遍歷磁盤上的某個目錄,并列出該目錄下所有的文件,請看下例:
#include stdio.h
#include direct.h
#include dos.h
#include malloc.h
#include memory.h
#include string.h
typedef struct find_t FILE_BLOCK
void main(void);
void main(void){FILE_BLOCK f-block; /* Define the find_t structure variable * /
int ret_code; / * Define a variable to store the return codes * /
/ * Use the "*.*" file mask and the 0xFF attribute mask to list
all files in the directory, including system files, hidden
files, and subdirectory names. * /
ret_code = _dos_findfirst(" *. * ", 0xFF, f_block);
/* The _dos_findfirst() function returns a 0 when it is successful
and has found a valid filename in the directory. * /
while (ret_code == 0){/* Print the file's name * /
printf(" %-12s
, f_block, name);
/ * Use the -dos_findnext() function to look
//要另外說下如fprintf(stderr, "Can't open %s\n", file_app);這是向文件或者系統(tǒng)設(shè)備輸出的函數(shù);但他的文件指針為stderr;這是c中的標(biāo)準錯誤輸出設(shè)備指針,系統(tǒng)自動分配為顯示器故相當(dāng)于printf("Can't open %s\n", file_app);
#include stdio.h
#include stdlib.h
#include string.h
#define BUFSIZE 1024
#define SLEN 81
void append(FILE *source, FILE *dest);
int main(void)
{
FILE *fa, *fs; //定義2個文件類型指針
int files = 0; // 追加文件個數(shù)
char file_app[SLEN];
char file_src[SLEN]; // 2個字符串用來儲存文件名;
puts("Enter name of destination file:");//輸出Enter name of destination file:
gets(file_app);//輸入要追加的文件名
if ((fa = fopen(file_app, "a")) == NULL)//fa指向追加的目的文件,以追加方式打開文件,如果打開失敗退出;
{
fprintf(stderr, "Can't open %s\n", file_app);
exit(2);
}
if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) != 0)//創(chuàng)建緩沖器與流相關(guān),大小為BUFSIZE,作用是提高IO速度;如果打開失敗退出
{
fputs("Can't create output buffer\n", stderr);
exit(3);
}
puts("Enter name of first source file (empty line to quit):");//輸出Enter name of first source file (empty line to quit):
while (gets(file_src) file_src[0] != '\0')//輸入源文件如果是空串結(jié)束循環(huán)
{
if (strcmp(file_src, file_app) == 0)//如果源和追加文件相同
fputs("Can't append file to itself\n",stderr);
else if ((fs = fopen(file_src, "r")) == NULL)//如果打開源文件失敗
fprintf(stderr, "Can't open %s\n", file_src);
else
{
if (setvbuf(fs, NULL, _IOFBF, BUFSIZE) != 0)//創(chuàng)建緩沖器與流相關(guān),大小為BUFSIZE,作用是提高IO速度;如果打開失敗開始下次循環(huán)
{
fputs("Can't create input buffer\n",stderr);
continue;
}
append(fs, fa);//函數(shù)
if (ferror(fs) != 0)//檢查文件操作是否有錯
fprintf(stderr,"Error in reading file %s.\n",
file_src);
if (ferror(fa) != 0)
fprintf(stderr,"Error in writing file %s.\n",
file_app);
fclose(fs);//關(guān)閉源文件
files++;//追加文件數(shù)+1
printf("File %s appended.\n", file_src);
puts("Next file (empty line to quit):");
}
}
printf("Done. %d files appended.\n", files);
fclose(fa);//關(guān)閉追加文件
return 0;
}
void append(FILE *source, FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while ((bytes = fread(temp,sizeof(char),BUFSIZE,source)) 0)//把源文件的內(nèi)容追加到追加文件,塊大小sizeof(char),塊數(shù)為BUFSIZE
fwrite(temp, sizeof (char), bytes, dest);//寫文件塊大小sizeof(char),塊數(shù)為BUFSIZE
}
這是我的TFTP程序中的一個函數(shù),是搜索當(dāng)前盤符下的所有文件,包括文件的大小,并發(fā)送到客戶端,其中就有查找當(dāng)前路徑下的文件,你自己挑一下,應(yīng)該能完成你的需求。
void FileList(sockaddr_in sour_addr,char strStartDir[])
{
char sendbuffer[1024];
sockaddr_in destaddr;
int sourlen = 0;
int ret = 0;
int len = 0;
int flen = 0;
fd_set fdr;
unsigned short blocknum = 0;
FILE *file;
char filename[128];
strcpy(filename,strStartDir+2); /*獲取文件名*/
strcat(filename,"\\*");
destaddr.sin_family = AF_INET;
destaddr.sin_port = sour_addr.sin_port;
destaddr.sin_addr.s_addr = inet_addr(desthost);//
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
hFind = FindFirstFile(filename, FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("Invalid File Handle");
}
else
{
while(FindNextFile(hFind,FindFileData))
{
printf(FindFileData.cFileName);
printf("\r\n");
memset(sendbuffer,'\0',1024);
len = filldata(blocknum++,FindFileData.cFileName,strlen(FindFileData.cFileName),sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)destaddr,sizeof(destaddr));
}
len = fillover(blocknum,"Over",4,sendbuffer,sizeof(sendbuffer));
ret = sendto(serverSock,sendbuffer,len,0,(sockaddr *)destaddr,sizeof(destaddr));
FindClose(hFind);
return;
}
}
#include
stdio.h
main()
{
char
str[80]="d:\\files\\dataFiles\\data\\avatar.bmp";
//
單斜杠處填雙斜杠
int
L,i;
L
=
strlen(str);
for
(i=L-1;i=0;i--)
if
(str[i]
==
'\\')
{
str[i]
='\0';
break;};
//
去掉文件名就是路徑
printf("Path=
%s",str);
return
0;
}
-----------
寫成函數(shù)和調(diào)用:
#include
stdio.h
void
fun(char
*str){
int
L,i;
L
=
strlen(str);
for
(i=L-1;i=0;i--)
if
(str[i]
==
'\\')
{
str[i]
='\0';
break;};
}
main()
{
char
str[80]="d:\\files\\dataFiles\\data\\avatar.bmp";
fun(str);
printf("Path=
%s",str);
return
0;
}
#include?stdio.h
int?search(int?a[],int?b,int?c,int?i)
{
int?x,y,z;
x=i+1;
z=b-1;
y=(x+z)/2;
while(x=z)
{
if(a[y]c)
{
z=y-1;
y=(x+z)/2;
continue;
}
if(a[y]c)
{
x=y+1;
y=(x+z)/2;
continue;
}
return?y+1;
}
return?-1;
}
int?main()
{
int?i,m,pos;
scanf("%d",m);
int?a[m];
for(i=0;im;i++)
{
scanf("%d",a[i]);
}
for(i=0;im;i++)
{
pos=search(a,m,a[i],i);
if(pos!=-1)
{
printf("FOUND?a[%d]=%d,?position?is?%d\n",i,a[i],i+1);
return?0;
}
}
if(pos==-1)
{
printf("NOT?FOUND\n");
}
return?0;
}
這種查找方法的數(shù)組必須是從小到大的,用遍歷的話就沒這個問題了。
標(biāo)題名稱:c語言搜索文件函數(shù),c++搜索文件
網(wǎng)站路徑:http://chinadenli.net/article41/dsgssed.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、微信公眾號、網(wǎng)站設(shè)計、全網(wǎng)營銷推廣、小程序開發(fā)、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)