标题:C读写档案的问题
只看楼主
yz1025
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:6
帖 子:473
专家分:915
注 册:2012-10-26
结帖率:97.67%
 问题点数:0 回复次数:0 
C读写档案的问题
有一需求,要将传入的二维数组写入档案,但是档案时常不明原因被锁死,
此时必须写出第二个档案,若第二个档案也被锁,就写第三个档案依此类推。

但是每次传入资料时,若发现档案解锁了,就要将资料写入最后一个档案,
再读取最后一个档案全部资料,串到前一个档案后,再读取该档全部资料,
再回写直到第一个档案后。二维数组资料有先后的顺序性。

不知道该怎么改,感觉有好多例外状况要处理?
---------------------------------------------------------------------
AllFiles => 已在stdafx.h声明为全局变量

.cpp
程序代码:
#include "stdafx.h"
#include "Write1.h"

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
                     )
{
    return TRUE;
}

int _stdcall Write1(char *FPath ,char *FN, int Count, int *A)
{
    char FP[Max_Size]={0};
    char FileP[Max_Size]={0};
    char FileP1[Max_Size]={0};

    strcpy(FP, FPath);
    strcpy(FileP, FP);
    strcpy(FileP1, FP);
    strcat(FileP,FN);

    FILE *stream1;
    
    unsigned char *TitleTemp = new unsigned char[Count];
    memcpy(&TitleTemp[0],&A[0],Count);
    A=NULL;

    if (getDirFile(FP,".1")==0)                            //搜寻档案
    {
        stream1 = fopen( FileP , "ab+" );
        if( stream1 != NULL )
        {
            int nwDataSize =fwrite(&TitleTemp[0],sizeof (char), Count, stream1);
            fflush(stream1);                            //强制输出磁碟缓冲区数据
        }
        else
            return Write2(FileP,Count,&TitleTemp[0]);    //写出新档
        fclose(stream1);
    }
    else if (getDirFile(FP,".1")!=0)    //這部分怎改?
    {
        strcat(FileP1,FN);strcat(FileP1,".1");
        if (strlen(FileP1)<=255)
        {
            if (Write2(FileP1,Count,&TitleTemp[0])==1)        //写出新档
                if (ReWrite(FileP,FileP1,Count,&TitleTemp[0])==0)//回写合并档案
                    return -1;    
            else
                return Write2(FileP1,Count,&TitleTemp[0]);
        }
    }

    delete [] TitleTemp;                                //内存空间
    TitleTemp=NULL;                                        //释放内存地址

    return 1;                                            //成功写入原档
}


.h
程序代码:
#include "stdafx.h"

int Write2(char*,int,unsigned char*);
int getDirFile(const char*,const char*);
int ReWrite(char*,char*,int,char*);
long GetFileLength(char*);

//------------------------------------------------------------------------------------------------------------------//
long GetFileLength( char * fileName)//取得档案大小
{
    if (!fileName && !*fileName) 
        return 0;
    
    HANDLE h; 
    WIN32_FIND_DATA info; 
    
    if ((h=FindFirstFile(fileName,&info)) != INVALID_HANDLE_VALUE) 
    { 
        FindClose(h); 
        if ((info.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) == 0) // Is it a file? 
        { 
            union 
            { 
                struct { DWORD low, high; } lh; 
                __int64 size; // MS large int extension 
            } file; 
            
            file.lh.low = info.nFileSizeLow; 
            file.lh.high= info.nFileSizeHigh;
            
            return (long)file.size; // will be casted to double 
        } 
        // It's a directory, not a file 
    } 
    return 0; // No such name. 
 }
//------------------------------------------------------------------------------------------------------------------//
int Write2(char *FN, int Count, unsigned char *A)
{
    char FN1[Max_Size]={0};
    strcat(FN1,FN);
    strcat(FN1,".1");

    FILE *stream2 = fopen( FN1, "ab+" );
    if( stream2 != NULL )
    {
        int nwDataSize =fwrite(&A[0],sizeof (char), Count, stream2);
        fflush(stream2);                            //强制输出磁碟缓冲区数据
    }
    else
        return Write2(FN1,Count,A);                    //失败写入新档
    fclose(stream2);

    return 1;                                        //成功写入新档
}
//------------------------------------------------------------------------------------------------------------------//
int getDirFile(const char* definePath, const char* extenName)
{
    short int i=0,j=0;

    _chdir(definePath);

    unsigned long hFile=0;
    struct _finddata_t fileName;
    hFile = _findfirst(extenName, &fileName);

    do 
    {
        if (fileName.time_create >0)
        {
            j=sprintf(AllFiles[i], "%s\\%s", definePath, fileName.name);
            i++;
        }
    } 
    while(hFile != -1 && _findnext(hFile, &fileName)==0);

    return (i-1);
}
//------------------------------------------------------------------------------------------------------------------//
int ReWrite(char *FN, char *FN1, int Count, unsigned char *A)
{
    int nwDataSize=0,ret=0;

    size_t BUFFER_SIZE =(unsigned)GetFileLength(FN1);

    if (BUFFER_SIZE != 0)
    {
        unsigned char *TitleTemp1 = new unsigned char[BUFFER_SIZE+Count];

        FILE *stream3 = fopen( FN1, "rb" );
            nwDataSize =fread(&TitleTemp1[0],sizeof (char),BUFFER_SIZE, stream3);
        fclose(stream3);
        
        memcpy(&TitleTemp1[BUFFER_SIZE],&A[0],Count);

        char filename[Max_Size]={0};
        strcpy(filename,FN1);
        ret = remove(filename);                     //删除暂存档

        if(ret == 0) 
        {
            FILE *stream4 = fopen( FN, "ab+" );    //接着档案后面继续写
                nwDataSize =fwrite(&TitleTemp1[0],sizeof (char), BUFFER_SIZE+Count, stream4);
                fflush(stream4);                    //强制输出磁碟缓冲区数据
            fclose(stream4);
        }
        else
            return 0;
    }
    else
        return 0;

    return 1;
}
//------------------------------------------------------------------------------------------------------------------//


[此贴子已经被作者于2020-5-15 15:41编辑过]

搜索更多相关主题的帖子: char 档案 int Count return 
2020-05-15 15:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501621-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.301198 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved