I have this code:
我有这个代码:
#include<stdio.h>
#include<stdlib.h>
char *K2G(int k)
{
static char g[10];
if (k > 1048576) {
sprintf(g, "%.2fGB", (float) k / 1048576);
} else {
if (k > 1024) {
sprintf(g, "%.2fMB", (float) k / 1024);
} else {
sprintf(g, "%dKB", k);
}
}
printf("%s\n", g);
return g;
}
main()
{
FILE *fp;
int imt = 0, imf = 0, imu = 0;
char cmt[40], cmf[40], cmti[20], cmfi[20], a[20], b[20];
while (1) {
system("clear");
fp = fopen("/proc/meminfo", "r");
fgets(cmt, 40, fp);
fgets(cmf, 40, fp);
fclose(fp);
printf("%s%s\n", cmf, cmt);
sscanf(cmt, "%s%d%s", a, &imt, b);
sscanf(cmf, "%s%d%s", a, &imf, b);
imu = imt - imf;
printf("%s/%s=%d%\n", K2G(imu), K2G(imt), imu * 100 / imt);
sleep(1);
}
}
The output I get is something like:
我得到的输出是这样的:
MemFree: 494256 kB
MemTotal: 10258000 kB
9.78GB
9.31GB
9.31GB/9.31GB=95%
The last line always displays the same two values before the equal sign. The output should have been:
最后一行始终在等号前显示相同的两个值。输出应该是:
MemFree: 494724 kB
MemTotal: 10258000 kB
9.31GB
9.78GB
9.31GB/9.78GB=95%
Why do I get duplicate values when I call function K2G with printf? This is the line giving me the incorrect results:
当我用printf调用函数K2G时,为什么会出现重复值?这条线给我的结果不正确:
printf("%s/%s=%d%\n", K2G(imu), K2G(imt), imu * 100 / imt);
1 个解决方案
#1
2
This behaviour is normal, you return a pointer to g
which is a static buffer, and on each call this buffer will be overwritten.
这种行为是正常的,你返回一个指向g的指针,这是一个静态缓冲区,并且在每次调用时,这个缓冲区都会被覆盖。
So if you do printf(..., K2G(x), K2G(Y),...)
, the parameters "seen" by printf will both be the same g
buffer with it's latest content.
因此,如果你做printf(...,K2G(x),K2G(Y),...),printf“看到”的参数将与它的最新内容是相同的g缓冲区。
You can do this:
你可以这样做:
char simu[20];
char simt[20];
strcpy(simu, K2G(imu));
strcpy(simt, K2G(imt));
printf(..., simu, simt,...);
EDIT:
编辑:
Or you can use another pattern where you have to provide a buffer to K2G:
或者您可以使用另一种模式,您必须为K2G提供缓冲区:
char *K2G(int k, char *g)
{
if (k > 1048576) {
sprintf(g, "%.2fGB", (float) k / 1048576);
} else {
if (k > 1024) {
sprintf(g, "%.2fMB", (float) k / 1024);
} else {
sprintf(g, "%dKB", k);
}
}
printf("%s\n", g);
return g;
}
...
char simu[20];
char simt[20];
K2G(imu, simu);
K2G(imt, simt);
printf(..., simu, simt,...);
This is more transparent and avoids the usage of strcpy
.
这更透明,避免使用strcpy。
#1
2
This behaviour is normal, you return a pointer to g
which is a static buffer, and on each call this buffer will be overwritten.
这种行为是正常的,你返回一个指向g的指针,这是一个静态缓冲区,并且在每次调用时,这个缓冲区都会被覆盖。
So if you do printf(..., K2G(x), K2G(Y),...)
, the parameters "seen" by printf will both be the same g
buffer with it's latest content.
因此,如果你做printf(...,K2G(x),K2G(Y),...),printf“看到”的参数将与它的最新内容是相同的g缓冲区。
You can do this:
你可以这样做:
char simu[20];
char simt[20];
strcpy(simu, K2G(imu));
strcpy(simt, K2G(imt));
printf(..., simu, simt,...);
EDIT:
编辑:
Or you can use another pattern where you have to provide a buffer to K2G:
或者您可以使用另一种模式,您必须为K2G提供缓冲区:
char *K2G(int k, char *g)
{
if (k > 1048576) {
sprintf(g, "%.2fGB", (float) k / 1048576);
} else {
if (k > 1024) {
sprintf(g, "%.2fMB", (float) k / 1024);
} else {
sprintf(g, "%dKB", k);
}
}
printf("%s\n", g);
return g;
}
...
char simu[20];
char simt[20];
K2G(imu, simu);
K2G(imt, simt);
printf(..., simu, simt,...);
This is more transparent and avoids the usage of strcpy
.
这更透明,避免使用strcpy。