<>不知道你找出的问题在那儿,我判断是alloca和malloc的误用。c++中对指针(内存)的操作总是容易出错,这些系统函数的使用,最好先仔细看看help。</P> " A! f1 ?7 Y5 {" L- G$ Q0 T' q<>alloca:</P> 9 Y7 V$ S& \ i1 N1 |3 z9 Q<>Allocates temporary stack space.</P>2 \1 R% R, ^- x6 {* b0 _0 J* f
<>alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P> 5 K2 }# ~4 d+ d1 x<>malloc:</P> % i* k, W$ A% ~. r) t6 q<>malloc allocates a block of size bytes from the memory heap. It allows a program to allocate memory explicitly as it抯 needed, and in the exact amounts needed.</P> k7 L7 u) k }6 k" O' ^
<>Allocates main memory.The heap is used for dynamic allocation of variable-sized blocks of memory. Many data structures, for example, trees and lists, naturally employ heap memory allocation.</P>: M- ^1 u4 t4 d3 B+ V& G3 e
! N8 f/ z2 U. V% j<>以下是改后的代码</P> # s% \- \5 t, e) z' {% e# ~<>//---------------------------------------------------------------------------</P> u$ g0 y* }% B/ ?: z. h
<>#include <vcl.h>3 V, V( m% ?0 A, J
#pragma hdrstop</P> : o8 M& G8 m/ \8 M; u; b: L6 e<>#include "Unit1.h"0 u3 c9 F! a/ K( ]- s" P! o
#include <fstream.h>+ N# c. F, ~: m$ l0 O( M
#include <dir.h> 5 B- a: ?# `% _% O2 v#include <sys\stat.h>) w4 i& B- f+ m
#include <malloc.h>9 ]$ h: \9 |7 p& g# |: t
//---------------------------------------------------------------------------- ]: `3 \8 b- N3 I) K! m9 D# b' h" b3 E
#pragma package(smart_init)& W B$ r( i1 {8 w6 [
#pragma resource "*.dfm"' ?& ]$ f; `* K0 D1 [
TForm1 *Form1; ) r, `1 V# H4 y/ [/ O, G. G; M unsigned char *BIN_Byte;6 F; `) h: p) |' X. u& j
long BIN_Byte_Len; ; W4 A5 ?% k8 h//---------------------------------------------------------------------------, P q1 a2 m* `
__fastcall TForm1::TForm1(TComponent* Owner): D/ c$ {! L2 X6 e8 y* Z2 P2 X
: TForm(Owner); ?7 s3 u5 L& P# j/ N
{/ l- J' D* ~) n; b
}6 W9 A) n* r! W
//---------------------------------------------------------------------------# u3 j+ |2 O/ A0 F/ Y$ _4 C' B( r/ k# v2 V
void __fastcall TForm1::Button1Click(TObject *Sender), _) p$ |1 ?. s G
{ 2 J8 X. U( m; V6 k# _2 S char sPath[MAXPATH]; ! q3 C5 s' V* T z struct stat statbuf;6 Z0 Z. k$ R3 [+ o# I* Z
FILE *stream;</P> Z% ~8 Z8 J9 d5 b; m& K% `
<> getcwd(sPath,sizeof(sPath));0 K3 A6 ^5 x+ j# n
OpenDialog1->InitialDir=sPath; . s3 L1 ?9 O% l) I- x. Y' x OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";1 _* d- [3 G% H* ?2 m3 G
OpenDialog1->FileName="fileFrom.BIN";) P5 _& U; Z) S' t/ p# {
if(OpenDialog1->Execute()) + m; L1 F T5 \9 g- ~+ E8 c& w { 0 X" g9 O! x: \! Z% i: B4 q$ E! [ strcpy(sPath,OpenDialog1->FileName.c_str());7 s) c) a7 Y# ?# J" Y" C' U
stream=fopen(sPath,"rb"); : | u' P; |7 W3 G K/ b; j fstat(fileno(stream),&statbuf);# Z+ e; y. G: I U) i2 M& O: D7 I
BIN_Byte_Len=statbuf.st_size; ; |8 D3 Z* L8 w& t# X BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len); 6 N. N3 n% `( r& L- T5 N0 g for(int i=0;i<BIN_Byte_Len;i++) ' ]- b$ g7 |6 Y& D9 { fscanf(stream,"%c",&BIN_Byte); //read file % R8 ?5 g F6 L5 G6 t fclose(stream); " J: k# l r7 c! M: Y }% t" d/ A" D1 ~+ x) y) r8 O
} 5 i& h! u$ F4 \/ A9 B. K//---------------------------------------------------------------------------8 I0 B/ v8 z% @7 b4 P4 o+ \ T' b' r. J
void __fastcall TForm1::Button2Click(TObject *Sender) ! I1 C: Z; G$ X9 j{ # A* z; o% s# r9 \$ E char sPath[MAXPATH]; $ {$ L, A- {' ?& |7 K FILE *stream; ) `1 c" U% ~1 T& l, n; W$ P& ~ getcwd(sPath,sizeof(sPath));//App.Path0 T( F$ d7 w2 H0 K7 [( t/ c+ h
strcat(sPath,"\\");6 F: S4 |6 G0 F4 L* b6 ?4 U
SaveDialog1->InitialDir = sPath; Q V9 O; |( \$ T. S
SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*"; / t/ {) k; q; o5 f# `. a SaveDialog1->FileName="fileTo.BIN";% X- I. p- X. o& Q3 a& v' T3 t( n$ u
if(SaveDialog1->Execute())$ ~, g) t4 \/ P& o) Z
{$ S& s8 A/ l. y* {1 z
strcpy(sPath , SaveDialog1->FileName.c_str());% Y# N" e( S) N* V; Q* s
stream=fopen(sPath,"wb");. ^( |) m9 O9 s% g8 G6 c' b1 Y( T9 M
for(int i=0;i<BIN_Byte_Len;i++)- m: g* ?8 {, k; e" \8 M
fprintf(stream,"%c",BIN_Byte); //save file # K; V0 I3 s% ]; M' L( { fclose(stream);" K5 i. B% q- H
} + a6 T1 y6 O' E+ k1 n4 m7 h; ]1 e free(BIN_Byte);3 g1 c+ j7 ]$ o1 ]
} 3 F- E$ V4 k( e% s; H) H//--------------------------------------------------------------------------- 5 [, R, R" X! Z. R! _void __fastcall TForm1::FormDestroy(TObject *Sender)- T9 e% g3 r3 |, p: A# L
{4 q: @: O4 r$ z, o4 r, L
if (BIN_Byte!=NULL) 3 h- ]( b' R& e( O6 I- g* M9 U/ o* S1 r free(BIN_Byte);% o; g3 m+ n2 E: d
} * A1 P) f$ B1 i3 z//---------------------------------------------------------------------------0 V" `4 n! S% P# W3 [
</P># |. `! x0 c( Q$ T8 T* t2 l5 }
<>free(sPath); free(stream);此两句也有问题。</P>* i$ F8 @* b- P0 y+ i! C5 Q: Q* ?$ ~
<>char sPath[MAXPATH];不需要释放;</P> 7 _# Q; n" w( ?) c7 c<>fclose(stream)已经起到free(stream)的作用</P>- ]( G9 T/ M+ A" I. w