Linux 将相对路径传递给 fopen()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9120842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:21:14  来源:igfitidea点击:

Pass a relative path to fopen()

clinuxfile-io

提问by Dino55

I am trying to pass a relative path to fopen(), but it can't seem to find the file. I need this to work on Linux. The filenames (ex: t1.txt) are saved in an array. So all I need is the "front part" of a relative path.

我试图将相对路径传递给 fopen(),但它似乎无法找到该文件。我需要它在 Linux 上工作。文件名(例如:t1.txt)保存在一个数组中。所以我需要的只是相对路径的“前部”。

Here's my code:

这是我的代码:

// Use strcat to create a relative file path
char path[] = "./textfiles/"; // The "front part" of a relative path
strcat( path, array[0] ); // array[0] = t1.txt

// Open the file
FILE *in;
in = fopen( "path", " r " );
if (!in1) 
{
    printf("Failed to open text file\n");
    exit(1);
}

采纳答案by pmg

in = fopen("path", " r ");

Two wrongs right there:

那里有两个错误:

  1. You don't want to open the file literally named "path", you want the file whose name is in the variable path
  2. the mode argument is invalid; you want "r"
  1. 您不想打开字面上名为“path”的文件,而是想要名称在变量中的文件 path
  2. mode 参数无效;你要"r"

To get them right do

为了让他们正确做

in = fopen(path, "r");

回答by dasblinkenlight

First, you need to add some space to pathin order to fit the content of array[0]in strcat, otherwise you'll be writing past the allocated area.

首先,您需要添加一些空间 topath以适合array[0]in的内容strcat,否则您将写过分配的区域。

Second, you are not passing pathto fopen, because you enclosed "path"in double quotes.

其次,您没有传递pathfopen,因为您"path"用双引号括起来。

char path[100] = "./textfiles/"; // Added some space for array[0]
strcat( path, array[0] );

// Open the file
FILE *in;
in = fopen( path, " r " ); // removed quotes around "path"
if (!in) 
{
    printf("Failed to open text file\n");
    exit(1);
}

回答by Kerrek SB

Your problem has nothing to do with fopenand everything with C string handling: strcatrequires enough memory in the destination buffer for the entire string, but you only provide enough memory for the initialstring. Bad! Using tools like valgrindwould tell you about your illegal access immediately, by the way.

您的问题fopen与 C 字符串处理无关,一切都与 C 字符串处理无关:strcat在整个字符串的目标缓冲区中需要足够的内存,但您只为初始字符串提供足够的内存。坏的!valgrind顺便说一下,使用类似的工具会立即告诉您您的非法访问。

Here's a naive solution:

这是一个天真的解决方案:

char buf[1000] = { };
strcat(buf, "./textfiles/");
strcat(buf, array[0]);

// ...

回答by hmjd

pathis only large enough to contain the characters it has been initialised with. The size of pathmust be increased. Allocate memory for pathbased on the filename being appended:

path仅足以包含已初始化的字符。path必须增加的大小。path根据附加的文件名分配内存:

const char* dir = "./textfiles/";
const size_t path_size = strlen(dir) + strlen(array[0]) + 1;
char* path = malloc(path_size);
if (path)
{
    snprintf(path, path_size, "%s%s", dir, array[0]);

    in = fopen(path, "r"): /* Slight differences to your invocation. */

    free(path);
}