我需要用'?'替换文本文件中的字符.它没有按预期工作.
该文件的内容为'abc'(没有引号),我必须使用unix系统调用:lseek(),open()和write().我不能使用标准的C文件I/O功能.
该计划最终将其扩展为更广泛的"查找和替换"实用程序.
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(){
int file = open("data", O_RDWR); //open file with contents 'abc'
lseek(file,0,0); //positions at first char at beginnging of file.
char buffer;
read(file,&buffer, sizeof(buffer));
printf("%c\n", buffer); // text file containing 'abc', it prints 'a'.
if (buffer == 'a'){
char copy = '?';
write(file,©,1); //text file containing 'abc' puts '?' were 'b' is.
}
close(file);
}
Run Code Online (Sandbox Code Playgroud)
文件"data"包含abc,我想替换一个用?制作它?bc但是我得到了?c
read()正在读取正确的char,但write() …