C++ 如何使用标题fcntl.h和unistd.h读取和写入文件?

C++ 如何使用标题fcntl.h和unistd.h读取和写入文件?,c++,c,file-io,fcntl,C++,C,File Io,Fcntl,我正在尝试学习如何使用头文件和。我创建了一个小示例来测试他们的程序的工作原理,但它没有按预期工作。这是我的密码: #include <fcntl.h> #include <unistd.h> int main() { int in=open( "test.in", O_RDONLY, S_IREAD ); int *a=new int[ 10 ]; read( in, a, 10 ); int out=open( "test.out", O_WRONLY

我正在尝试学习如何使用头文件
。我创建了一个小示例来测试他们的程序的工作原理,但它没有按预期工作。这是我的密码:

#include <fcntl.h>
#include <unistd.h>

int main() {
  int in=open( "test.in", O_RDONLY, S_IREAD );
  int *a=new int[ 10 ];
  read( in, a, 10 );
  int out=open( "test.out", O_WRONLY, S_IWRITE );
  write( out, a, 10 );
  close( in ); close( out );
  return 0;
}
#包括
#包括
int main(){
int in=打开(“test.in”,O_RDONLY,S_IREAD);
int*a=新的int[10];
读(in,a,10);
int out=打开(“test.out”,O_WRONLY,S_IWRITE);
写出(a,10);
关闭(入);关闭(出);
返回0;
}
输入文件是:
12345678910

程序编译正常,但没有创建任何输出文件。谁能告诉我我的代码有什么问题吗?提前感谢。

来自

#包括
...
int-fd;
模式=S|u IRUSR | S|u IWUSR | S|u IRGRP | S|IROTH;
char*filename=“/tmp/file”;
...
fd=打开(文件名,O|u WRONLY | O|u CREAT | O|u TRUNC,模式);
...
只需与您的代码进行比较。

来自

#包括
...
int-fd;
模式=S|u IRUSR | S|u IWUSR | S|u IRGRP | S|IROTH;
char*filename=“/tmp/file”;
...
fd=打开(文件名,O|u WRONLY | O|u CREAT | O|u TRUNC,模式);
...

只需与您的代码进行比较。

您用于打开第二个文件的标志,
O\u WRONLY
将不会创建输出文件。如果此文件不存在,您可能需要尝试其他标志,如
O\u create
O\u APPEND
。 这对您应该很有用,因为您可能希望在写入文件时使用几个标志来处理文件创建和行为!
好运=)

打开第二个文件时使用的标志,
O\u WRONLY
不会创建输出文件。如果此文件不存在,您可能需要尝试其他标志,如
O\u create
O\u APPEND
。 这对您应该很有用,因为您可能希望在写入文件时使用几个标志来处理文件创建和行为! 祝你好运。

分得一杯羹

写作部分:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
  int const a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int const out { open( "testnums.out", 
          O_WRONLY | O_CREAT, S_IWRITE | S_IREAD) };
  if(out==-1) {
    perror("Cannot open file");
    return 1;
  }
  ssize_t const written { write( out, a, sizeof(a) ) };
  if(written<0) {
    perror("Write error");
  }
  close( out );

  return 0;
}
它写出“a”数组:

$ hexdump testnums.out 
0000000 0001 0000 0002 0000 0003 0000 0004 0000
0000010 0005 0000 0006 0000 0007 0000 0008 0000
0000020 0009 0000 000a 0000                    
0000028
请注意,这是不可移植的-每个编译器/体系结构在这里可能有一些不同的输出

下面是再次阅读并将其写入stdout的部分:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main() {

  int const in { open( "testnums.out", O_RDONLY ) };

  if(in==-1) {
    perror("Cannot open file");
    return 1;
  }

  int a[10];
  ssize_t const r { read( in, a, sizeof(a) ) };
  if(r!=sizeof(a)) {
    fprintf(stderr, "Could not read complete array.");
    return 1;
  }
  if(r<0) {
     perror("Read error");
     close(in);
     return 1;
  }
  close(in);

  for(unsigned int i(0); i<sizeof(a)/sizeof(int); ++i) {
    printf("%d ", a[i]);
  }
  printf("\n");

  return 0;
 }
常规:在您的代码中有很多小问题(例如:检查返回值完全丢失,没有包含所有需要的头文件,写入了错误的字节数,…),您可能希望阅读不同的手册页,如

$ man 2 open
$ man 2 read
$ man 2 write
$ man 2 close
分裂帝国

写作部分:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>

int main() {
  int const a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  int const out { open( "testnums.out", 
          O_WRONLY | O_CREAT, S_IWRITE | S_IREAD) };
  if(out==-1) {
    perror("Cannot open file");
    return 1;
  }
  ssize_t const written { write( out, a, sizeof(a) ) };
  if(written<0) {
    perror("Write error");
  }
  close( out );

  return 0;
}
它写出“a”数组:

$ hexdump testnums.out 
0000000 0001 0000 0002 0000 0003 0000 0004 0000
0000010 0005 0000 0006 0000 0007 0000 0008 0000
0000020 0009 0000 000a 0000                    
0000028
请注意,这是不可移植的-每个编译器/体系结构在这里可能有一些不同的输出

下面是再次阅读并将其写入stdout的部分:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main() {

  int const in { open( "testnums.out", O_RDONLY ) };

  if(in==-1) {
    perror("Cannot open file");
    return 1;
  }

  int a[10];
  ssize_t const r { read( in, a, sizeof(a) ) };
  if(r!=sizeof(a)) {
    fprintf(stderr, "Could not read complete array.");
    return 1;
  }
  if(r<0) {
     perror("Read error");
     close(in);
     return 1;
  }
  close(in);

  for(unsigned int i(0); i<sizeof(a)/sizeof(int); ++i) {
    printf("%d ", a[i]);
  }
  printf("\n");

  return 0;
 }
常规:在您的代码中有很多小问题(例如:检查返回值完全丢失,没有包含所有需要的头文件,写入了错误的字节数,…),您可能希望阅读不同的手册页,如

$ man 2 open
$ man 2 read
$ man 2 write
$ man 2 close

UH您的代码缺少所有错误检查。把它们放进去,你就不必问别人出了什么事;你的程序会告诉你的!额外问题:您的输入文件不包含10
int
s,您的代码也不尝试读取10
int
s(它读取10个字节)。@melpomene您能告诉我如何修复您提到的问题吗?@RondogiannisAristophanes阅读手册。您的代码缺少所有错误检查。把它们放进去,你就不必问别人出了什么事;你的程序会告诉你的!额外问题:您的输入文件不包含10
int
s,您的代码也不尝试读取10
int
s(它读取10个字节)。@melpomene您能告诉我如何修复您提到的问题吗?@RondogiannisAristophanes阅读手册我正在复制您的代码,您的
#include
s但是我得到了关于模式参数
s#IREAD
s#IREAD
“的错误不在此范围内。我尝试了
s#uiwusr
s#uirusr
我正在复制您的代码,您的
#include
s但是我得到了关于模式参数
s#IREAD
的错误。”不在此范围内。我尝试了
S_IWUSR
S_IRUSR
tooThis拒绝了我的权限。这会拒绝我的权限。