我正在尝试从文件中读取一些文本,然后使用write()、read()和write()将其写入另一个文件。
这是我的write(),用于要写入的文件(我想创建一个新文件并将其写入):
fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);
这将文件权限设置为我完全不了解的内容。 这是write()的输出:
---------T 1 chaitanya chaitanya 0 -02-11 09:38 test-1
甚至读取权限也被锁定。 我尝试搜索此内容,但找不到任何内容。奇怪的是,write()仍然成功地将数据写入文件。
另外,如果我执行“ chmod 777 test-1”,则事情将再次正常运行。
有人可以让我知道我的公开通话中哪里出问题了吗?
谢谢!
供您参考,我在下面粘贴了完整的程序:
#include
#include
#include
#include
int main () {
char buffer[512], ch;
int fIn, fOut, i;
ssize_t bytes;
FILE *fp = NULL;
//open a file
fIn = open ("test", O_RDONLY);
if (fIn == -1) {
printf("\nfailed to open file.");
return 1;
}
//read from file
bytes = read (fIn, buffer, sizeof(buffer));
//and close it
close (fIn);
printf("\nSuccessfully read %d bytes.\n", bytes);
//Create a new file
fOut = open ("test-1", O_RDWR | O_CREAT | O_SYNC);
printf("\nThese are the permissions for test-1\n");
fflush(stdout);
system("ls -l test-1");
//write to it and close it.
write (fOut, buffer, bytes);
close (fOut);
//write is somehow locking even the read permission to the file. Change it.
system("chmod 777 test-1");
fp = fopen ("test-1", "r");
if (fp == NULL) {
printf("\nCan't open test-1");
return 1;
}
while (1)
{
ch = fgetc(fp);
if (ch == EOF)
break;
printf("\n%c", ch);
}
fclose (fp);
return 0;
}