テキストファイルの文字を '?'に置き換える必要があります。期待どおりに機能しません。C、unix、write()、open()、lseek()でcharを上書きする
ファイルには 'abc'(引用符なし)のファイルがあり、lseek()、open()、write()というUNIXシステムコールを使用する必要があります。標準の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