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
Solved

reading wrong

Posted on 2011-03-16
12
387 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: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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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‚Ķ
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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