텍스트 파일의 문자를 '?'로 바꿔야합니다. 예상대로 작동하지 않습니다.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,©,1); //text file containing 'abc' puts '?' were 'b' is.
}
close(file);
}
파일 "데이터는"ABC, 내가 와을 대체 할 포함? 및 확인이 ? BC하지만 난 오른쪽 문자를 읽고()
읽기 c를? 받고 있어요,하지만 쓰기() 다음 문자로 쓰고있다. 이유가 무엇인가요?
몇 시간 동안 google을 검색했습니다.
감사합니다.
쓴 맛을 경험하고, 반갑지 않은 코드를 찾는 것이 짜증나게됩니다 (아무도 작업 코드에 도움이 필요하지 않습니까?).그래서 : if ** if (buffer == 'a') ** put ** lseek (file, -1, 1); ** 그리고 작동합니다. – Pygar