[英]Why is the output order changing when I print using printf?
I want to generate an output like this "firstname surname: day.month.year " but what i get is some mixed up order. 我想生成一个像“firstname surname:day.month.year”这样的输出,但我得到的是一些混合顺序。 I am new to C and I don't know what's going on here.
我是C的新手,我不知道这里发生了什么。
So this is what i get 所以这就是我得到的
This is my code 这是我的代码
char string[imax];
fgets (string, imax, team1); //wo, max count, aus welchem file
int i=1, k=0;
char delimiter[] = " ";
char *day, *month, *year, *firstname, *surname;
char *stats[5];
while(fgets(string,imax,team1) != 0)
{
/*stats[0] = strtok(string,delimiter);
while(i <=4)
{
stats[i] = strtok(NULL,delimiter);
i++;
}*/
day = strtok(string,delimiter);
month = strtok(NULL,delimiter);
year = strtok(NULL,delimiter);
firstname = strtok(NULL,delimiter);
surname = strtok(NULL,delimiter);
printf("%s ", firstname);
printf("%s:", surname);
printf("%s.", day);
printf("%s.", month);
printf("%s. ", year);
}
EDIT: I get the same order as the order in the file i am reading from 编辑:我得到的订单与我正在阅读的文件中的订单相同
First, you need to add a newline after printing the year. 首先,您需要在打印年份后添加换行符。 Either change
要么改变
printf("%s. ", year);
to 至
printf("%s.\n ", year);
or add 或添加
fputc( '\n', stdout );
The second thing is, that after fgets()
the newline of the source text file is part of string
and therefore part of surname
. 第二件事是,在
fgets()
之后,源文本文件的换行符是string
一部分,因此是surname
一部分。 You can handle this by adding '\\n'
to delimiter
(and if it's a Windows text file opened on another system (eg UNIX), '\\r'
too) 你可以通过在
delimiter
添加'\\n'
来处理这个问题(如果它是在另一个系统(例如UNIX)上打开的Windows文本文件),也可以'\\r'
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.