我已經編寫了代碼,理想情況下應該從一個文檔中獲取數據,對其進行加密並將其保存在另一個文檔中。與文件描述符代碼中的問題。 C(Linux)
但是當我嘗試執行代碼時,它不會將加密數據放入新文件中。它只是留下了空白。有人請發現代碼中缺少的內容。我試過但我無法弄清楚。
我認爲讀/寫功能有問題,或者我錯誤地實現了do-while循環。
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
int fdin,fdout,n,i,fd;
char* buf;
struct stat fs;
if(argc<3)
printf("USAGE: %s source-file target-file.\n",argv[0]);
fdin=open(argv[1], O_RDONLY);
if(fdin==-1)
printf("ERROR: Cannot open %s.\n",argv[1]);
fdout=open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0644);
if(fdout==-1)
printf("ERROR: %s already exists.\n",argv[2]);
fstat(fd, &fs);
n= fs.st_size;
buf=malloc(n);
do
{
n=read(fd, buf, 10);
for(i=0;i<n;i++)
buf[i] ^= '#';
write(fd, buf, n);
} while(n==10);
close(fdin);
close(fdout);
}
都讀取,並使用相同的文件描述符寫?沒有人使用fd {in,out}? – racetrack 2010-09-05 18:40:25