免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1700 | 回复: 1
打印 上一主题 下一主题

write file [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-28 20:25 |只看该作者 |倒序浏览
发现了一个奇怪的问题,我的程序在下面没有问题,可以得到正确的,数据并且显示图象,但是同样的程序在下得到的文件大小是不一样的,很奇怪,好象文件指针发生了偏移从中的129和130可以看出,文件指针的偏移不是80000,大家遇到过同样的问题吗,这个问题把我弄郁闷了,偏偏老师需要的是版本.下面是程序和log


/************************************************
This program make difference between 2 images,
this difference will store in another SRF image.
*************************************************/

#define _FILE_OFFSET_BITS 64
#define _LARGEFILE64_SOURCE
#define _LARGE_FILES

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>

//#define MAX_BUFFER_SIZE 16384
//#define MAX_BUFFER_SIZE 16777216L //L
//#define MAX_BUFFER_SIZE 33554432L //L // I think its the best buffer size

// declare the file pointers for imageA, imageB and Image Difference
FILE *fileA, *fileB, *fileDiff;
FILE *Log;
FILE *fileDiff1;
// declare the file_length function
off64_t file_length(FILE *f);

int main(int argc, char *argv[])
{
        printf("+---------------------------------+\n");
        printf("|    Make Ref. Difference v1.3    |\n");
        printf("+---------------------------------+\n\n");

        printf("+---------------------------------+\n");
        printf("| Starting the difference process |\n");
        printf("+---------------------------------+\n");
        printf("==> Part 1 <==\n");
       
        // declare a register to do the loop
        register long int lngCount;
       
        long lngMaxBufferSize;
        long lngBufferSize;
        long lngDataTypeSize;
        long lngXImageA;
        long lngYImageA;
        long lngXImageB;
        long lngYImageB;
        long lngWidth;
        long lngHeight;
   
    int i=0;
   
   
// How to call?
// ./makeRefDiff 16777216 ./fileA.srf 10 10 ./fileB.srf 20 20 ./fileC_ref.srf 40 40

        printf("argc = %d\n", argc);

        // *********************************
        // Test if all arguments is present.
        if (argc != 11)
        {
                printf("[ERROR] Incorrect number of arguments.\n");
                printf("Enter the full file name for 3 srf Images:\n");
                printf("- BufferSizeInBytes;\n");
                printf("- Image A;\n");
                printf("- Coord. X of Point in Image A;\n");
                printf("- Coord. Y of Point in Image A;\n");
                printf("- Image B; \n");
                printf("- Coord. X of Point in Image B;\n");
                printf("- Coord. Y of Point in Image B;\n");
                printf("- Output Image = Image A - Image B.\n");
                printf("- Width; and\n");
                printf("- Height;\n");
               
                return 0;
        }
       
        sscanf(argv[1],  "%ld", &lngMaxBufferSize);
        sscanf(argv[3],  "%ld", &lngXImageA);
        sscanf(argv[4],  "%ld", &lngYImageA);
        sscanf(argv[6],  "%ld", &lngXImageB);
        sscanf(argv[7],  "%ld", &lngYImageB);
        sscanf(argv[9],  "%ld", &lngWidth);
        sscanf(argv[10], "%ld", &lngHeight);

        printf("lngMaxBufferSize = %ld\n", lngMaxBufferSize);

        printf("fileA       = %s\n", argv[2]);
        printf("fileB       = %s\n", argv[5]);
        printf("fileC       = %s\n", argv[8]);

        printf("lngXImageA       = %ld\n", lngXImageA);
        printf("lngYImageA       = %ld\n", lngYImageA);
        printf("lngXImageB       = %ld\n", lngXImageB);
        printf("lngYImageB       = %ld\n", lngYImageB);
        printf("lngWidth         = %ld\n", lngWidth);
        printf("lngHeight        = %ld\n", lngHeight);




        // open the first file
        if( (fileA  = fopen64( argv[2], "r" )) == NULL )
        {
                printf( "[ERROR] The file %s can not be opened.\n", argv[2] );
                return 0;
        }
        else
                printf( "The file %s was opened.\n", argv[2] );
       
        // open the second file
        if( (fileB  = fopen64( argv[5], "r" )) == NULL )
        {
                printf( "[ERROR] The file %s can not be opened.\n", argv[5] );
                return 0;
        }
        else
                printf( "The file %s was opened.\n", argv[5] );

        // test if output file already exists
        if( !((fileDiff  = fopen64( argv[8], "r" )) == NULL) )
        {
                printf( "[ERROR] The file %s already exists.\nRemove it before start this program.\n", argv[8] );
                return 0;
        }

        // open the output file
        if( (fileDiff  = fopen64( argv[8], "w" )) == NULL )
        {
                printf( "[ERROR] The file %s can not be opened.\n", argv[8] );
                return 0;
        }
        else
                printf( "The file %s was opened.\n", argv[8] );

        printf( "-> Starting the difference process <-\n" );
       
    if( (fileDiff1  = fopen64( argv[8], "w" )) == NULL )
        {
                printf( "[ERROR] The file %s can not be opened.\n", argv[8] );
                return 0;
        }
        else
                printf( "The file %s was opened.\n", argv[8] );

        printf( "-> Starting the difference process <-\n" );
       
        //open the log file
        if( (Log  = fopen64( "./test.txt" ,"w+" )) == NULL )
        {
                printf( "[ERROR] The file  can not be opened.\n" );
                return 0;
        }
        else
                printf( "The file  was opened.\n" );


        // open the output file
        // ********************
        // Test if both file have the same size
          long lngWidthA;  
          long lngWidthB;
        long lngHeightA;
          long lngHeightB;

        // set the position to read the file width and height
        fseeko64( fileA,   (off64_t)4LL, SEEK_SET );
        fseeko64( fileB,   (off64_t)4LL, SEEK_SET );
        fread(&lngWidthA,  (off64_t)4LL, (off64_t)1LL, fileA);
        fread(&lngHeightA, (off64_t)4LL, (off64_t)1LL, fileA);
        fread(&lngWidthB,  (off64_t)4LL, (off64_t)1LL, fileB);
        fread(&lngHeightB, (off64_t)4LL, (off64_t)1LL, fileB);
       
        printf("lngWidthA  = %d\nlngWidthB  = %d\n\n", lngWidthA, lngWidthB);
        printf("lngHeightA = %d\nlngHeightB = %d\n\n", lngWidthA, lngWidthB);
       
        // check if the file size is different
/*       
        if ( (lngWidthA != lngWidthB) || (lngHeightA != lngHeightB) )
        {
                printf("[ERROR] Different file size.");
                return 0;
        }
*/

        // Test if both file use the same type
        long lngTypeA;
          long lngTypeB;

        fseeko64(fileA, (off64_t)20LL, SEEK_SET );
        fseeko64(fileB, (off64_t)20LL, SEEK_SET );
        fread(&lngTypeA, (off64_t)4LL, (off64_t)1LL, fileA);
        fread(&lngTypeB, (off64_t)4LL, (off64_t)1LL, fileB);
        printf("lngTypeA = %d\nlngTypeB = %d\n", lngTypeA, lngTypeB);
       
        if (lngTypeA != lngTypeB)
        {
                printf("[ERROR] Different file type.");
                return 0;
        }

        // ******************************************
        // Copy the header from file A to output File
        fseeko64( fileA,    (off64_t) 0LL, SEEK_SET );
        fseeko64( fileB,    (off64_t)32LL, SEEK_SET );
        fseeko64( fileDiff, (off64_t) 0LL, SEEK_SET );
       
        long header[8];
        float *bufferA;
        float *bufferB;
        float *bufferC;
        float *origBufferA;
        float *origBufferB;
        float *origBufferC;
       
        lngBufferSize = lngMaxBufferSize / 4L ;
        lngDataTypeSize = 4L;
                                       

        // check if Width is correct
        if ( (lngXImageA + lngWidth) > lngWidthA )
                lngWidth = lngWidthA - lngXImageA;

        if ( (lngXImageB + lngWidth) > lngWidthB )
                lngWidth = lngWidthB - lngXImageB;

        // check if Height is correct
        if ( (lngYImageA + lngHeight) > lngHeightA )
                lngHeight = lngHeightA - lngYImageA;

        if ( (lngYImageB + lngHeight) > lngHeightB )
                lngHeight = lngHeightB - lngYImageB;

        fread (header, (off64_t)4LL, (off64_t)8LL, fileA);

        header[1] = lngWidth;
        header[2] = lngHeight;       
        header[4] = (long)(lngWidth * lngHeight);

        fwrite(header, (off64_t)4LL, (off64_t)8LL, fileDiff);

        // print a Header information
        printf("+--------+\n| HEADER |\n+--------+\n");
        printf("Magic = %d\nWidth = %d\nHeight = %d\nDepth = %d\nLength (long) =  %d\nORB Data Type = %d\nMap Type = %d\nMap Length = %d\n", header[0], header[1], header[2], header[3], header[4], header[5], header[6], header[7]);
       
       
               
        long lngLinesToRead;
        long lngCountOperation;

        lngLinesToRead = (lngBufferSize / lngWidth);


        printf("lngBufferSize  = %ld\n", lngBufferSize);
        printf("lngWidth       = %ld\n", lngWidth);

        printf("==> Part 2 <==\n");


//        lngLinesToRead = (long)1L;

//        printf("->lngLinesToRead = %ld", lngLinesToRead);


// @todo : remove it.
        lngLinesToRead = 1LL;
       
        lngMaxBufferSize = lngLinesToRead * lngWidth * 4LL;

        lngBufferSize = lngWidth * 4LL;

        printf("=> lngLinesToRead   = %ld\n", lngLinesToRead  );
        printf("=> lngMaxBufferSize = %ld\n", lngMaxBufferSize);

        // allocate the buffer for sucessives reads
        bufferA = (float *)malloc(lngBufferSize);
        bufferB = (float *)malloc(lngBufferSize);
        bufferC = (float *)malloc(100000000);
       
        // store the original address
        origBufferA = bufferA;
        origBufferB = bufferB;
        origBufferC = bufferC;

        // Copy the header from file A to output File
        fseeko64( fileA,    (off64_t)32LL, SEEK_SET );
        fseeko64( fileB,    (off64_t)32LL, SEEK_SET );
        fseeko64( fileDiff, (off64_t)32LL, SEEK_SET );

        // Put the file pointer in corner of rect to read
        fseeko64( fileA,    (off64_t)( (4LL) * (lngYImageA * lngWidthA + lngXImageA) ), SEEK_CUR );
        fseeko64( fileB,    (off64_t)( (4LL) * (lngYImageB * lngWidthB + lngXImageB) ), SEEK_CUR );

        // do iteraction in buffer
        for (lngCount = 0L; lngCount < lngHeight; lngCount++)
        {

                fread (bufferA, lngDataTypeSize, (off64_t)lngWidth, fileA);
                fread (bufferB, lngDataTypeSize, (off64_t)lngWidth, fileB);
               
               
      
        fprintf(Log,"++++++++++++++++++++++++++++++>fileDiff position is = %d \n",ftello64(fileDiff));
        
                for (lngCountOperation = 0L; lngCountOperation < lngWidth ; lngCountOperation ++)
                {
                        // do the difference
//            if ( (*(bufferA) == -9999.0) || (*(bufferB) == -9999.0) )
//               *(bufferC) = -9999.0;
//            else
               *(bufferC) = (*(bufferA) - *(bufferB));

/*               
                        printf("lngCountOperation = %ld\n", lngCountOperation);
                        printf("bufferA = %f\n", *(bufferA));
                        printf("bufferB = %f\n", *(bufferB));
                        printf("bufferC = %f\n", *(bufferC));

                        fprintf( Log,"lngCountOperation = %ld\n", lngCountOperation);
                        fprintf(Log,"bufferA = %f\n", *(bufferA));
                        fprintf(Log,"bufferB = %f\n", *(bufferB));
                        fprintf(Log,"bufferC = %f\n", *(bufferC));
*/
            
                        bufferA++;
                        bufferB++;
                        bufferC++;

      }


                bufferA = origBufferA;

                bufferB = origBufferB;

                bufferC = origBufferC;

         
        printf("+++++++++++++++++++++>file pointer = %ld ---------%d\n",ftell(fileDiff),i+1);
                if (fwrite(bufferC, lngDataTypeSize, (off64_t)lngWidth,      fileDiff) != lngWidth)
                {
                     printf("ërorr happen , please check!\n");
                      return ;   
         }                        
      
        
                i = i+1;
         
                printf("---->---->bufferC = %ld ---------%d\n",ftell(fileDiff),i);       
        fprintf(Log,"--------------->---->fileDiff position is = %d ------------------%d\n",ftello64(fileDiff),i);
        fseeko64( fileDiff, (off64_t)( (4LL) * (lngCount-1)  * lngWidth + 32LL ), SEEK_SET );
        
                // Put the file pointer in corner of rect to read
                fseeko64( fileA,    (off64_t)( (4LL) * ((lngCount + lngYImageA) * lngWidthA + (lngXImageA + 8LL) ) ), SEEK_SET );
                fseeko64( fileB,    (off64_t)( (4LL) * ((lngCount + lngYImageB) * lngWidthB + (lngXImageB + 8LL) ) ), SEEK_SET );

            }
           

        printf("\n\n");
        printf("+---------------------------------+\n");
        printf("|            Finished             |\n");
        printf("+---------------------------------+\n");


    free(bufferA);
    free(bufferB);
    free(bufferC);
        fclose(fileA);
        fclose(fileB);
        fclose(fileDiff);
        fclose(fileDiff1);
        fclose(Log);
    system("PAUSE");       
        return 1;
}


/*
* returns the length of a file (in bytes)
*/
off64_t file_length(FILE *f)
{
        off64_t pos;
        off64_t end;

        pos = ftell(f);
        fseeko64 (f, (off64_t)0LL, SEEK_END);
        end = ftello64 (f);
        fseeko64 (f, (off64_t)pos, SEEK_SET);

        return end;
}


f position is = 9760032 ------------------0
++++++++++++++++++++++++++++++>fileDiff position is = 9760032
--------------->---->fileDiff position is = 9840032 ------------------
++++++++++++++++++++++++++++++>fileDiff position is = 9840032
--------------->---->fileDiff position is = 9920032 ------------------
++++++++++++++++++++++++++++++>fileDiff position is = 9920032
--------------->---->fileDiff position is = 10000032 ------------------
++++++++++++++++++++++++++++++>fileDiff position is = 10000032
--------------->---->fileDiff position is = 10080032 ------------------
++++++++++++++++++++++++++++++>fileDiff position is = 10080032
--------------->---->fileDiff position is = 10160032 ------------------129
++++++++++++++++++++++++++++++>fileDiff position is = 10160032
--------------->---->fileDiff position is = 10240043 ------------------130
++++++++++++++++++++++++++++++>fileDiff position is = 10240032
--------------->---->fileDiff position is = 10320043 ------------------
++++++++++++++++++++++++++++++>fileDiff position is = 10320032
--------------->---->fileDiff position is = 10400042 ------------------
++++++++++++++++++++++++++++++>fileDiff position is = 10400032

论坛徽章:
0
2 [报告]
发表于 2008-02-28 20:28 |只看该作者
对不起大家了,问题没描述明白,上面的程序在LINUX下面运行正常,但WINDOWS下面得到的文件的大小是错误的,从LOG中可以看到在129和130之间,文件指针发生了偏移.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP