-2
 typedef struct tagFileheader    
{
  unsigned short   Type;                  //  00h  File Type Identifier to check if the file is bmp or not
  unsigned int     FileSize;              //  02h  Size of bmp file No.of bytes of the bitmap file           
  unsigned int     PxOffset;              //  0Ah  Offset to bitmap pixel data  
}Fileheader;

 Fileheader filehdr;
 Fileheader *pFileheader = &filehdr;

 unsigned short Get16U(unsigned int *x)
 {
     unsigned short temp;
     temp = *x & (0xFFFF);
     return temp;
 }  

 unsigned int Get32U(unsigned int *x)
 {
    unsigned int temp;
    temp = *x ;
    return temp; 
 }

 int Get32(unsigned int *x)
 {
    int temp;
    temp = *x ;
    return temp; 
 }

void main()
{
    unsigned int headersize,i ;  

    bAddress = fopen("D:/Tapan/Projects/Jacquard/BMP/03Body.bmp","rb");  // open the file and send the start address of its memory location                               

    pFileheader->Type = Get16U(bAddress);                 // save the first two bytes of bmp file data 
    (char*)bAddress++;
    (char*)bAddress++;                                    // increment the address by 2 bytes to reach address of "Filesize" parameter 

    if(pFileheader->Type == bmpSIGNATURE)                 // read further bytes of the FILE header and INFO header only if the file is a bitmap 
     { 

        pFileheader->FileSize   = Get32U(bAddress);         // save the filesize
        bAddress = bAddress + 2;                            // increment the address by 8 bytes to reach address of "offset" parameter

        pFileheader->PxOffset    = Get32U(bAddress);        // save the offset
        bAddress++;                                         // increment the address by 4 bytes to reach address of "info header size" parameter
     }
}

Here is my code. My main intention in this code is to read the bmp file header.I want to declare a pointer which will hold the start address of the buffer which will be created by fopen().And using that start address i will read the data from buffer. I am using this code in ARM controller. This is not the full code.

Before main i have declared bAddress globally as -

unsigned int *bAddress

When i compile the code i get following error -- a value of type "int" cannot be assigned to an entity of type "unsigned int*' Now mY question is that,is my pointer declaration correct? I have seen FILE being used to declare a pointer variable for fopen() How should i use FILE here.How should i declare the structure of FILE

Community
  • 1
  • 1

2 Answers2

3

Using

(char*)bAddress++;
(char*)bAddress++;  

will not move the FILE* by two characters. In fact, it will render bAddress unusable. What you need is:

fgetc(bAddress);
fgetc(bAddress);

However, I have a feeling changing those two lines won't solve your problems. You obviously don't understand how to use a FILE* to perform I/O. You need to spend some time reading up on how file I/O works using FILE* before using it in a real program.

You can start with a tutorial at http://www.tutorialspoint.com/cprogramming/c_file_io.htm.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    not to mention that fopen(..) will not compile cause it will return a `FILE*` and not a char pointer – odedsh Feb 07 '15 at 08:40
  • what is the structure of FILE – tapan chawda Feb 07 '15 at 08:43
  • 1
    @tapanchawda, the structure of `FILE` depends on the implementation. The language does not specify it. It only specifies how to use `FILE*`. – R Sahu Feb 07 '15 at 08:45
  • So in my case how should i declare the FILE structure. – tapan chawda Feb 07 '15 at 08:45
  • `FILE* inFile = fopen("..", "r");` Specify the file name instead of `".."`. – R Sahu Feb 07 '15 at 08:46
  • i just want the start address of the bmp file that i will open using fopen function – tapan chawda Feb 07 '15 at 08:46
  • 2
    @tapanchawda, that comment is another indication that you need to get more knowledgeable about file I/O using C. – R Sahu Feb 07 '15 at 08:48
  • i have added more code which shows how i will get the value from the pointed address and how i will increment the pointer – tapan chawda Feb 07 '15 at 11:41
  • @tapanchawda More code doesn't make it any less wrong - To reiterate: `fopen()` does not "create a buffer". A `FILE *` is just a pointer to a some file descriptor structure private to the C library - there is no "value to get from it", and incrementing it is nonsensical. There is no "start address of the file" - you have to allocate your own buffer and use `fread()` or similar to get the data into memory where you can access it. There are countless tutorials and documentation out there to help you learn how file I/O in C works. – Notlikethat Feb 08 '15 at 12:53
0

I think it is not good idea to read bmp file in text mode. Use binary mode "rb" (not "r") and fread () function, and do not change pointer to file structure, as well as data in FILE's fields directly.

VolAnd
  • 6,367
  • 3
  • 25
  • 43