Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2341679
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:05:45

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

#include
#pragma hdrstop

#include "Unit1.h"
#include
#include
#include
#include
//---------------------------------------------------------------------------
#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[MAXPATH];
        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                        fscanf(stream,"%c",&BIN_Byte[i]); //read file
                fclose(stream);
        }
        free(sPath);
        free(stream);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        char sPath[MAXPATH];
        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                        fprintf(stream,"%c",&BIN_Byte[i]);  //save file
                fclose(stream);
        }
        free(sPath);
        free(stream);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
        free(BIN_Byte);
}
//---------------------------------------------------------------------------

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

--------------------next---------------------

阅读(962) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~