小编Der*_*rek的帖子

使用路径连接文件以获取C中的完整路径

使用C,我试图将目录中的文件名与它们的路径连接起来,这样我就可以为每个文件调用stat(),但是当我尝试在循环中使用strcat时,它将前一个文件名与下一个文件名连接起来.它在循环中修改了argv [1],但是我很久没有使用过C了,所以我很困惑......

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>

int main(int argc, char *argv[]) {
 struct stat buff;

 int status;

 if (argc > 1) {
  status = stat(argv[1], &buff);
  if (status != -1) {
   if (S_ISDIR(buff.st_mode)) { 
     DIR *dp = opendir(argv[1]);
     struct dirent *ep;
     char* path = argv[1];
     printf("Path = %s\n", path);

     if (dp != NULL) {
       while (ep = readdir(dp)) {
       char* fullpath = strcat(path, ep->d_name);
       printf("Full Path = %s\n", fullpath);
     } …
Run Code Online (Sandbox Code Playgroud)

c unix linux file

3
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

file ×1

linux ×1

unix ×1