简体   繁体   English

C-printf()无法正常工作?

[英]C - printf() not working correctly?

I'm writing a simple program which takes data about people in this format: name,age,gender,info 我正在编写一个简单的程序,该程序以以下格式接收有关人的数据:姓名,年龄,性别,信息

and it will display them like this [name: , age: , gender: , info: ] 并以如下方式显示它们:[姓名:,年龄:,性别:,信息:]

Here is my code so far: 到目前为止,这是我的代码:

#include <stdio.h>

int main() {
char name[10];
int age;
char gender[2];
char info[50];

while(scanf("%9[^,],%i,%c,%49[^\n]", name, &age, gender, info) == 4) {
    printf("[name: %s, age: %i, gender: %c, info: %s]\n", name, age, gender, info);
}

return 0;

} }

So I decided to write my output to another text file using >. 因此,我决定使用>将输出写入另一个文本文件。 And it does nto display correctly, the ] bracket displays on a new line and [name: by itself. 并且它并不能正确显示,[]括号显示在新行上,而[name:单独显示。

This is my input: 这是我的输入:

 eliza,7,F,likes animals
 bob,9,M,fast at running
 sue,6,F,likes painting

And the output is: 输出为:

 [name: eliza, age: 7, gender: J, info: likes animals
 ]
 [name: 
 bob, age: 9, gender: J, info: fast at running
 ]
 [name: 
 sue, age: 6, gender: J, info: likes painting
 ]

Can someone help? 有人可以帮忙吗? I can't figure out why it prints the data like this, I tried using strstr() to check if any of my variables contained the new line character. 我不知道为什么它会打印这样的数据,我尝试使用strstr()来检查我的任何变量是否包含换行符。

You have two issues. 你有两个问题。 Firstly, I believe this is windows (or at least the file you're reading was created in Windows), which means you have \\r\\n not just \\n at line endings. 首先,我相信这是Windows(或至少你正在阅读在Windows中创建的文件),这意味着你必须\\r\\n不只是\\n的行结尾。 You can fix that by opening the file in text mode but that's unreliable; 您可以通过以文本模式打开文件来解决此问题,但这并不可靠。 it's better to filter the extra \\r out manually. 最好手动过滤掉多余的\\r

That's what puts a newline after each "info" field. 这就是在每个“ info”字段之后添加换行符的原因。

The second problem is that you reject the \\n from the info field, so it's still there as the first character for later "name" fields, which is why you have the extra line break there. 第二个问题是您拒绝了\\n信息字段,因此它仍然是后面的“名称”字段的第一个字符,这就是为什么您在此处有多余的换行符的原因。 To fix it, just put a space at the start of your scanf string (which will swallow any and all whitespace) 要解决此问题,只需在scanf字符串的开头放置一个空格(它将吞没所有空白)

And Inspired points out your third issue (which I hadn't noticed); Inspired指出了您的第三个问题(我没有注意到); you need to treat gender as a character not a string. 您需要将性别视为字符而不是字符串。 The "correct" way to read it is like this: 读取它的“正确”方式是这样的:

char gender; // no need to have an array of characters
scanf( "blah %c blah", &gender );

and print like: 并打印如下:

printf( "blah %c blah", gender );
#include <stdio.h>

int main(void) {
    char name[10];
    int age;
    char gender;//change
    char info[50];

    while(scanf("%9[^,],%i,%c,%49[^\n]%*c", name, &age, &gender, info) == 4) {
        printf("[name: %s, age: %i, gender: %c, info: %s]\n", name, age, gender, info);
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM