<>不知道你找出的问题在那儿,我判断是alloca和malloc的误用。c++中对指针(内存)的操作总是容易出错,这些系统函数的使用,最好先仔细看看help。</P>) o5 y/ m+ }4 `! ]. K' t& f5 X
<>alloca:</P> & i% ]% M- ^2 j3 `6 i) j; A<>Allocates temporary stack space.</P>3 n5 D4 X1 P( H/ Z Q, p
<>alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>0 a, W" [6 N" _
<>malloc:</P> 8 N) E: O8 k" S" P, ~<>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> ( c' t F$ `/ F6 q% v2 ?<>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>; O% l' _' m: S Y$ Q
6 ]1 H* O! B4 J( ^5 W" Z, V7 q$ |<>以下是改后的代码</P> ) r' `% V/ }! u4 ^; V$ R) H<>//---------------------------------------------------------------------------</P>& i% [: Q" T$ G$ b% c. V' l- V7 k
<>#include <vcl.h>3 K& D, ^# Y+ X& I
#pragma hdrstop</P> / Z( I* J4 Y, M% v* h9 I) O( ?/ W<>#include "Unit1.h" $ U. u: F* _; b0 e* f#include <fstream.h> 9 @8 x7 P( P! m* |4 ^#include <dir.h> ) _, S8 v6 K: F* O; M' N2 \0 H$ k#include <sys\stat.h>& }1 }: y) H( t
#include <malloc.h> : G, I2 j1 ^% j( o- I//--------------------------------------------------------------------------- % e( j7 A$ w0 s, S% ^#pragma package(smart_init) 3 z1 Y& F! X" z$ O* j7 J. n7 G#pragma resource "*.dfm"7 D; U! I( O5 G) u0 @
TForm1 *Form1; * c# m! n3 s2 g2 N, @& N% { unsigned char *BIN_Byte; ' l$ l. l- K' M long BIN_Byte_Len; " H5 R, ~9 L6 ~: q4 A \ d//--------------------------------------------------------------------------- 9 T$ U& z( o! ]- q__fastcall TForm1::TForm1(TComponent* Owner). h3 l' s8 o( ]( H$ s, ?/ v
: TForm(Owner)4 x5 j/ ?) W% {
{0 q% g6 G; z0 @: s
} ) L+ ~: h6 W4 ?. P//---------------------------------------------------------------------------8 y- r( |7 f. I$ G3 o
void __fastcall TForm1::Button1Click(TObject *Sender)( R+ `, t9 {6 K
{% U* R) F3 L$ F( ?, A
char sPath[MAXPATH];8 ^/ k* _) t3 B, X7 Z; u3 ? v
struct stat statbuf;, C5 q/ j# I" k1 i
FILE *stream;</P>9 X, o" C2 Y$ n) A( ^( ]
<> getcwd(sPath,sizeof(sPath));& u; W) W I( O: ^
OpenDialog1->InitialDir=sPath; ( P5 P/ Z( H7 K4 |6 d OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*"; * n; B5 r+ _1 p7 H$ t OpenDialog1->FileName="fileFrom.BIN"; # [6 g2 r+ t) ^/ I. u( |7 K if(OpenDialog1->Execute())* D( I7 c E1 E" H3 R7 P
{9 x6 b- |& X: [3 X
strcpy(sPath,OpenDialog1->FileName.c_str());& E4 M7 I8 o6 Q! c) f
stream=fopen(sPath,"rb"); 3 u% J! o0 f9 z: n fstat(fileno(stream),&statbuf);& V4 n9 a+ e2 @
BIN_Byte_Len=statbuf.st_size; 8 \* C7 N. L2 I- N+ G- j BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len); - l2 P" T' b! \( L5 f" c for(int i=0;i<BIN_Byte_Len;i++)0 T( {3 D$ O: L8 ^8 @9 g
fscanf(stream,"%c",&BIN_Byte); //read file! K; }! |' |- [+ X! s
fclose(stream);* O% ~5 @- E4 ?: G" K) e
}! i& w6 ~6 r$ B7 M h
}7 q: k" y. _8 X `
//---------------------------------------------------------------------------* C. N' _8 }, ~; W4 J
void __fastcall TForm1::Button2Click(TObject *Sender)/ M" J6 k' q2 z
{6 A Q- k) m$ N M3 i
char sPath[MAXPATH]; f4 e; ^* p& L2 f0 ^
FILE *stream;& y6 c4 t* @ K) V
getcwd(sPath,sizeof(sPath));//App.Path 3 U3 P; h. K0 G6 W2 Y strcat(sPath,"\\"); + R" v; E# q' E5 w+ M# ] SaveDialog1->InitialDir = sPath; 1 T) ~ A+ A5 j! J; V# {- ]- t SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*"; ! a$ X, t- i2 R3 k. c/ M SaveDialog1->FileName="fileTo.BIN";: i+ [- U. b% B7 @: J
if(SaveDialog1->Execute()) & ?7 }# X: a3 Y( R4 c {) P$ ]9 Q& y$ V
strcpy(sPath , SaveDialog1->FileName.c_str()); - s5 p- S Z; x* _ }9 e stream=fopen(sPath,"wb"); ! j) J( i, q0 q+ q. l for(int i=0;i<BIN_Byte_Len;i++) / m* h% i9 R2 l! Y8 A5 I fprintf(stream,"%c",BIN_Byte); //save file 2 U1 e/ x$ Y9 t. q2 | fclose(stream); h" e4 r6 [% G! o& A1 n }: A$ V: |" L! ~3 n' M
free(BIN_Byte);. v+ E3 A) Q2 s. p* X8 o- V' \0 S/ O u
} : d* x. b% \+ \$ z/ {7 p//---------------------------------------------------------------------------/ i' L! |: L7 Y
void __fastcall TForm1::FormDestroy(TObject *Sender) % h. I: Q! B$ B4 n3 Z# ^+ \{ - e! h) [3 B2 T) v if (BIN_Byte!=NULL) 1 z. @" g9 t) S ?2 q0 W1 d) |4 g) c free(BIN_Byte); 0 ] U& t- J) l6 Q; S: A} % z; G: ?, X* c' I0 `//---------------------------------------------------------------------------, k% |# _* R6 z5 h1 {& I' p
</P>8 y2 v0 N0 J" B& c3 ]
<>free(sPath); free(stream);此两句也有问题。</P>3 `& }/ _$ N; x k3 N
<>char sPath[MAXPATH];不需要释放;</P>, ]$ B! Z% \' Q S, a
<>fclose(stream)已经起到free(stream)的作用</P> 3 f$ L5 a L; c4 {1 @0 f3 L