#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
int main(int argc,char *argv[])
{
int fd;
int i=1;
for(i=1;i<argc;++i)
{
char temp;
fd=open(argv[i],"O_RDWR");
if (fd==-1)
perror("file:");
while (read(fd,&temp,1)!=EOF)
{
putchar(temp);
}
}
}
我執行./a.out a b
。 a
和b
是我的目錄中的文件。我收到一個錯誤,說File exists
。 行open(argv[i],"O_RDWR")
未打開該文件。當標誌是字符串指針而不是int時,open()函數錯誤
自文件存在以來它返回-1
。那麼我應該如何使用open
系統調用打開文件?
如果你已經編譯了警告,你肯定會得到一個這個程序。 – 2013-02-28 19:02:48
flags參數是一串位,用無符號整數表示。誰知道什麼無意義的標誌通過了... – vonbrand 2013-02-28 19:31:45