Ⅰ 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在脱离编译器运行的时你所定的目录就不能改变了也就是说你自能在写程序时定义一次目标文件的路径