나는 바이너리로 파일을 읽을 수 있고 나중에이 바이너리를 사용하여 똑같은 파일을 만들 수있는 exe 프로그램을 만들려고합니다.fwrite가 전체 파일 (그냥 시작)을 복사하지 않는 것 같습니다
그래서 fopen(content,"rb")
을 사용하여 파일을 이진 파일 인 을 읽고 fwrite
을 사용하여 스트림에 쓸 수 있음을 알았습니다. 하지만 문제는 내가 fwrite
모든 것을 복사하지 않는 것입니다.
예를 들어 열린 텍스트에 31231232131
이 포함되어 있습니다. 다른 파일에 쓸 때 3123
(처음 4 바이트) 만 복사합니다.
나는 그것이 매우 간단하다는 것을 알 수있다. 나는 실종되었다. 그러나 나는 무엇을 모르고있다.
#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;
}
else 절을'{'와'}'로 묶습니다. 들여 쓰기는'C++ '에서 블록을 결정하지 않습니다. 파일을 열지 않으면 코드가 충돌합니다. –
C로 프로그래밍하지 않고 C++을 사용하고 있습니다. '<< "연산자를 사용하여 출력을하는 방법은 100 % C++입니다. 질문에 다시 태그를 추가했습니다. – unwind
@unwind CUT은 거의 없지만 C 질문입니다. – user3125367