小编Pyg*_*gar的帖子

C,unix并用write(),open()和lseek()覆盖一个char

我需要用'?'替换文本文件中的字符.它没有按预期工作.

该文件的内容为'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,&copy,1); //text file containing 'abc' puts '?' were 'b' is.
    }

close(file);
}
Run Code Online (Sandbox Code Playgroud)

文件"data"包含abc,我想替换一个制作它?bc但是我得到了?c

read()正在读取正确的char,但write() …

c file seek

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

file ×1

seek ×1