我想从文件中读取一个数字并增加数字而不是将其写入文件。我想使用读/写而不是 fscanf/fprintf。我试图将整数更改为字符串,但我在 Google 上发现 linux 中不存在 itoa。这是我的代码:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main(int argc,char *argv[]){
int f;
f=open(argv[1],O_RDWR);
char c[10];
read(f,c,10);
int ceva=atoi(c);
printf("%d ",ceva);
ceva++;
//itoa (ceva,c,10);
lseek(f,0,SEEK_SET);
write(f,ceva,sizeof(int));
}
最佳答案
你对 fprintf
有什么看法?您需要以某种方式将整数转换为字符串。另一种选择是使用 snprintf
将整数转换为字符串,然后 write
字符串。
关于c - Linux写整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448025/