Link to home
Start Free TrialLog in
Avatar of BeginToLearn
BeginToLearn

asked on

reading wrong

hi all,
 My code works for the first time. When it repeat second time in the for loop, it have segmentation fault due to 0x002b96ac in fread () from /lib/libc.so.6 ( when i run debugger)
 Please explain what I did wrong. thanks a lot.

--------------------------
 // read in trunk and send trunk of each file until done fo each file and move on
       int result;
       if( size >= 2048)
       {
             for( int i= 0; i < count;i++)
             {
                  buffer= (char*)malloc(sizeof(char) *2048);
                  memset(buffer,'\0',2048);
                  result= fread (buffer,1,2048,fp);
                 
                  if (result != 2048)
                  {
                        printf(" Reading error in block 2048\n",stderr);
                        exit(1);
                  }
                  send(sock, buffer, 2048,0);
                  free(buffer);
                  fp= fp + 2048; //move fp to next block
                  printf("i is %d. next block \n",i);
             }
             
             //send the remainder trunk
             buffer= (char*)malloc(sizeof(char) *remainder);
           memset(buffer,'\0',remainder);
             result= fread (buffer,1,remainder,fp);
             if (result != remainder)
             {
                   printf(" Reading error in remainder ",stderr);
                   exit(1);
             }
             send(sock, buffer, remainder, 0);
             free(buffer);
       }
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


fp is the file (stream) pointer.  Adding 2048 to it will likely put your program "out in the ozone".  It will surely make the pointer be something other than what you want.


Don't increment that pointer.  :)



Kent
The problem is the line

fp= fp + 2048; //move fp to next block

You must NOT alter the value of a file descriptor returned by 'fopen()', sice it is not a 'file pointer' in terms of 'seeking' - that is what 'fseek()' (http://www.cplusplus.com/reference/clibrary/cstdio/fseek/) is for:


fseek(fp,2048,SEEK_CUR);

Open in new window

Avatar of BeginToLearn
BeginToLearn

ASKER

i get the concept now . So in implementation,
can i use like this:
    result= fread (buffer,1,2048,fp+ i*2048);

and remove fp = fp + 2048
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
so it means i don't need to move anything ?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
tks sara.
 U point me to a potential/hidden bug :) let me fix it.
Hi Begin,

It's actually a little more complex than you might think.  If you can't verify that the send has completed, you can't read the next block of data into the same buffer.  You might wind up putting data into the buffer before send has sent it all, which could result in garbage on the receiving end.


Kent
so u mean i should check the send function in order to continue right?
you can check whether send has completed by calling select. you would do the call before copying from read-buffer to write-buffer. the select waits until the socket is ready for writing.

Sara
tks. for this stage will only check number of byte sent. I will take note on select() .