2021.12
vs2017对话框MFC。
long myoutputSTREAM(long j, long lsize, char* buffer, int mode_return, CString filename)
{
long l_start;
l_start = j;
while(1)
{
if (j >= lsize) break;
if (mode_return == 1 && buffer[j + 2] == 'e' && buffer[j + 3] == 'n' && buffer[j + 4] == 'd' && buffer[j + 5] == 's' && buffer[j + 6] == 't' &&buffer[j + 7] == 'r' &&buffer[j + 8] == 'e' &&buffer[j + 9] == 'a' &&buffer[j + 10] == 'm') //找到结尾了
{ //回车2字节 + 9个字符 + 回车2字节
j += 13;
goto myoutputSTREAM_out;
}
if ( ( mode_return ==0 || mode_return == 2 ) && buffer[j + 1] == 'e' && buffer[j + 2] == 'n' && buffer[j + 3] == 'd' && buffer[j + 4] == 's' && buffer[j + 5] == 't' &&buffer[j + 6] == 'r' &&buffer[j + 7] == 'e' &&buffer[j + 8] == 'a' &&buffer[j + 9] == 'm') //找到结尾了
{ //回车1字节 + 9个字符 + 回车1字节
j += 11;
goto myoutputSTREAM_out;
}
j++;
}
return j;
myoutputSTREAM_out:
FILE * STREAMfile;
STREAMfile = fopen(filename, "wb");
if (NULL == STREAMfile)
{
printf("ERROR output File open failed\n");
return j;
}
if (mode_return == 1) fwrite(buffer+ l_start, 1, j-13 - l_start, STREAMfile); //输出时不含回车
else fwrite(buffer + l_start, 1, j-11 - l_start, STREAMfile); //输出时不含回车
fclose(STREAMfile);
return j;
}
然后,在按钮程序中,加入:
// TODO: 在此添加控件通知处理程序代码
int mode_return = 0; //回车模式
long lsize = 0; //PDF文件大小
char * buffer; //存PDF文件
int num_output=0; //output文件的编号
FILE * PDFfile;
CString cstr, cstrin, cstrout;
GetDlgItem(IDC_EDIT1)->GetWindowText(cstrin);
PDFfile = fopen(cstrin, "rb");
if (NULL == PDFfile)
{
printf("ERROR File open failed\n");
return;
}
//获取文件大小
fseek(PDFfile, 0, SEEK_END);
lsize = ftell(PDFfile);
rewind(PDFfile);
if (lsize > 2000000000)
{
printf("ERROR File too large\n");
fclose(PDFfile);
return;
}
buffer = (char*)malloc(lsize+100); //多申请一些保险一点
if (NULL == buffer)
{
printf("ERROR Memory malloc failed\n");
fclose(PDFfile);
return;
}
fread(buffer, sizeof(char), lsize, PDFfile);
fclose(PDFfile);
//printf("%s\n",argv[i]);
printf(" %ld ", lsize);
long j;
j = 0;
if (buffer[8] == (char)0x0A ) //回车判断
{
mode_return = 0;
j += 9;
}
else if (buffer[8] == (char)0x0D )
{
if (buffer[9] == (char)0x0A) { mode_return = 1; j += 10; }
else { mode_return = 2; j += 9; }
}
else
{
printf("ERROR return detect\n");
return;
}
while (1)
{
if (j >= lsize)
break;
if (buffer[j - 1] != 'd' && buffer[j] == 's' && buffer[j + 1] == 't' &&buffer[j + 2] == 'r' &&buffer[j + 3] == 'e' &&buffer[j + 4] == 'a' &&buffer[j + 5] == 'm') //找到了
{
if (mode_return == 1) j += 8;
else j += 7;
cstrout = cstrin;
num_output++; //output文件的编号
cstr.Format("-%d", num_output);
cstrout += cstr;
j = myoutputSTREAM(j, lsize, buffer, mode_return, cstrout);
continue;
}
j++;
}