C++ fwrite 不写入文本文件,不知道为什么?

标签 c++ fwrite

我有这段代码基本上从文件中读取并创建新文件并将内容从源文件写入目标文件。它读取缓冲区并创建文件,但是 fwrite
没有将内容写入新创建的文件,我不知道为什么。
这是代码。 (我必须只将它与 _sopen 一起使用,它是遗留代码的一部分)

#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <string>
#include <share.h>
#include <sys\stat.h>


int main () {
  std::string szSource = "H:\\cpp\\test1.txt";
  FILE* pfFile;
  int iFileId = _sopen(szSource.c_str(),_O_RDONLY, _SH_DENYNO, _S_IREAD);
  if (iFileId >= 0) 
     pfFile = fdopen(iFileId, "r");
   //read file content to buffer 
   char * buffer;
   size_t result;
   long lSize;
   // obtain file size:
   fseek (pfFile , 0 , SEEK_END);
   lSize = ftell (pfFile);
   fseek(pfFile, 0, SEEK_SET);
 //   buffer = (char*) malloc (sizeof(char)*lSize);
   buffer = (char*) malloc (sizeof(char)*lSize);

   if (buffer == NULL)
   {

       return false;
   }

   // copy the file into the buffer:
   result = fread (buffer,lSize,1,pfFile);   
   std::string szdes = "H:\\cpp\\test_des.txt";
   FILE* pDesfFile;
   int iFileId2 = _sopen(szdes.c_str(),_O_CREAT,_SH_DENYNO,_S_IREAD | _S_IWRITE);
  if (iFileId2 >= 0) 
     pDesfFile = fdopen(iFileId2, "w+");

   size_t f = fwrite (buffer , 1, sizeof(buffer),pDesfFile );
   printf("Error code: %d\n",ferror(pDesfFile));

   fclose (pDesfFile);

  return 0;
}

您可以创建主文件并尝试它是否适合您。
谢谢

最佳答案

将您的代码更改为以下内容,然后报告您的结果:

int main () {
  std::string szSource = "H:\\cpp\\test1.txt";
  int iFileId = _sopen(szSource.c_str(),_O_RDONLY, _SH_DENYNO, _S_IREAD);
  if (iFileId >= 0) 
  {
    FILE* pfFile;
    if ((pfFile = fdopen(iFileId, "r")) != (FILE *)NULL)
    {
      //read file content to buffer 
      char * buffer;
      size_t result;
      long lSize;
      // obtain file size:
      fseek (pfFile , 0 , SEEK_END);
      lSize = ftell (pfFile);
      fseek(pfFile, 0, SEEK_SET);
      if ((buffer = (char*) malloc (lSize)) == NULL)
        return false;

      // copy the file into the buffer:
      result = fread (buffer,(size_t)lSize,1,pfFile);   
      fclose(pfFile);

      std::string szdes = "H:\\cpp\\test_des.txt";
      FILE* pDesfFile;
      int iFileId2 = _sopen(szdes.c_str(),_O_CREAT,_SH_DENYNO,_S_IREAD | _S_IWRITE);
      if (iFileId2 >= 0) 
      {
        if ((pDesfFile = fdopen(iFileId2, "w+")) != (FILE *)NULL)
        {
          size_t f = fwrite (buffer, (size_t)lSize, 1, pDesfFile);
          printf ("elements written <%d>\n", f);

          if (f == 0)
            printf("Error code: %d\n",ferror(pDesfFile));

          fclose (pDesfFile);
        }
      }
    }
  }

  return 0;
}

[编辑]

对于其他海报,显示 fwrite 的用法/结果 - 下面的输出是什么?

#include <stdio.h>

int main (int argc, char **argv) {
   FILE *fp = fopen ("f.kdt", "w+");

   printf ("wrote %d\n", fwrite ("asdf", 4, 1, fp));

   fclose (fp);
}

[/编辑]

关于C++ fwrite 不写入文本文件,不知道为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7270742/

相关文章:

c++ - 递归函数上的 OpenMP 并行化

c++ - Qt Visual Studio 插件是 Qt Visual Studio 集成的子集吗?

c - 在 Linux 上使用 fwrite 作为原子进程

c - 如何使用 write/fread 向结构数组写入/读取数据?

c++ - fwrite 不在 C 中保存完整数组

c++ - 检查该值是否已存在于 vector 中

c++ - 创建单个 .o 文件

c - 为什么此代码不更新文件?

c - 设置 uint32 key 位置并将其写入文件时遇到问题

c++ - 范围基数并插入 vector C++11