Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

reading wrong

Posted on 2011-03-16
12
Medium Priority
?
395 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 46

Expert Comment

by:Kent Olsen
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Accepted Solution

by:
jkr earned 668 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 668 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 35

Assisted Solution

by:sarabande
sarabande earned 664 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 46

Expert Comment

by:Kent Olsen
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 35

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

886 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