Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C/C+中读取和写入字节数组中的文件+;_C++ - Fatal编程技术网

C++ 在C/C+中读取和写入字节数组中的文件+;

C++ 在C/C+中读取和写入字节数组中的文件+;,c++,C++,我试图用C/C++读取一个文件并将其存储在一个字节数组中,一旦数据被存储,我就试图再次将其写回。但是当我写回它时,我不能正确地保存它,我在某些地方丢失了一些字节 例如:如果我试图保存一个2161 KB的文件,那么新创建的文件将以2160 KB的大小保存,这意味着我在这个过程中丢失了一些数据,但我无法找到它 //Getting the file name cout<<"Enter Filename\n"; char filePath[100]; cin >> filePat

我试图用C/C++读取一个文件并将其存储在一个字节数组中,一旦数据被存储,我就试图再次将其写回。但是当我写回它时,我不能正确地保存它,我在某些地方丢失了一些字节

例如:如果我试图保存一个
2161 KB
的文件,那么新创建的文件将以
2160 KB
的大小保存,这意味着我在这个过程中丢失了一些数据,但我无法找到它

//Getting the file name
cout<<"Enter Filename\n";
char filePath[100];
cin >> filePath;
FILE *file = NULL;
if ((file = fopen(filePath, "rb")) == NULL)
    cout << "File Failed To Load\n";
else
    cout << "File Loaded Successfully\n";

//Getting the size
long prev=ftell(file);
fseek(file, 0L, SEEK_END);
long fileSize =ftell(file);
fseek(file,prev,SEEK_SET);

//Creating a buffer and saving it back
BYTE *fileBuf = new BYTE[fileSize];
cout << "fileSize" << fileSize;
fread(fileBuf, fileSize, 1, file);
FILE* file2 = fopen( "test_output.dat", "wb" );
fwrite( fileBuf, 1, fileSize, file2 );
cout<<"\nsaved";

当我阅读它时,这是文件大小
2212314
,当我再次阅读它时,写入后,我得到
2211840

请确保您正在检查实际大小。Windows有不同的显示方式

比较以字节为单位的“大小”(例如16666),而不是“磁盘上的大小”,后者将根据文件系统向上取整


更新 您的drop box链接具有如下代码:-

FILE* output = fopen( "test_output.dat", "wb" );
fwrite( fileBuf, 1, fileSize, output );
std::cout<<"\nsaved";

FILE *file2 = NULL;
if ((file2 = fopen( "test_output.dat", "rb")) == NULL)
    std::cout << "Could not open specified file\n";
else
    std::cout << "File opened successfully\n";

long prev2=ftell(file2);
fseek(file2, 0L, SEEK_END);
long fileSize2 =ftell(file2);
fseek(file2,prev2,SEEK_SET);

std::cout << "fileSize" << fileSize2;
FILE*output=fopen(“test_output.dat”、“wb”);
fwrite(fileBuf,1,fileSize,output);

std::coutI不确定,但尝试以相同的方式使用
fread
fwrite
。在
fread
中读取
1
大小的块
fileSize
,在
fwrite
中写入
fileSize
大小的块
1
。这不是C。它使用
cout
@GirishNair,输出不可能来自该程序。请显示真实代码和真实输出。@GirishNair,当您重新阅读文件时,您没有忘记将“rb”与
fopen
一起使用,是吗?@GirishNair-另一件事:添加不相关或不正确的标记以“吸引注意”是自欺欺人的。吸引注意力的方法是用一个写得好的问题和一个SCCCE来显示问题。不,我读的文件和写的文件大小不同,这与磁盘大小或类似的东西无关。我不知道为什么不同:(
FILE* output = fopen( "test_output.dat", "wb" );
fwrite( fileBuf, 1, fileSize, output );
std::cout<<"\nsaved";

FILE *file2 = NULL;
if ((file2 = fopen( "test_output.dat", "rb")) == NULL)
    std::cout << "Could not open specified file\n";
else
    std::cout << "File opened successfully\n";

long prev2=ftell(file2);
fseek(file2, 0L, SEEK_END);
long fileSize2 =ftell(file2);
fseek(file2,prev2,SEEK_SET);

std::cout << "fileSize" << fileSize2;