C fopen()的这些字符串是否正确连接?

C fopen()的这些字符串是否正确连接?,c,unix,fopen,strcpy,strcat,C,Unix,Fopen,Strcpy,Strcat,我想连接两个字符串config_path和config_file,并将该字符串传递到fopen()。问题是,fopen()返回错误,即使我100%确认文件存在。事实上,在将字符串传递到fopen()之前,我会在命令行中打印该字符串,如果我将该字符串直接复制到源代码中,则fopen()会找到该文件。这里有什么问题 命令行输出 config: /nfs/stak/students/m/morriluk/.myshellrc|header: HOME Unable to open configurat

我想连接两个字符串
config_path
config_file
,并将该字符串传递到
fopen()
。问题是,
fopen()
返回错误,即使我100%确认文件存在。事实上,在将字符串传递到
fopen()
之前,我会在命令行中打印该字符串,如果我将该字符串直接复制到源代码中,则
fopen()
会找到该文件。这里有什么问题

命令行输出

config: /nfs/stak/students/m/morriluk/.myshellrc|header: HOME
Unable to open configuration file: No such file or directory
源代码

  1 #define _POSIX_C_SOURCE 200908L
  2 #define SHELL_BSIZE 1024
  3 #define BSIZE 128
  4 #define CONFIG_FILE "/.myshellrc"
  5
  6 #include <stdio.h>
  7 #include <stdlib.h>
  8 #include <string.h>
  9 #include <dirent.h>
 10 #include <unistd.h>
 11 #include <errno.h>
 12 #include <sys/types.h>
 13 #include <sys/wait.h>
 14
 15 char *WARNINGS = "0";
 16
 17 int main()
 18 {
 19     int i;
 20     char buffer[BSIZE], *arg = NULL;
 21     FILE *f;
 22     char *config_path, *str, *config_file = CONFIG_FILE;
 23     char *header = "HOME";
 24
 25     config_path = getenv("HOME");
 26
 27     str = malloc((strlen(config_path)+strlen(config_file))*sizeof(char));
 28     strcpy(str, config_path);
 29     strcat(str, config_file);
 30
 31     printf("config: %s|header: %s\n", str, header);
 32
 33     for (i = 0; i < BSIZE; i++)
 34         buffer[i] = '\0';
 35
 36     if ((f = fopen(str, "r")) != NULL){
 37
 38     }
 39     else {
 40         perror("Unable to open configuration file");
 41     }
 42     return 0;
 43 }
1#定义POSIX_C_SOURCE200908L
2.定义外壳尺寸1024
3#定义BSIZE 128
4#定义配置文件“/.myshellrc”
5.
6#包括
7#包括
8#包括
9#包括
10#包括
11#包括
12#包括
13#包括
14
15字符*警告=“0”;
16
17 int main()
18 {
19国际一级;
20字符缓冲区[BSIZE],*arg=NULL;
21文件*f;
22 char*config\u path,*str,*config\u file=config\u file;
23 char*header=“HOME”;
24
25 config_path=getenv(“HOME”);
26
27 str=malloc((strlen(config_path)+strlen(config_file))*sizeof(char));
28 strcpy(str,配置路径);
29 strcat(str,配置文件);
30
31 printf(“配置:%s |头:%s\n”,str,头);
32
33表示(i=0;i
通过从ENV获取路径打开文件的示例程序

OpenFile()
{
string filename;
FILE      *fp;
filename = string(getenv("HOME")) + "/FileName"; //Home - Configpath, Replace File Name
fp = fopen(filename, "r");
if (fp == NULL ) cout<<"filename read failed"<<endl;
/* Write Code to perform task on File */
fclose(fp); //Close handle 
}

若上面的代码并没有解决问题,让我知道unix风格和名为的绝对文件路径,通过从ENV获取路径打开文件的示例程序

OpenFile()
{
string filename;
FILE      *fp;
filename = string(getenv("HOME")) + "/FileName"; //Home - Configpath, Replace File Name
fp = fopen(filename, "r");
if (fp == NULL ) cout<<"filename read failed"<<endl;
/* Write Code to perform task on File */
fclose(fp); //Close handle 
}

如果上述代码不能解决问题,请告诉我unix风格和名为

的绝对文件路径您没有在
str
中为空终止符预留空间,是吗?将
1
添加到缓冲区以终止
0
。确保
config\u路径
以“\\”结尾。试着打印
str
。就这么简单吗?我在这上面浪费了4个小时。。。非常感谢您的帮助您没有在
str
中为空终止符预留空间,是吗?将
1
添加到用于终止
0
的缓冲区。确保
config\u路径
以“\\”结尾。试着打印
str
。就这么简单吗?我在这上面浪费了4个小时。。。非常感谢你的帮助