[C] 写文件 →→→→→进入此内容的聊天室

来自 , 2020-08-20, 写在 C, 查看 189 次.
URL http://www.code666.cn/view/a70dc404
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys\stat.h>
  5. #include <io.h>
  6. #include <string.h>
  7.  
  8. int main(void)
  9. {
  10.    int handle;
  11.    char string[40];
  12.    int length, res;
  13.  
  14.    /*
  15.     Create a file named "TEST.$$$" in the current directory and write
  16.     a string to it.  If "TEST.$$$" already exists, it will be overwritten.
  17.    */
  18.  
  19.    if ((handle = open("TEST.$$$", O_WRONLY | O_CREAT | O_TRUNC,
  20.                          S_IREAD | S_IWRITE)) == -1)
  21.    {
  22.       printf("Error opening file.\n");
  23.       exit(1);
  24.    }
  25.  
  26.    strcpy(string, "Hello, world!\n");
  27.    length = strlen(string);
  28.  
  29.    if ((res = write(handle, string, length)) != length)
  30.    {
  31.       printf("Error writing to the file.\n");
  32.       exit(1);
  33.    }
  34.    printf("Wrote %d bytes to the file.\n", res);
  35.  
  36.    close(handle);
  37.    return 0;
  38. }
  39.  

回复 "写文件"

这儿你可以回复上面这条便签

captcha