我试图将变量写入文件,但我不断收到语法错误,或者它不会将其打印到文件中。
我有 info.uptime,我想将其打印到“file.txt”,所以我尝试了:
int fd = open("file.txt", O_WRONLY);
write(fd, info.uptime, length(info.uptime));
长度函数是我创建的用于查看数字长度的函数。 在文件中,数字消失了,所以我尝试了:
int fd = open("file.txt", O_WRONLY);
write(fd,("%lu" ,info.uptime), length(info.uptime));
现在它给了我太多参数的语法错误。 我该怎么做?
在 ubuntu 上
编辑:我只能使用:
#include <unistd.h>
#include <sys/sysinfo.h>
#include <sys/syscall.h>
#include "helper.h"
编辑2:
int length(int n)
{
int i;
for (i=0;n>=1;i++)
{
n = n/10;
}
return i;
}
最佳答案
你的语法错误。类型:
man -s 3 write
从命令行。
<小时/>编辑:答案不再有意义,因为问题已更新为使用正确的语法。
关于c - 用C编写系统调用,如何将整数值打印到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22358620/