Solved

reading wrong

Posted on 2011-03-16
12
386 Views
Last Modified: 2012-05-11
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);
       }
0
Comment
Question by:BeginToLearn
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 35150577

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
0
 
LVL 86

Expert Comment

by:jkr
ID: 35150605
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

0
 

Author Comment

by:BeginToLearn
ID: 35150610
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
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 86

Accepted Solution

by:
jkr earned 167 total points
ID: 35150612
As a side note, given your code, there's is absolutels no need to 'seek' at all - that has been done by 'freak()' implicitly.
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 167 total points
ID: 35150650

Nope.....

Every time that you read from a file, the "current" spot in the file becomes the next unread character in the file.  Just loop on the fread() function and you'll read the entire file in 2048 byte "chunks".

 fread (buffer, 1, 2048, fp);


Kent
0
 

Author Comment

by:BeginToLearn
ID: 35150671
so it means i don't need to move anything ?
0
 
LVL 33

Assisted Solution

by:sarabande
sarabande earned 166 total points
ID: 35150707
don't free the buffer after send. send runs asynchronously and so it will use invalid memory.

you better use a fixed array for read and write.

char buffer_in [2048];
char buffer_out [2048];

which you define above the loop and do a memcpy after fread.

did you check the value of remainder when crashing?

remainder can be zero if the filesize is a multiple of 2048.

Sara
0
 

Author Comment

by:BeginToLearn
ID: 35150759
tks sara.
 U point me to a potential/hidden bug :) let me fix it.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35150896
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
0
 

Author Comment

by:BeginToLearn
ID: 35151723
so u mean i should check the send function in order to continue right?
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35155448
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
0
 

Author Comment

by:BeginToLearn
ID: 35158084
tks. for this stage will only check number of byte sent. I will take note on select() .
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question