gpt4 book ai didi

c - f 打开文件并跳过字符

转载 作者:行者123 更新时间:2023-11-30 16:48:47 24 4
gpt4 key购买 nike

是否有一种干净的方法可以在不进行系统调用的情况下打开这样的文件:

ID*_LogConfig.csv

我尝试了以下方法,但没有成功。

/*Read setup file*/
getcwd(cwd, sizeof(cwd));
snprintf(source, sizeof(source),"%s/ID%*d_LogConfig.csv",cwd);
if( NULL == (input = fopen(source,"r")))
{
snprintf(errbuffer,sizeof(errbuffer), "Could not open file %s - check existence/rights\n", source);
exitHandler(1, errbuffer);
}

它输出:

/mnt/dataflash/canfilter.d/ID*_LogConfig.csv not found

但是例如cat/mnt/dataflash/canfilter.d/ID*_LogConfig.csv 它显示文件内容。

我的折衷解决方案是系统调用 ll ID*_LogConfig.csv 并使用输出作为文件名。

最佳答案

这一行

snprintf(source, sizeof(source),"%s/ID%*d_LogConfig.csv",cwd);

可能不会产生您认为的效果。

%*d 部分是一个格式说明符,其字段宽度为 per the POSIX printf() documentation

A field width, or precision, or both, may be indicated by an ( '*' ). In this case an argument of type int supplies the field width or precision. Applications shall ensure that arguments specifying field width, or precision, or both appear in that order before the argument, if any, to be converted. A negative field width is taken as a '-' flag followed by a positive field width. A negative precision is taken as if the precision were omitted. In format strings containing the "%n$" form of a conversion specification, a field width or precision may be indicated by the sequence "*m$", where m is a decimal integer in the range [1,{NL_ARGMAX}] giving the position in the argument list (after the format argument) of an integer argument containing the field width or precision, for example:

printf("%1$d:%2$.*3$d:%4$.*3$d\n", hour, min, precision, sec);

所以,这一行

snprintf(source, sizeof(source),"%s/ID%*d_LogConfig.csv",cwd);

期望再传递两个个整数参数。由于您没有传递它们,因此您会调用未定义的行为。

查看这个答案:https://stackoverflow.com/a/19897395/4756299

关于c - f 打开文件并跳过字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42834140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com