<>不知道你找出的问题在那儿,我判断是alloca和malloc的误用。c++中对指针(内存)的操作总是容易出错,这些系统函数的使用,最好先仔细看看help。</P>9 o# b. A3 i; r$ {8 U' ]) ], R/ @
<>alloca:</P> ! `4 I9 Y2 c% ~7 @. `8 m/ d<>Allocates temporary stack space.</P>3 o& T* U( {9 y- ?0 \# I$ m
<>alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P> # |2 J [1 E% L<>malloc:</P> 7 v% l# k" b9 H& T ^: i<>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># y1 \+ \3 B! l6 y" I: p4 E
<>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>! r! c9 l" d7 m7 s
4 R' e& o- R. }/ P0 e; A<>以下是改后的代码</P> ! t0 A- @4 u9 s; Z<>//---------------------------------------------------------------------------</P>' S8 ~4 d9 D" j3 m4 k! I+ q) k
<>#include <vcl.h>) F8 R+ g3 Y& A3 e% `
#pragma hdrstop</P> 4 j) a4 R2 E" @" R0 s$ j<>#include "Unit1.h" 2 m; @" J- g& a; Z( n1 E1 a3 G#include <fstream.h> ' H$ |" a+ n& `* X# V; x#include <dir.h> : z; E2 P+ a2 }0 `#include <sys\stat.h>) ?$ _- M, ?0 E% N: H( |% i
#include <malloc.h> % T7 ]9 g) M3 L* u3 n( j) w//---------------------------------------------------------------------------5 E4 W+ \1 R3 t3 X n4 b
#pragma package(smart_init) 4 K1 b& W8 K6 Z* ~#pragma resource "*.dfm" 5 q2 h) q6 C4 E6 o$ dTForm1 *Form1; - `' Z8 i7 l' ^. r |$ i1 t* @ unsigned char *BIN_Byte;9 @! N+ l3 L$ Z6 a: f+ `
long BIN_Byte_Len; 1 f: W+ A! h! z; o ?$ P1 T//---------------------------------------------------------------------------( w) b" R5 m7 d7 x2 \
__fastcall TForm1::TForm1(TComponent* Owner) , D$ u6 m {2 r( A' j% G0 x- c : TForm(Owner)2 R7 W( | y: X1 f
{ W+ V! }. V+ X) q, S: U} ; F% `0 H: V: ]4 _$ x S; g//---------------------------------------------------------------------------" }' @* k0 L; _2 I
void __fastcall TForm1::Button1Click(TObject *Sender) # C; K4 G+ n( Q+ D* n# b& Z{ / [% G* }; C9 f% C+ B2 E" l char sPath[MAXPATH];( K0 Y. y" p) y3 R
struct stat statbuf; 7 P1 i; t/ [$ u# Y FILE *stream;</P> % `6 a Q* ~( Y2 c7 t- M<> getcwd(sPath,sizeof(sPath));, p2 g. V7 F- V! T/ {1 l
OpenDialog1->InitialDir=sPath;; a1 A" H1 z% E5 Z6 A" W
OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*"; + v- K6 [8 P* h# E* z OpenDialog1->FileName="fileFrom.BIN";% k1 G; M$ U) `( y
if(OpenDialog1->Execute())) {" L1 y7 V2 }/ f3 p6 H
{; O' M; A1 ]/ w" T8 J+ M
strcpy(sPath,OpenDialog1->FileName.c_str()); ' G) R6 H9 `- {# h7 g$ [& N' \% P$ ? stream=fopen(sPath,"rb");& q9 k" m# j) o& T6 R) J# b. O
fstat(fileno(stream),&statbuf);5 ?9 P- B5 }( c( o! ^. ]) x
BIN_Byte_Len=statbuf.st_size; " T. b1 y0 H; p0 W BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);1 a" @0 P7 K5 `+ W1 }8 m8 f
for(int i=0;i<BIN_Byte_Len;i++)$ e$ _% g8 T4 F0 T( Y) M1 J$ L0 H
fscanf(stream,"%c",&BIN_Byte); //read file3 D4 F' u$ ]! c
fclose(stream); # M; [; ?/ j( P% p) q; A; S } H4 H1 G' E3 k( U* Y2 k9 K/ a} # J& K4 d4 l+ M: N7 g* ^//--------------------------------------------------------------------------- * d* G4 x% c' ^* F Cvoid __fastcall TForm1::Button2Click(TObject *Sender)/ Y& h# A& f: `, t) Z; b
{ $ R {6 }# l! h char sPath[MAXPATH]; # |8 h# {9 x8 A5 F FILE *stream; 9 j8 Q; T. [7 k; Q3 A. u getcwd(sPath,sizeof(sPath));//App.Path . {) d3 R# O8 K4 E1 e strcat(sPath,"\\"); 5 F( T, k# M2 s; b- l: E SaveDialog1->InitialDir = sPath; . Y% E) V( r4 }( k, t7 ~# |- N/ y SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*"; " g7 ], E/ B: K6 b% @# ^! I SaveDialog1->FileName="fileTo.BIN";3 j6 s2 x" W! T: e; c
if(SaveDialog1->Execute())! Y) q9 y0 A0 D6 B
{ ( l5 y+ |! _; S" p$ G, F7 } strcpy(sPath , SaveDialog1->FileName.c_str()); 5 Z: V4 m9 u% V0 q+ b; s stream=fopen(sPath,"wb");9 Q8 m3 B2 F1 e, K; j# Y) N
for(int i=0;i<BIN_Byte_Len;i++)% o. B% u! g; z( g' n
fprintf(stream,"%c",BIN_Byte); //save file 9 Z' F0 J: h7 ]% _ fclose(stream);( O$ G, O1 x0 q) Q2 v, F
}' k0 [; b( R9 w1 {# v; s' ? `9 c
free(BIN_Byte); $ V \) B2 c& b( O}; {( q, J1 R- a" U$ {& R
//---------------------------------------------------------------------------2 O1 x, A# q. S# g S( J
void __fastcall TForm1::FormDestroy(TObject *Sender)0 h# G9 w* c m! T
{8 F- j1 ?% K B
if (BIN_Byte!=NULL) ( n" G- y& G/ X+ z/ D) P free(BIN_Byte);; f# l: Q" f, j- G" T+ ^
}" n( d: k* T5 g N7 ]
//---------------------------------------------------------------------------# }1 ?: @. T1 B) H' z
</P>) ?5 k8 p+ V+ V6 `( |( h8 v; v
<>free(sPath); free(stream);此两句也有问题。</P> / x7 j7 I4 ]+ b3 p% @<>char sPath[MAXPATH];不需要释放;</P> 9 f, k) @ F" ~, K* x<>fclose(stream)已经起到free(stream)的作用</P>% D1 P1 [6 i, w; F