kenneth 发表于 2004-6-9 11:41

老大,簡單的文件操作程序,不知道錯在那裡!救命阿

新建一個專案,在窗口Form1上加兩個按鈕,一個OpenDialog1,一個SaveDialog1;
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <fstream.h>
#include <dir.h>
#include <sys\stat.h>
#include <malloc.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
        unsigned char *BIN_Byte;
        long BIN_Byte_Len;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        char sPath;
        struct stat statbuf;
        FILE *stream;

        getcwd(sPath,sizeof(sPath));
        OpenDialog1->InitialDir=sPath;
        OpenDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
        OpenDialog1->FileName="fileFrom.BIN";
        if(OpenDialog1->Execute())
        {
                strcpy(sPath,OpenDialog1->FileName.c_str());
                stream=fopen(sPath,"rb");
                fstat(fileno(stream),&statbuf);
                BIN_Byte_Len=statbuf.st_size;
                BIN_Byte=(unsigned char *)alloca(BIN_Byte_Len);
                for(int i=0;i<BIN_Byte_Len;i++)
                        fscanf(stream,"%c",&BIN_Byte); //read file
                fclose(stream);
        }
        free(sPath);
        free(stream);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        char sPath;
        FILE *stream;
        getcwd(sPath,sizeof(sPath));//App.Path
        strcat(sPath,"\\");
        SaveDialog1->InitialDir = sPath;
        SaveDialog1->Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
        SaveDialog1->FileName="fileTo.BIN";
        if(SaveDialog1->Execute())
        {
                strcpy(sPath , SaveDialog1->FileName.c_str());
                stream=fopen(sPath,"wb");
                for(int i=0;i<BIN_Byte_Len;i++)
                        fprintf(stream,"%c",&BIN_Byte);  //save file
                fclose(stream);
        }
        free(sPath);
        free(stream);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
        free(BIN_Byte);
}
//---------------------------------------------------------------------------

打開一個檔案,再另存為另一個新檔案
結果兩個檔案內容一樣
順序不一樣了
檔案前面一段內容跑到結尾去了
就像下面的情況
原來檔案內容:ABCDEFG
新檔案內容:CDEFGAB
老大救救小弟哦

kenneth 发表于 2004-6-9 13:05

<P>怎麼一個人也沒有阿</P>

kenneth 发表于 2004-6-9 17:41

<P>這個是對2進制文件的操作</P><P>我用的是一個字符一個字符的操作方法</P>

kenneth 发表于 2004-6-9 19:13

<P>問題解決了</P><P>不用回了</P>

high_hill 发表于 2004-6-15 15:14

<P>不知道你找出的问题在那儿,我判断是alloca和malloc的误用。c++中对指针(内存)的操作总是容易出错,这些系统函数的使用,最好先仔细看看help。</P>
<P>alloca:</P>
<P>Allocates temporary stack space.</P>
<P>alloca allocates size bytes on the stack; the allocated space is automatically freed up when the calling function exits.</P>
<P>malloc:</P>
<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>
<P>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>

<P>以下是改后的代码</P>
<P>//---------------------------------------------------------------------------</P>
<P>#include &lt;vcl.h&gt;
#pragma hdrstop</P>
<P>#include "Unit1.h"
#include &lt;fstream.h&gt;
#include &lt;dir.h&gt;
#include &lt;sys\stat.h&gt;
#include &lt;malloc.h&gt;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
        unsigned char *BIN_Byte;
        long BIN_Byte_Len;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
        char sPath;
        struct stat statbuf;
        FILE *stream;</P>
<P>        getcwd(sPath,sizeof(sPath));
        OpenDialog1-&gt;InitialDir=sPath;
        OpenDialog1-&gt;Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
        OpenDialog1-&gt;FileName="fileFrom.BIN";
        if(OpenDialog1-&gt;Execute())
        {
                strcpy(sPath,OpenDialog1-&gt;FileName.c_str());
                stream=fopen(sPath,"rb");
                fstat(fileno(stream),&amp;statbuf);
                BIN_Byte_Len=statbuf.st_size;
                BIN_Byte=(unsigned char *)malloc(BIN_Byte_Len);
                for(int i=0;i&lt;BIN_Byte_Len;i++)
                        fscanf(stream,"%c",&amp;BIN_Byte); //read file
                fclose(stream);
        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        char sPath;
        FILE *stream;
        getcwd(sPath,sizeof(sPath));//App.Path
        strcat(sPath,"\\");
        SaveDialog1-&gt;InitialDir = sPath;
        SaveDialog1-&gt;Filter = "BIN_File(*.BIN)|*.BIN|All_File(*.*)|*.*";
        SaveDialog1-&gt;FileName="fileTo.BIN";
        if(SaveDialog1-&gt;Execute())
        {
                strcpy(sPath , SaveDialog1-&gt;FileName.c_str());
                stream=fopen(sPath,"wb");
                for(int i=0;i&lt;BIN_Byte_Len;i++)
                        fprintf(stream,"%c",BIN_Byte);  //save file
                fclose(stream);
        }
        free(BIN_Byte);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
        if (BIN_Byte!=NULL)
             free(BIN_Byte);
}
//---------------------------------------------------------------------------
</P>
<P>free(sPath); free(stream);此两句也有问题。</P>
<P>char sPath;不需要释放;</P>
<P>fclose(stream)已经起到free(stream)的作用</P>
[此贴子已经被作者于2004-6-15 15:16:40编辑过]

kenneth 发表于 2004-6-18 10:23

<P>我已經改成塊處理的方法了</P><P>謝謝你</P>
页: [1]
查看完整版本: 老大,簡單的文件操作程序,不知道錯在那裡!救命阿