ユーザタイプ$。/ replace i xy data.txt
data.txtには「これはテストファイル、テストファイルのみ」という単語が含まれています。したがって、すべてのiはxyに置き換えられます。つまり、thxys xyはテストfxyle、test fxyleのみテキストファイルの文字を置き換える - 論理エラー?
私はかなり近いと思います。しかし、iをxyに置き換える代わりに、私のコードはiをxに置き換えるだけです。私はエラーが38行strcpyであると思う。 しかし、30行目から40行目まではロジックが正しいですか?私が言って....別のバッファへの最初のバッファ内の各要素(BUF)
コピーbufの要素について
(TEMP)時間
BUF要素は==「i」があれば
1つの文字しようとしています コピーXY「」「i」は
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#define BUFFERSIZE 4096
/*replace i xy data.txt */
int main(int ac, char *av[])
{
int in_fd, out_fd, n_chars, BufElement,j,x;
ssize_t nread,nwrite;
off_t newpos;
char buf[BUFFERSIZE],temp[300];
/*Open file containing original data*/
if ((in_fd=open(av[3], O_RDWR)) == -1)
{
printf("Cannot open %s\n", av[3]);
exit(1);
}
/*Read characters from file to buffer*/
while ((nread = read(in_fd , buf, BUFFERSIZE)) > 0)
{
for (BufElement=0;BufElement < nread;BufElement++)
{
for (j=0; j < strlen(av[1]); j++)
{
temp[BufElement] = buf[BufElement];
if (buf[BufElement] == av[1][j])
strncpy(temp+BufElement,av[2],strlen(av[2])); /*ERROR*/
}
}
}
printf("%s\n",buf);
printf("%s\n",temp);
newpos = lseek(in_fd, 0, SEEK_SET);
nwrite = write(in_fd,temp,36);
close(in_fd);
}
}
なぜホイールを再発明しますか?あなたは 'sed'と一時ファイルの使用を見ましたか? –
はい、私は自分のコマンドを書いています –
'temp [BufElement] = buf [BufElement];行を最も内側のループの外側に置くことができます。 –