Reading a AFP file to struct in c#

  afp, c++

I’m new to C# and I’m assigned a task where I have to write a program in C# to get AFP pagecount.
We have a program written in C++ previously but this has to be done in C#.

I tried using binary reader to read the files and validate with given condition to get pagecount,but it’s not working as expected.

this is the original code Snippet in C++,how do I write this in C#?

unsigned long  ProcessAFPFile(char *szInputPath,char *szFileName,char *szMess)
{
    char szInputFile[MAX_PATH];
    struct StructuredFieldIntroducer{
        char          CB;
        unsigned char M_Length;
        unsigned char L_Length;
        unsigned char M_Cmd;
        unsigned char L_Cmd;
        unsigned char Data[32758]; 
    }SFI;
    int RetVal = fread(&SFI,1,1,InputFile);
    wsprintf(szInputFile,"%s%s",szInputPath,szFileName);
    if ((InputFile = fopen(szInputFile,"rb")) == NULL){
        wsprintf(szMess,"Cannot open file");
        return -0L;
    }
    RetVal = fread(&SFI,1,1,InputFile);
    if(SFI.CB != 0x5A){
        fclose(InputFile);
//      wsprintf(szMess,"Invalid AFP file");
        return -0L;        
    }
     while(TRUE){
        RetVal = fread(&SFI,9,1,InputFile);
        if(feof(InputFile)){
            fclose(InputFile);
//          wsprintf(szMess,"");
            return BPageCnt;
        }
     ReadSize = SFI.M_Length << 8;
        ReadSize += SFI.L_Length;
        ReadSize++;
        if(!fread(&SFI,ReadSize,1,InputFile)){
            fclose(InputFile);
//          wsprintf(szMess,"Invalid file end");
            return BPageCnt;
        }
        switch(SFI.M_Cmd){
            case 0xA8:
                if(SFI.L_Cmd == 0xAF)
                    BPageCnt++;
        }
    }    
    return BPageCnt;
}

Input to the above code is a AFP File.

Source: Windows Questions C++

LEAVE A COMMENT