Solved

reading wrong

Posted on 2011-03-16
12
388 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 45

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
Technology Partners: 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!

 
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:Kent Olsen
Kent Olsen 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 34

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: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 34

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

756 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