c - 通过 snprintf 打开
问题描述
我在编译我的程序时遇到问题,但是,在编译警告时,在尝试压缩strcpy
&strcat
时snprintf
(以删除不必要的代码行)
传递 'snprintf' 的参数 2 从没有强制转换的指针生成整数
呈现自己。我试过四处寻找有这个问题的其他人,但是一旦调整他们的解决方案似乎不适用于我的代码
fileDir
在程序开始时定义为:char fileDir[1000];
event->name
是文件的名称(及其扩展名),例如picture1.jpg。
hashDirectory
是"/home/user/Documents/_Hash"
。
FILE *ftest2=fopen(snprintf(fileDir, "%s: %s: %s", hashDirectory, event->name, ".txt"), "wt");
我可以打开文件,但是只有当我使用strcpy
and strcat
- 这是我想要摆脱的。
解决方案
因此,所有评论都证明了您的代码存在一些问题。
现在我们将跳过fopen
并专注于snprintf
. snprintf
就像printf
除了你需要首先传递两个额外的参数,一个char *
指示在哪里存储渲染的字符数据,一个size_t
指示在char *
. 这样做的惯用方法(假设 achar[]
是目的地)是这样使用sizeof()
的:
int res = snprintf(fileDir, sizeof(fileDir), /* Other arguments omitted */);
snprintf
返回打印的字符数(作为int
)。如果该数字大于或等于,sizeof(fileDir)
则其中的任何内容都fileDir
将被截断(但将始终被NULL
终止)。
if (res >= sizeof(fileDir)) {
/* fileDir contains an incomplete path, handle this as an error */
}
最后,因为的返回值snprintf
是 a int
,所以你不能将它作为第一个参数传递给,fopen
因为它期望第一个参数是 a const char *
。所以这些需要是单独的步骤。
说了这么多,您构建路径的方式似乎也不正确。在下面的完整示例中,我已修复它:
FILE *ftest2;
int res = snprintf(fileDir, sizeof(fileDir), "%s/%s.txt", hashDirectory, event->name);
if (res >= sizeof(fileDir)) {
fprintf(stderr, "The pathname was truncated. Cannot proceed.\n");
return -1;
}
ftest2 = fopen(fileDir, "wt");
if (!ftest2) {
fprintf(stderr, "Failed to open `%s': %s\n", fileDir, strerror(errno));
return -1;
}
/* Rest of your code */
重要说明,如果第一个参数snprintf
是指向动态分配内存的指针(即 from malloc
),您将无法使用sizeof()
,而是需要显式传递动态分配区域的大小。sizeof()
在编译时评估(忽略VLA),而不是在运行时。
(如果你想使用那部分,你需要调用才能#include
string.h
工作errno.h
)strerror()
推荐阅读
- c# - 如何根据所选项目相乘
- python - 从 Pandas Timeseries Dataframe 中的多天中选择特定时间
- r - 增加选择输入的高度
- google-apps-script - 当谷歌电子表格的特定单元格变为“检查校准表”时,我需要一个脚本来自动发送电子邮件
- tensorflow - 如果实际正确类别的概率很小(
我需要关于训练模型的损失(categorical_crossentropy)的输入-目标组合的梯度。对于大多数示例,梯度看起来不错,但网络训练得越深入,出现的输入-目标组合越多,所有梯度都为零。为什么会这样,因为当概率绝对不同于目标时会发生这种情况?
我将网络定义如下:
X_train, Y
- javascript - 输入 addEventListener - 点击有效,onchange/oninput 无效
- regex - 匹配法语字符
- python - 如何在使用已删除项目创建另一个列表时从列表中删除项目?
- c# - Cosmos DB 调用永远不会通过异步调用返回
- c# - 根据 form_Load() 函数推进条形进度