数学建模社区-数学中国

标题: 老大,簡單的文件操作程序,不知道錯在那裡!救命阿 [打印本页]

作者: kenneth    时间: 2004-6-9 11:41
标题: 老大,簡單的文件操作程序,不知道錯在那裡!救命阿
新建一個專案,在窗口Form1上加兩個按鈕,一個OpenDialog1,一個SaveDialog1;7 x( v3 V5 P; t& |8 U- _
//---------------------------------------------------------------------------, G. t4 |- K6 t* O+ X# j
$ p3 E" y( [( C- O4 K
#include <vcl.h>. ]3 d9 d+ R) T( g# h& L  e
#pragma hdrstop; @' Y7 Q" X) z- I( S+ E0 V
6 M4 n( f' q1 E) f
#include "Unit1.h"
/ S* E" u3 W6 W7 B  o#include <fstream.h>
8 U" O% u5 Z8 a, P( D& x7 N#include <dir.h>
- L& D1 p( M1 W#include <sys\stat.h>
  j, A- K, ^, _( ]#include <malloc.h>
  p( A: t, S9 d# o8 Q' \0 M//---------------------------------------------------------------------------: E; u3 s5 d  V
#pragma package(smart_init). Z( l9 O  `6 Q9 ^4 T9 G1 U
#pragma resource "*.dfm"
5 O! u' E+ ?( X1 _$ nTForm1 *Form1;
- N+ L. H6 S, @6 ^$ u  n4 D        unsigned char *BIN_Byte;  Z9 w5 @! i- l. i% W7 ?# W* _* Q, k
        long BIN_Byte_Len;. R" O- S0 ?3 Z6 v: j' b
//---------------------------------------------------------------------------
& e4 v9 c3 ?$ F5 i3 Y__fastcall TForm1::TForm1(TComponent* Owner)3 v* K: I8 l/ o# k1 y; Y
        : TForm(Owner)7 M0 u+ s& O9 h6 R) t; ^
{1 j+ _1 g# j( ^
}
9 x! e  |, f- R4 \* \' o) G//---------------------------------------------------------------------------+ x6 }, G' b$ A8 L% |
void __fastcall TForm1::Button1Click(TObject *Sender)" v* A* }" l1 s
{
  P/ }( D! h" r$ P        char sPath[MAXPATH];
% h+ c! f1 B. {3 C        struct stat statbuf;, _7 o$ Y0 S  k8 [6 I! n, Z
        FILE *stream;
9 l6 o0 c9 `$ W0 |+ `, y6 @+ y5 u4 `- K" Q
        getcwd(sPath,sizeof(sPath));
5 F: j& D& I8 k; L7 `3 O        OpenDialog1->InitialDir=sPath;
" P5 B6 ]- n+ h% H2 J- J8 c# d        OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";  ?+ Y' T2 i6 y5 [! j% D8 i
        OpenDialog1->FileName="fileFrom.BIN";# Y' X/ `0 }3 ?, i( S, B2 U
        if(OpenDialog1->Execute())( R- u4 o  L& E* ]5 _' R
        {  @; J& }0 L" R9 S% a7 |3 s
                strcpy(sPath,OpenDialog1->FileName.c_str());
: a4 A1 }" J9 b. K. m  C                stream=fopen(sPath,"rb");
1 h% z9 b1 B+ N1 [* ~4 F# ?                fstat(fileno(stream),&statbuf);& l0 A3 Z8 o+ T5 ?8 E  }
                BIN_Byte_Len=statbuf.st_size;$ s8 C( }& Z) l+ K1 ^2 e
                BIN_Byte=(unsigned char *)alloca(BIN_Byte_Len);, \7 S4 S$ \" Z& z8 i" `
                for(int i=0;i<BIN_Byte_Len;i++)
' d, H. v1 k9 v/ i% G# c5 ~- }/ J                        fscanf(stream,"%c",&BIN_Byte); //read file
% C/ \( |- y, v( u  W4 W                fclose(stream);
( F" D) w6 }+ B5 j) d        }: _0 z( f, p( X. R, r
        free(sPath);, C- ?( c; ~# Q& u$ p
        free(stream);+ l- S" d! m% `6 z( I
}
. k" q# K& t( F//---------------------------------------------------------------------------
" m2 r/ p7 o$ {void __fastcall TForm1::Button2Click(TObject *Sender)
- y) ?  J6 d: O7 n5 n  z7 Y{4 ?. l( \2 x4 V7 o2 c- ^, }: u& V
        char sPath[MAXPATH];3 f7 m* T; W& T
        FILE *stream;
4 F7 O* |1 E& R0 T8 D$ _        getcwd(sPath,sizeof(sPath));//App.Path' l0 k4 \/ i$ ^2 {* h2 a( l
        strcat(sPath,"\\");# P% `2 S& d! [* J3 u) ~6 g4 p
        SaveDialog1->InitialDir = sPath;/ S, H( Q) D! y% x$ q
        SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
" l: h- t' o8 J9 A9 C, w/ H        SaveDialog1->FileName="fileTo.BIN";" K1 l4 Y+ \2 e1 U5 ]. e5 v
        if(SaveDialog1->Execute())
* p- I$ T# r  P9 q        {& M, R) y: l0 W* n- x2 Z: Z/ `
                strcpy(sPath , SaveDialog1->FileName.c_str());
: |. ?5 b# T% [$ P6 O                stream=fopen(sPath,"wb");
% `4 a9 G* q' H( l& r* |                for(int i=0;i<BIN_Byte_Len;i++)% j7 ^* A3 S5 L3 X& X
                        fprintf(stream,"%c",&BIN_Byte);  //save file* }- J0 x/ I$ b2 C% f2 v
                fclose(stream);7 H' v- O) O1 p; v' X. J
        }
7 E  F! \! Q- \! r+ t        free(sPath);# j# _9 x: v, e6 z( ]- k+ i+ P5 D5 Q
        free(stream);
3 `3 s0 ~0 ]6 m$ e" V}! K! |2 r" c7 j: c( ^
//---------------------------------------------------------------------------# M1 N) Z. T! I! y
void __fastcall TForm1::FormDestroy(TObject *Sender)" Y7 U- z" A7 ?9 r6 s
{7 C9 B& r* i- P3 F! g  ~) n% v( B! y
        free(BIN_Byte);
4 Y/ u% I6 f; M- Y}
% Q8 L; t+ l) T9 K; J$ q//---------------------------------------------------------------------------
' K  A1 \: R8 M. b) u- Y' y( a' L' h2 k$ d, c) [
打開一個檔案,再另存為另一個新檔案$ v- P. y7 a; l8 a# _
結果兩個檔案內容一樣4 Q8 ~  ?& s! t. {5 H5 X3 D2 }
順序不一樣了
" s. P- U* p5 o( ], k$ t檔案前面一段內容跑到結尾去了
! _( P' I3 ^8 y9 f7 F就像下面的情況
0 @+ V1 L2 [; ^; y4 Z* a原來檔案內容:ABCDEFG
% O- p2 @- L* ^9 t# V1 j  ]/ }新檔案內容:CDEFGAB/ E  G1 N- ]6 R6 E+ o5 y4 b6 Z" ]
老大救救小弟哦& j/ \0 |8 N/ K- U7 p% o* y) T
[attach]208[/attach]
作者: kenneth    时间: 2004-6-9 13:05
<>怎麼一個人也沒有阿</P>[em03]
作者: kenneth    时间: 2004-6-9 17:41
<>這個是對2進制文件的操作</P><>我用的是一個字符一個字符的操作方法</P>[em04]
作者: kenneth    时间: 2004-6-9 19:13
<>問題解決了</P><>不用回了</P>[em01]
作者: high_hill    时间: 2004-6-15 15:14
<>不知道你找出的问题在那儿,我判断是alloca和malloc的误用。c++中对指针(内存)的操作总是容易出错,这些系统函数的使用,最好先仔细看看help。</P>
4 N) |2 O4 `% c3 i<>alloca:</P>
: s3 R! F/ @7 v0 X3 K. q8 f; i<>Allocates temporary stack space.</P>
. d: t( o6 s- g6 x' u% `<>alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>
9 V' P* R7 u0 `' f4 O/ R<>malloc:</P>+ ]6 Z7 g* P$ P  Y! c( X4 z* f
<>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>
9 e1 R6 k# y4 Q0 m7 Q6 `3 {<>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>
$ i3 ~6 R( v( W, Y! B4 }6 q5 f7 T; `
( r8 ]9 c' G( h. r5 ?/ G<>以下是改后的代码</P>; ]7 t" w  \& z) d  g2 d/ y  }, O# L
<>//---------------------------------------------------------------------------</P>$ D) g3 D/ S4 ]$ y; S5 K
<>#include &lt;vcl.h&gt;
( x! C9 o' a5 |5 `#pragma hdrstop</P>
' k$ r  y; l( }) c2 {7 S. D5 ]<>#include "Unit1.h"
9 T' [) j( {$ i/ G#include &lt;fstream.h&gt;) t# _1 S$ p5 F; ~+ o
#include &lt;dir.h&gt;
' ?. v& J2 O1 K: c$ M. ^#include &lt;sys\stat.h&gt;9 F2 s  ^" X0 a7 H
#include &lt;malloc.h&gt;; Q5 G: Q. O7 Z3 u8 H
//---------------------------------------------------------------------------
6 Z$ F' Q' L$ Y$ m2 O" u. [#pragma package(smart_init)) ^6 i: `0 ^; b* U; f/ V
#pragma resource "*.dfm"
; V& m) ~7 C' y) R5 k2 g, zTForm1 *Form1;
  }, S0 b! D5 A+ [- `( w1 l* G6 b( t        unsigned char *BIN_Byte;, B2 @; c! k& n" D# ~
        long BIN_Byte_Len;
) |5 [+ g! U! `: _! ^! T) B' l//---------------------------------------------------------------------------
" H. ~. {" C2 i! p% t__fastcall TForm1::TForm1(TComponent* Owner)/ E3 W5 w, ]/ x7 j  `( q3 o7 J" P6 l* _
        : TForm(Owner)/ s; R$ a3 P& m6 c" o1 M
{: h& q' @4 P" `
}" U+ Q/ ?) |2 V9 u. {6 q( ?
//---------------------------------------------------------------------------
4 m+ y$ t( e2 x0 dvoid __fastcall TForm1::Button1Click(TObject *Sender)
. u- v9 J$ t8 g- n$ \. L{
$ L1 i) j! T" s# x! g+ \. t        char sPath[MAXPATH];
8 k6 {3 T* a/ X. m. Q  y0 K        struct stat statbuf;0 U1 y2 W) I0 v9 f; _. }
        FILE *stream;</P>
: w" ^+ J" I+ J<>        getcwd(sPath,sizeof(sPath));
0 F" M' Z  H# r5 e- w        OpenDialog1-&gt;InitialDir=sPath;! R' C' p" e& x3 h" g. M6 S
        OpenDialog1-&gt;Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";! {8 y- ^) O8 |& Z( i/ Q% J
        OpenDialog1-&gt;FileName="fileFrom.BIN";# S$ u  J2 F2 _* [# t- O* _
        if(OpenDialog1-&gt;Execute()); d. \9 z$ @( P
        {
4 l! U' ?5 X! B0 o8 x                strcpy(sPath,OpenDialog1-&gt;FileName.c_str());
! Y8 t5 }0 e5 p& \" s( M                stream=fopen(sPath,"rb");
, p3 A" @0 h: u3 L( x. k, ^) R                fstat(fileno(stream),&amp;statbuf);3 X) T3 v! r/ ]6 N1 Y8 i
                BIN_Byte_Len=statbuf.st_size;% w1 a/ p) ^2 H0 F' d& u. _' v2 y3 {
                BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
( A1 r; n, k- ?7 `$ K                for(int i=0;i&lt;BIN_Byte_Len;i++)
, T7 T" a4 F! f3 d5 U% I0 [$ S2 \! [                        fscanf(stream,"%c",&amp;BIN_Byte); //read file6 }  m8 S# T' b
                fclose(stream);
4 G: {8 v% z2 k) b        }
3 Z) W" u5 Y0 b}3 z' C7 F, u9 o) C* V
//---------------------------------------------------------------------------6 I$ `% c  W: P; K4 I; K# _2 U+ t0 I
void __fastcall TForm1::Button2Click(TObject *Sender)( i+ o' a( P9 S
{0 s4 q+ }( N$ Y- L9 b$ Y7 z
        char sPath[MAXPATH];
+ Z2 `: u( U6 e6 i4 }, ~- P0 ^        FILE *stream;
0 v2 B. T' d! u        getcwd(sPath,sizeof(sPath));//App.Path5 P6 [( B4 U; k/ H1 A  r
        strcat(sPath,"\\");
7 U; s  F3 _3 @9 q: d0 b9 d        SaveDialog1-&gt;InitialDir = sPath;
7 y3 W: e1 |/ u5 V        SaveDialog1-&gt;Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";! j1 s& \7 z( S$ R; x& b
        SaveDialog1-&gt;FileName="fileTo.BIN";3 H; I: q% Y4 c" z4 Q' Y' {
        if(SaveDialog1-&gt;Execute())# v8 H& b; k9 D/ h
        {
3 R5 N6 y" j( T! [7 R6 W6 W                strcpy(sPath , SaveDialog1-&gt;FileName.c_str());# t* B" y& C; j5 X9 P& x8 V" M
                stream=fopen(sPath,"wb");
8 D% f) W; o# {5 y0 ~0 i                for(int i=0;i&lt;BIN_Byte_Len;i++)
8 f8 h+ W+ t+ i: o; ]                        fprintf(stream,"%c",BIN_Byte);  //save file
5 C$ r6 T1 q# R$ W2 E                fclose(stream);
& F8 l4 ~9 Y4 R( z7 I        }8 r6 ^! `+ X' F9 c
        free(BIN_Byte);
5 h% n9 R) m( @+ l# l}7 M1 v& c  S  Y& m( t8 B( m: s  C
//---------------------------------------------------------------------------
9 P  C, ]5 m; w0 ?void __fastcall TForm1::FormDestroy(TObject *Sender)
1 n' Q3 @( u  K8 @/ W( d{
  D$ m7 l- L+ h; V. Q        if (BIN_Byte!=NULL)
  h- d$ T6 q* z' G$ s2 q" w, W             free(BIN_Byte);
. ~6 a0 ]. l! l$ G}
# Q5 W) f& S; `! F$ i+ y//---------------------------------------------------------------------------
2 ?7 b( T6 E" c/ R; b</P>
, p; p: Z! P* h, h<>free(sPath); free(stream);此两句也有问题。</P>+ d5 Y: z' q9 J0 l/ e
<>char sPath[MAXPATH];不需要释放;</P>
8 d2 H2 P9 L1 u  x5 r<>fclose(stream)已经起到free(stream)的作用</P>6 Y3 [' l1 i- a% z( I+ {+ D" u
[此贴子已经被作者于2004-6-15 15:16:40编辑过]

作者: kenneth    时间: 2004-6-18 10:23
<>我已經改成塊處理的方法了</P><>謝謝你</P>[em01]




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5