導航:首頁 > 文件處理 > c語言獲取當前文件夾路徑

c語言獲取當前文件夾路徑

發布時間:2022-11-02 22:06:42

Ⅰ c語言如何獲取用戶通過鍵盤輸入的文件目錄中的文件名和文件路徑,ballball大佬幫幫我🙏求代碼

int main()
{
string s = "c:\\abc\\def\\text.txt";
int xie_index = s.find_last_of('\\'); // 路徑中最後一個\的位置
string file_dirname = s.substr(0, xie_index + 1);
string file_basename = s.substr(xie_index + 1, s.size());
cout << file_dirname << endl << file_basename << endl;
}

Ⅱ C語言打開文件的路徑問題

#include<stdio.h>
int main(void)
{
int a, sum;
FILE *ifp, *ofp;

sum = 0;
if((ifp = fopen("C:\Users\huangwj\Desktop\h.txt", "rt"))==NULL)
{
printf("not in");
return 0;
}
if((ofp = fopen("C:\Users\huangwj\Desktop\yes.txt", "wt"))==NULL)
{
printf(" out!!");
return 0;
}
while( fscanf(ifp, "%d", &a) != EOF)
sum += a;
fprintf(ofp, "The sum is %d. \n", sum);
fclose(ifp);
fclose(ofp);
return 0;
}

如果對您有幫助,請記得採納為滿意答案,謝謝!祝您生活愉快!

Ⅲ 如何用C語言獲取目錄下的文件和目錄列表

1、可以利用getenv函數來實現。
在Linux系統中,home目錄的定義是通過系統環境變數中的HOME變數值來確定的,在shell下可以通過
echo $HOME來查看。
而在C語言中,庫函數getenv可以用作獲取環境變數值。該函數位於stdlib.h, 原型為
char *getenv(char *name);
功能為獲取名字為name的環境變數字元串。
所以,下面代碼就可以獲取到home目錄名了:
2、常式:
char *home;
home = getenv("HOME");
printf("the home path is %s\n", home);

Ⅳ 在C語言讀文件時,如何說明是當前目錄

直接fopen("abc.txt","r");
就可以了。
不過要注意一點,如果你使用VC編譯調試,那麼在裡面點!號運行的時候,那個當前目錄是指工程目錄,而不是Debug文件夾裡面。當然,你直接把exe文件拿出來用的話就是一般意義下的當前目錄了。

Ⅳ C語言問題 已知某一路徑,如何得到該路徑下的某一文件夾的路徑

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <io.h>

#define MAX_FILE_PATH_LEN 128
#define INVALID_HANDLE -1

int main()
{
char aucFilePath[MAX_FILE_PATH_LEN + 1] = {0};
char aucTempPath[MAX_FILE_PATH_LEN + 1] = {0};
char *pFilePath = NULL;
int iDirNum = 0;
int iFileNum = 0;

_finddata_t finddata = {0};
long findhandle = INVALID_HANDLE;

//獲取當前目錄,也可以做入參傳入aucFilePath
pFilePath = getcwd(aucFilePath, MAX_FILE_PATH_LEN);
if (NULL == pFilePath)
{
printf("get current working directory fail, \n"
"errno = %d, errro description : %s\n", errno, strerror(errno));
getchar();
return -1;
}

printf("current working directory = %s\n", aucFilePath);
strncpy(aucTempPath, aucFilePath, strlen(aucFilePath));
strncat(aucTempPath, "\\*", MAX_FILE_PATH_LEN);

findhandle = _findfirst(aucTempPath, &finddata);
if(findhandle != INVALID_HANDLE)
{
do {
if(finddata.attrib & _A_SUBDIR)
{
if ((0 != strcmp(finddata.name, ".")) && (0 != strcmp(finddata.name, "..")))
{
//子目錄個數
iDirNum++;
printf("sub directory name = %s, path = %s\\%s, iDirNum = %d\n",
finddata.name, aucFilePath, finddata.name, iDirNum);
}
}
else
{
//子文件個數
iFileNum++;
printf("file = %s, iFileNum = %d\n", finddata.name, iFileNum);
}
}while(0 == _findnext(findhandle, &finddata));
_findclose(findhandle);
}

getchar();
return 0;
}

Ⅵ VC環境中用C語言查找當前路徑下的所有文件和文件夾的函數是什麼

這是我的TFTP程序中的一個函數,是搜索當前盤符下的所有文件,包括文件的大小,並發送到客戶端,其中就有查找當前路徑下的文件,你自己挑一下,應該能完成你的需求。
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;
}
}

Ⅶ C語言怎麼讀取某一文件夾下的所有文件夾和文件

讀取的代碼方式如下:

int main()

{

long file;

struct _finddata_t find;

_chdir("d:\");

if((file=_findfirst("*.*", &find))==-1L)

{

printf("空白! ");

exit(0);

}

printf("%s ", find.name);

while(_findnext(file, &find)==0)

{

printf("%s ", find.name);

}

_findclose(file);

return 0;

}

Ⅷ c語言中如何得到當前文件所在位置

如果是通過open方式打開的,那麼第一個參數就是文件路徑信息:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *path, int oflag, /* mode_t mode */...);

如果是通過fopen方式打開的,那麼第一個參數就是文件路徑信息:
#include <stdio.h>
FILE *fopen(const char *filename, const char *mode);

無論通過open還是fopen打開文件,都必須先知道文件路徑信息,盡管可能是相對路徑。
如果知道了filename的內容,我們就可以定位它的絕對路徑,也就是你說的完全路徑。

1. filename本身就是絕對路徑,ok。
2. filename是相對路徑,那麼先通過getcwd獲取進程的執行路徑,然後再獲取絕對路徑即可。
#include <unistd.h>
extern char *getcwd(char *buf, size_t size);

但是,如果進程在打開文件後又執行了chdir、fchdir之類函數的話,估計就不能夠再獲取文件路徑信息了。
#include <unistd.h>
int chdir(const char *path);
int fchdir(int fildes);

Ⅸ c語言如何獲得文件當前路徑

http://hi..com/andywangcn/item/7633efda5517baf9ca0c39c6
獲得雙斜杠路徑不包含文件名
TCHAR _szPath[MAX_PATH + 1]={0};
GetMoleFileName(NULL, _szPath, MAX_PATH);
(_tcsrchr(_szPath, _T('\\')))[1] = 0;//刪除文件名,只獲得路徑 字串
CString strPath;
for (int n=0;_szPath[n];n++)
{
if (_szPath[n]!=_T('\\'))
{
strPath +=_szPath[n] ;
}
else
{
strPath += _T("\\\\");
}
}
MessageBox(strPath);//輸出==e:\\program\\Debug\\
//頭文件用到 windows.h

Ⅹ C語言 如何取得當前可執行程序所在的路徑

一般默認的目錄是和你所遍的程序在同一個目錄里如果你想改,那麼只要在使用fopen函數的時候輸入目錄地址就可以了要注意的是一旦你這個程序生成了exe在脫離編譯器運行的時你所定的目錄就不能改變了也就是說你自能在寫程序時定義一次目標文件的路徑

閱讀全文

與c語言獲取當前文件夾路徑相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:758
蘋果郵件無法連接伺服器地址 瀏覽:963
phpffmpeg轉碼 瀏覽:672
長沙好玩的解壓項目 瀏覽:145
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:737
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:486
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:383
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:350
風翼app為什麼進不去了 瀏覽:779
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:151
伊克塞爾文檔怎麼進行加密 瀏覽:893
app轉賬是什麼 瀏覽:163