使用相对路径

时间:2016-09-11 02:28:29

标签: c unix path

以下代码应该如下工作:打印目录中的文件列表,并打印每个.c文件的内容。 它在UNIX中为同一目录执行时工作正常:./a.out ./

但是,我无法使其适用于./a.out ../differentDir执行。

我知道如果绝对路径作为参数提供,我可以使用argv [1]。然而,当它以相对路径的形式提供时,我迷失了。

#include <sys/types.h>

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUFFSIZE 32768

int main(int argc, char **argv) {

    char buf[BUFFSIZE];
    DIR *dp;
    struct dirent *dirp;
    char filename[80];
    int  name_length;
    FILE *fp;

    if (argc != 2) {
        fprintf(stderr, "usage: %s dir_name\n", argv[0]);
        exit(1);
    }

    if ((dp = opendir(argv[1])) == NULL ) {
        fprintf(stderr, "can't open '%s'\n", argv[1]);
        exit(1);
    }

        while ((dirp = readdir(dp)) != NULL ){
           printf("%s\n", dirp->d_name);
           memset(filename, '\0', sizeof(filename));
           strcpy(filename, dirp->d_name);
           printf(" ** %s ", filename);
           name_length = strlen(filename);
           printf(" name_length=%d \n", name_length);
            if (findC(filename)) // checking if the file has a .c extension
            {
                fp=fopen(filename, "r");
                if (fp == NULL)
                    fprintf(stderr, "Can't open .C file!\n");
                else
                    {// if the file was opened successfuly:
                        do
                        {
                           fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
                           buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
                           puts(buf);
                        }
                        while (!feof(fp));
                    printf("\n\n");
                    fclose(fp);
                    }
            }
        }
    closedir(dp);
    return(0);
}

/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
    int len = strlen(name);
    if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
        return 1;
    return 0;
}

3 个答案:

答案 0 :(得分:1)

打开要读取的文件时,文件路径名也需要是相对的。

scaled()

答案 1 :(得分:0)

您可以在此示例中使用realpath(argv1...)realpath将返回相对路径的绝对路径。

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main(int argc, char **argv) {
    char *path = "../..";
    char buff[PATH_MAX + 1]; /* not sure about the "+ 1" */
    char *res = realpath(path, buff);
    if (res) {
        printf("This source is at %s.\n", buff);
    } else {
        perror("realpath");
        exit(EXIT_FAILURE);
    }
    return 0;
}

要在程序中包含所需的行为,您可以在代码中使用realpath

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>

#define BUFFSIZE 32768

int main(int argc, char **argv) {

    char buf[BUFFSIZE];
    DIR *dp;
    struct dirent *dirp;
    char filename[80];
    int  name_length;
    FILE *fp;
    char buff[PATH_MAX + 1]; /* not sure about the "+ 1" */

    if (argc != 2) {
        fprintf(stderr, "usage: %s dir_name\n", argv[0]);
        exit(1);
    }
    char *res = realpath(argv[1], buff);
    if ((dp = opendir(res)) == NULL ) {
        fprintf(stderr, "can't open '%s'\n", argv[1]);
        exit(1);
    }

    while ((dirp = readdir(dp)) != NULL ){
        printf("%s\n", dirp->d_name);
        memset(filename, '\0', sizeof(filename));
        strcpy(filename, dirp->d_name);
        printf(" ** %s ", filename);
        name_length = strlen(filename);
        printf(" name_length=%d \n", name_length);
        if (findC(filename)) // checking if the file has a .c extension
        {
            fp=fopen(filename, "r");
            if (fp == NULL)
                fprintf(stderr, "Can't open .C file!\n");
            else
            {// if the file was opened successfuly:
                do
                {
                    fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
                    buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
                    puts(buf);
                }
                while (!feof(fp));
                printf("\n\n");
                fclose(fp);
            }
        }
    }
    closedir(dp);
    return(0);
}

/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
    int len = strlen(name);
    if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
        return 1;
    return 0;
}

答案 2 :(得分:0)

您可以先使用相对路径或绝对路径切换到目录 chdir,然后通过 getcwd 获取绝对路径

#include <sys/types.h>

#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define BUFFSIZE 32768
#define PATH_SIZE 512

int main(int argc, char **argv) {

    char buf[BUFFSIZE];
    char path[PATH_SIZE];
    DIR *dp;
    struct dirent *dirp;
    char filename[80];
    int  name_length, r;
    FILE *fp;

    if (argc != 2) {
        fprintf(stderr, "usage: %s dir_name\n", argv[0]);
        exit(1);
    }
    
    strcpy(path, argv[1]);
    r = chdir(path);
    if( r != 0 )
    {
        printf("Invalid path '%s'\n",path);
        exit(1);
    }
    getcwd(path,PATH_SIZE);

    if ((dp = opendir(path)) == NULL ) {
        fprintf(stderr, "can't open '%s'\n", argv[1]);
        exit(1);
    }

        while ((dirp = readdir(dp)) != NULL ){
           printf("%s\n", dirp->d_name);
           memset(filename, '\0', sizeof(filename));
           strcpy(filename, dirp->d_name);
           printf(" ** %s ", filename);
           name_length = strlen(filename);
           printf(" name_length=%d \n", name_length);
            if (findC(filename)) // checking if the file has a .c extension
            {
                fp=fopen(filename, "r");
                if (fp == NULL)
                    fprintf(stderr, "Can't open .C file!\n");
                else
                    {// if the file was opened successfuly:
                        do
                        {
                           fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
                           buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
                           puts(buf);
                        }
                        while (!feof(fp));
                    printf("\n\n");
                    fclose(fp);
                    }
            }
        }
    closedir(dp);
    return(0);
}

/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
    int len = strlen(name);
    if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
        return 1;
    return 0;
}