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;
}
您可以创建主文件并尝试它,看看它是否适合您。
谢谢
I have this code that basically reads from file and creates new file and write the content from the source to the destination file. It reads the buffer and creates the file, but fwrite
doesn't write the content to the newly created file, I have no idea why.
here is the code. (I have to use only this with _sopen
, its part of legacy code)
#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;
}
You can make main file and try it see if its working for you .
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
将您的代码更改为以下内容,然后报告您的结果:
[编辑]
对于其他发帖者,显示 fwrite 的用法/结果 - 以下的输出是什么?
[/编辑]
Change your code to the following and then report your results:
[edit]
for other posters, to show the usage/results of fwrite - what is the output of the following?
[/edit]
sizeof(buffer) 是指针的大小,即 4,而不是缓冲区中的项目数。
如果 buffer 是一个数组,则 sizeof(buffer) 可能会起作用,因为它返回数组中的字节数。
sizeof(buffer) is the size of the pointer, i.e. 4 and not the number of items in the buffer
If buffer is an array then sizeof(buffer) would potentially work as it returns the number of bytes in the array.
fwrite 的第三个参数是 sizeof(buffer),它是 4 个字节(一个指针)。您需要传入要写入的字节数(lSize)。
更新:看起来您还缺少指示文件应该读/写的标志:_O_RDWR
这对我有用......
The third parameter to fwrite is sizeof(buffer) which is 4 bytes (a pointer). You need to pass in the number of bytes to write instead (lSize).
Update: It also looks like you're missing the flag indicating the file should be Read/Write: _O_RDWR
This is working for me...
由于找不到_sopen的信息,只能查看
man open
。它报告:int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
您的呼叫
_sopen(szdes.c_str(),_O_CREAT,_SH_DENYNO,_S_IREAD | _S_IWRITE);
与其中任何一个都不匹配,您似乎有标志和“某些东西”和模式/什么是 SH_DENY ?man _sopen
的结果是什么?最后,在 fclose 文件指针后,不应该从 _sopen 关闭文件描述符吗?
顺便说一句,您的最后几行应该如下所示:
因为您当前正在写入文件,无论 O_CREAT 后的 fdopen 是否成功。您也在顶部执行相同的操作,无论 RDONLY 文件的 fdopen 是否成功,您都会处理读取(和写入):(
Since I can't find info about _sopen, I can only look at
man open
. It reports:int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
Your call
_sopen(szdes.c_str(),_O_CREAT,_SH_DENYNO,_S_IREAD | _S_IWRITE);
doesn't match either one of those, you seem to have flags and 'something' and modes / what is SH_DENY?What is the result of
man _sopen
?Finally, shouldn't you close the file descriptor from _sopen after you fclose the file pointer?
Your final lines should look like this, btw :
Since you currently write the file regardless of whether or not the fdopen after the O_CREAT succeeded. You also do the same thing at the top, you process the read (and the write) regardless of the success of the fdopen of the RDONLY file :(
您正在使用 C 和 C++ 的混合体。这很令人困惑。
sizeof 运算符不会执行您期望的操作。
You are using a mixture of C and C++. That is confusing.
The sizeof operator does not do what you expect it to do.
看起来像@PJL和<一个href="https://stackoverflow.com/questions/7270742/c-fwrite-dosnt-write-to-text-file-have-no-idea-why/7270808#7270808">@jschroedl 发现真正的问题,但也是一般性问题:
fwrite 指出:
因此,如果返回值小于传递的计数,请使用 费罗尔来了解发生了什么。
Looks like @PJL and @jschroedl found the real problem, but also in general:
Documentation for fwrite states:
So if the return value is less than the count passed, use ferror to find out what happened.