简体   繁体   中英

fwrite doesn't seem to copy the whole file (just the start)

I'm trying to make a exe program that can read any file to binary and later use this binary to make the exact same file.

So I figured out that I can use fopen(content,"rb") to read a file as binary, and using fwrite I can write block of data into stream. But the problem is when I fwrite it doesn't seems copy everything.

For example the text I opened contains 31231232131 in it. When I write it into another file it only copies 3123 (first 4 bytes).

I can see that it's a very simple thing that I'm missing but I don't know what.

#include <stdio.h>
#include <iostream>

using namespace std;
typedef unsigned char BYTE;
long getFileSize(FILE *file)
{
    long lCurPos, lEndPos;
    lCurPos = ftell(file);
    fseek(file, 0, 2);
    lEndPos = ftell(file);
    fseek(file, lCurPos, 0);
    return lEndPos;
}

int main()
{
    //const char *filePath = "C:\\Documents and Settings\\Digital10\\MyDocuments\\Downloads\\123123.txt";
    const char *filePath = "C:\\Program Files\\NPKI\\yessign\\User\\008104920100809181000405,OU=HNB,OU=personal4IB,O=yessign,C=kr\\SignCert.der";


    BYTE *fileBuf;          
    FILE *file = NULL;      
    if ((file = fopen(filePath, "rb")) == NULL)
        cout << "Could not open specified file" << endl;
    else
        cout << "File opened successfully" << endl;
        long fileSize = getFileSize(file);
        fileBuf = new BYTE[fileSize];
        fread(fileBuf, fileSize, 1, file);
        FILE* fi = fopen("C:\\Documents and Settings\\Digital10\\My Documents\\Downloads\\gcc.txt","wb");
    fwrite(fileBuf,sizeof(fileBuf),1,fi);


    cin.get();
    delete[]fileBuf;
    fclose(file);
    fclose(fi);
    return 0;
}
fwrite(fileBuf,fileSize,1,fi);

您确实读取了fileSize字节,但是正在写入sizeof(...)字节,即new返回的指针大小。

A C++ way to do it:

#include <fstream>

int main()
{
    std::ifstream in("Source.txt");
    std::ofstream out("Destination.txt");
    out << in.rdbuf();
}

You have swapped the arguments of fread and fwrite . Element size precedes the number of elements. Should be like so:

fread(fileBuf, 1, fileSize, file);

And

fwrite(fileBuf, 1, fileSize, fi);

Also address my comment from above:

Enclose the else clause in { and } . Indentation does not determine blocks in c++. Otherwise your code will crash if you fail to open the file.

EDIT: and the another problem - you have been writing sizeof(fileBuf) bytes which is constant. Instead you should write the exact same number of bytes as you've read. Having in mind the rest of your code you could simply replace sizeof(fileBuf) with fileSize as I've done above.

fileBuf = new BYTE[fileSize];
        fread(fileBuf, fileSize, 1, file);
        FILE* fi = fopen("C:\\Documents and Settings\\[...]\gcc.txt","wb");
    fwrite(fileBuf,sizeof(fileBuf),1,fi);

fileBuf is a pointer to BYTE . You declared it yourself, look: BYTE *fileBuf . And so sizeof(filebuf) is sizeof(BYTE *) .

Perhaps you wanted:

fwrite(fileBuf, fileSize, 1, fi);

which closely mirrors the earlier fread call.

I strongly recommend that you capture the return values of I/O functions and check them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM