c语言获取当前项目路径的文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言获取当前项目路径的文件相关的知识,希望对你有一定的参考价值。
参考技术A /*从字符串的左边截取n个字符*/char * left(char *dst,char *src, long n)
char *p = src;
char *q = dst;
int len = strlen(src);
if(n>len) n = len;
/*p += (len-n);*/ /*从右边第n个字符开始*/
while(n--) *(q++) = *(p++);
*(q++)='\0'; /*有必要吗?很有必要*/
return dst;
int substr(char *s1, char *s2)
char *s3 = strstr(s1,s2);
if(s3 == NULL)
return -1;
return strlen(s1)-strlen(s3);
char* mysubstr(char* srcstr, int offset, int length)
int total_length = strlen(srcstr);
int real_length = ((total_length - offset) >= length ? length : (total_length - offset)) + 1;
char *tmp;
if (NULL == (tmp=(char*) malloc(real_length * sizeof(char))))
printf("Memory overflow . \n");
exit(0);
strncpy(tmp, srcstr+offset, real_length - 1);
tmp[real_length - 1] = '\0';
return tmp;
#define ENCODE 1
#define IN_PCM(a) strcat(a,"/file/input.pcm")
#define OUT_SPX(a) strcat(a,"/file/out.spx")
#define OUT_PCM(a) strcat(a,"/file/out.pcm")
int main(int argc, char **argv)
char* path;
char* dist;
path = argv[0];
char* index = strstr(path,PROJECT_PATH);
long diff = index-path;
char *pathDir = mysubstr(path,0,substr(path,PROJECT_PATH)+9);
// 创建一个字符串数组
char arr1[64] = 0;
strcpy(arr1, pathDir);
char arr2[64] = 0;
strcpy(arr2, pathDir);
char* temp0=IN_PCM(pathDir);
char* temp=OUT_SPX(arr1);
char* tempOut=OUT_PCM(arr2);
// 创建一个字符串数组
char arr0[64] = 0;
strcpy(arr0, temp0);
printf("after arr0=%s\n,arr1=%s\n,arr2=%s\n",arr0,arr1,arr2);
return 0;
c语言读取文件的路径怎么设定
有如下程序,运行后输入路径即可读取文本文件,现在我想让他实现读取和程序在同一目录下的文件student.dat,请问怎么改?#include <stdio.h>
#include <stdlib.h>#define LINE 1024char *ReadData(FILE *fp, char *buf)
return fgets(buf, LINE, fp);//读取一行到buf
void someprocess(char *buf)
printf("%s", buf);
void main()
FILE *fp;
char *buf, filename[20], *p; printf("input file name:");
gets(filename);
if ((fp=fopen(filename, "r"))==NULL)
printf("open file error!!\n");
return;
buf=(char*)malloc(LINE*sizeof(char)); while(1)
p=ReadData(fp, buf);//每次调用文件指针fp会自动后移一行
if(!p)//文件读取结束则跳出循环
break;
someprocess(buf);
char** getFileNameArray(const char *path, int* fileCount)
int count = 0;
char **fileNameList = NULL;
struct dirent* ent = NULL;
DIR *pDir;
char dir[512];
struct stat statbuf;
//打开目录
if ((pDir = opendir(path)) == NULL)
myLog("Cannot open directory:%s\n", path);
return NULL;
//读取目录
while ((ent = readdir(pDir)) != NULL)
//统计当前文件夹下有多少文件(不包括文件夹)
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
count++;
//while
//关闭目录
closedir(pDir);
// myLog("共%d个文件\n", count);
//开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间
if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL)
myLog("Malloc heap failed!\n");
return NULL;
//打开目录
if ((pDir = opendir(path)) == NULL)
myLog("Cannot open directory:%s\n", path);
return NULL;
//读取目录
int i;
for (i = 0; (ent = readdir(pDir)) != NULL && i < count;)
if (strlen(ent->d_name) <= 0)
continue;
//得到读取文件的绝对路径名
snprintf(dir, 512, "%s/%s", path, ent->d_name);
//得到文件信息
lstat(dir, &statbuf);
//判断是目录还是文件
if (!S_ISDIR(statbuf.st_mode))
if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1))
== NULL)
myLog("Malloc heap failed!\n");
return NULL;
memset(fileNameList[i], 0, strlen(ent->d_name) + 1);
strcpy(fileNameList[i], ent->d_name);
myLog("第%d个文件:%s\n", i, ent->d_name);
i++;
//for
//关闭目录
closedir(pDir);
*fileCount = count;
return fileNameList 参考技术A "student.dat",或者直接设定它的路径,比如在C盘WINDOWS下就是:"C:\\WINDOWS\\student.dat"注意要多加\,也就是说是\\
以上是关于c语言获取当前项目路径的文件的主要内容,如果未能解决你的问题,请参考以下文章