shasanka
shasanka

Reputation: 33

i am trying to read the values stored inside the buffer but instead its showing me the address?

i want to print the values in the location not the address.. when i run the program using breakpoint it does increases the values but doesn't print the values contained in the addresses..

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "conio.h"

    int main()
    {
       char ch;
       char buffer[100];
       char* p;
       p = buffer;
       FILE *fp;

       fp = fopen("D:\\Telenor_Short_01.vts","rb");//  binary mode

       fseek(fp,0,SEEK_END); //sets the file position of the stream to the given offset
       int size=ftell(fp); //returns the current file position of the given stream.
       printf("size of file is :%d\n",size);

       if( fp == NULL ) //error checking
       {
          perror("Error while opening the file.\n");
          exit(EXIT_FAILURE);
       }

       fread(p,1,100,fp);
       for (int i=0;i<100;i++)
       {

           printf("%04x\n",*p);
           p++;
       }
       fclose(fp);

       /*printf("The contents of %s file are :\n", file_name);*/

       /*int i;
       while( ( ch = fgetc(fp) ) != EOF )
       {
          printf("%02X ",ch);
          if( !(++i % 16) ) putc('\n', stdout);
       }
       fclose(fp);
       putc('\n', stdout);*/
       _getch();
       return 0;
    }

this is the output:

size of file is :185153907
5bf894
5bf895
5bf896
5bf897
5bf898
5bf899
5bf89a
5bf89b
5bf89c
5bf89d
5bf89e
5bf89f
5bf8a0
5bf8a1
5bf8a2
5bf8a3
5bf8a4
5bf8a5
5bf8a6
5bf8a7
5bf8a8
5bf8a9
5bf8aa
5bf8ab
5bf8ac
5bf8ad
5bf8ae
5bf8af
5bf8b0
5bf8b1
5bf8b2
5bf8b3
5bf8b4
5bf8b5
5bf8b6
5bf8b7
5bf8b8
5bf8b9
5bf8ba
5bf8bb
5bf8bc
5bf8bd
5bf8be
5bf8bf
5bf8c0
5bf8c1
5bf8c2
5bf8c3
5bf8c4
5bf8c5
5bf8c6
5bf8c7
5bf8c8
5bf8c9
5bf8ca
5bf8cb
5bf8cc
5bf8cd
5bf8ce
5bf8cf
5bf8d0
5bf8d1
5bf8d2
5bf8d3
5bf8d4
5bf8d5
5bf8d6
5bf8d7
5bf8d8
5bf8d9
5bf8da
5bf8db
5bf8dc
5bf8dd
5bf8de
5bf8df
5bf8e0
5bf8e1
5bf8e2
5bf8e3
5bf8e4
5bf8e5
5bf8e6
5bf8e7
5bf8e8
5bf8e9
5bf8ea
5bf8eb
5bf8ec
5bf8ed
5bf8ee
5bf8ef
5bf8f0
5bf8f1
5bf8f2
5bf8f3
5bf8f4
5bf8f5
5bf8f6
5bf8f7

but i want the values ..

Upvotes: 0

Views: 66

Answers (1)

fluter
fluter

Reputation: 13786

First, check fp for NULL immediately after fopen. Second seek to the beginning of the file before fread. Last, most importantly, check the return value of fread, because that's the number of elements that was read into the buffer. It may be smaller than the buffer.

   fp = fopen("D:\\Telenor_Short_01.vts","rb");//  binary mode
   if( fp == NULL ) //error checking
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

   fseek(fp,0,SEEK_END); //sets the file position of the stream to the given offset
   int size=ftell(fp); //returns the current file position of the given stream.
   printf("size of file is :%d\n",size);

   fseek(fp, 0, SEEK_SET);
   int nread = fread(p, 1, 100, fp);
   for (int i=0; i<nread; i++)
   {
       printf("%04x\n", *p);
       p++;
   }
   fclose(fp);

In addition, in C, you can access array elements either by subscription or by pointers arithmetic, the two are the same effect:

char arr[8];
arr[3] is same as *(arr + 3);
&arr[3] is same as arr + 3;

To read the big file by chunks:

fseek(fp, 0, SEEK_SET);
char buf[4096];
int nread;
int i;
while (1) {
    nread = fread(buf, 1, 4096, fp);
    for (i=0; i<nread; i++)
    {
        printf("%02x\n", buf[i]);
    }
    if (nread < 4096)
        break;
}
fclose(fp);

Upvotes: 2

Related Questions