提问人:Daniel Boos 提问时间:4/21/2023 最后编辑:Yakov GalkaDaniel Boos 更新时间:4/22/2023 访问量:117
在 printf 中的引号(格式)之间添加逗号会改变输出?
Adding comma between quotes (format) in printf changes the output?
问:
我面临着一个非常奇怪的问题,我只能在 linux 上生成。我真的在互联网上找不到类似的东西,但如果我的搜索技巧不够好,如果这是任何问题的重复,我深表歉意。
采用以下最小可行代码: 在仍然有问题的情况下,我尽可能地削减了开支。
#include <stdio.h>
#define MAX 10
#define MAXNAME 30
typedef struct sup{
int rating;
char supName[MAXNAME];
}sup;
typedef struct spec{
char name[MAXNAME];
float width;
float height;
float depth;
char material[MAXNAME];
sup supvisor[MAX];
}spec;
int read_Supervisors(char fileName[], spec *specread){
int i=0;
char buffer[MAX];
FILE *supervisors = fopen(fileName, "r");
while (i < MAX && fgets (buffer, sizeof(buffer), supervisors)) {
if(sscanf(buffer, "%19s %d", specread->supvisor[i].supName, &specread->supvisor[i].rating) == 2){
i++;}}
fclose(supervisors);
return i;
}
void read_Specs(char fileName[],spec *spec1){
FILE *specs = fopen(fileName, "r");
fscanf(specs, "%29[^\n]\n %19s\n %f\n %f\n %f", spec1->name, spec1->material,&spec1->width, &spec1->height, &spec1->depth);
fclose(specs);
}
int main(int argc, char *argv[]) {
spec spec1 = {.name = ""}; // Initialize .name at zero
//Read specs file.
read_Specs(argv[1],&spec1);
int iteration = read_Supervisors(argv[2], &spec1),
selectedSuper = 1,
calculatedDays = 50;
float priceOfProject = 1;
//Print all the values.
printf("Dear %s,"
"\nThank you for your order.\n"
"Your retaining wall of specifications:\n"
"Material: %s\n"
"width: %.2f m\n"
"height: %.2f m\n"
"depth: %.3f m\n"
"Will be completed in %d days and the assigned project supervisor is %s\n"
"The estimated price is: $%.2f\n", spec1.name, spec1.material, spec1.width,spec1.height,spec1.depth,calculatedDays, spec1.supvisor[selectedSuper].supName,priceOfProject);
return 0;
}
输出:
,ear Jason Oliver
Thank you for your order.
Your retaining wall of specifications:
Material: Concrete
width: 5.50 m
height: 1.00 m
depth: 0.250 m
Will be completed in 50 days and the assigned project supervisor is Sally
The estimated price is: $1.00
请注意,打印出来的是 ,ear 而不是 Dear Jason oliver,
删除逗号时,它可以完美地工作。我可以在逗号之前使用逗号,但不能在逗号之后使用逗号。Dear %s,
Dear %s
%s
我尝试了很多东西,增加了 #defines,我尝试过拆分为多个 printf,我尝试了不同版本的 C11、C23,我还尝试更新了我的 linux。我要指出的是,这是 linux 的全新安装。
以下是我正在使用的 linux(22.04) 环境的一些信息: Linux Linux 5.19.0-35-generic #36~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC 周五 2 月 17 日 15:17:25 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
编译器版本: gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
文件结构:
规格 .dat 文件:
Jason Oliver
Concrete
5.50
1.0
0.250
生成器 .dat 文件:
Bob 50
Sally 78
Jian 69
Hecctor 89
答:
您的输入文件必须使用 Windows 上流行的行尾,而不是 Unix 使用的单行。您的代码将名称读取到字符,因此成为 .当您将名称打印到终端时,指示它将光标移动到行的开头(它是回车控制字符)。在此之后打印的下一个字符是逗号,因此它会覆盖位于同一位置的“Dear”的“D”。\r\n
\n
\n
\r
name
\r
您也可以修复 your to exclude from the string。请注意,在这种情况下,格式字符串中的以下内容不正确。一个简单的空格就可以跳过 和 ,以及其他空格字符:fscanf
\r
\n
\r
\n
fscanf(specs, "%29[^\r\n] %19s %f %f %f", spec1->name, spec1->material,&spec1->width, &spec1->height, &spec1->depth);
或者,如果这是一个选项,您可以使用适用于大多数操作系统的命令修复输入文件:dos2unix
$ dos2unix specs.dat
评论
'\n'
'\r\n'
fopen()
\n
\r\n
\n
fread
chars
ftell
%[^\r\n]
'\r'
'\n'
评论
#define MAX 10
太小,无法处理 Builders.dat 中最长的行char buffer[MAX];
那么哪里是超过 10 个字符。fgets (buffer, sizeof(buffer), supervisors)
Hecctor 89