Two consecutive fread errors and fread return values

Source: Internet
Author: User
Tags fread

 

I am writing a code today. I want to read a buffer frame into a file. Because there are two different implementations of neon and opencl, I need to read the file twice. The Code is as follows:

 

 1   FILE *file; 2   int i = 0; 3   INTER_BLOCK_SIZE_GPU_RS *block_size; 4   file = fopen(name, "rb"); 5   if (file == NULL) { 6     LOGE("Can‘t open file: %s\n", name); 7     return ; 8   } 9 //    LOGI("file open OK:%s",name);10     #if USE_OCL11      LOGI("read pool buffer");12          int readCount=0;13       for(i=0;i<50;++i){14        readCount=fread(rs_obj.buffer_pool_map_ptr, rs_obj.buffer_pool_size,1,  file);15          LOGI("OCL read buffer pool count==%d",readCount);16           if(readCount == 1)17                   break;18       }19 20     #if USE_NEON21       for(i=0;i<50;++i){22                readCount=fread(rs_obj.pool, rs_obj.buffer_pool_size,1,  file);23               LOGI("NEON read[%d] buffer pool count==%d",i,readCount);    24 25                 if(readCount == 1)26                   break;27               28          }29       LOGI("  ");30       #endif

It is found that the previous file was successfully read, And the next 50 times failed to read the file. I don't know why for a long time?

 

Later, we found that the pointer file * file to be read by C language files is compiled to read the file content. When reading a text file, the file is at the end of the file, C language output input pointers are indeed shared.

Therefore, when you use fread for the second time, 0 is always returned. You only need to add the code before the second execution of fread:

Fseek (file, 0, seek_set. Solve the problem.

 

In additionSize_tFread (Void*Buffer,Size_tSize,Size_tCount,File*Stream);

Returns the number of actually read count, which is the size. For example:

A file contains four char characters: 1, 2, 3, 4

Count = fread (rs_obj.buffer_pool_map_ptr, 1, sizeof (INT), file); 4,

Count = fread (rs_obj.buffer_pool_map_ptr,Sizeof (INT ),1, file); returns 1

Therefore, the actual read is a multiple of the size.

Two consecutive fread errors and fread return values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.