Solved

reading wrong

Posted on 2011-03-16
12
384 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
Comment Utility

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
Comment Utility
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
Comment Utility
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
 
LVL 86

Accepted Solution

by:
jkr earned 167 total points
Comment Utility
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
Comment Utility

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
Comment Utility
so it means i don't need to move anything ?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 166 total points
Comment Utility
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
Comment Utility
tks sara.
 U point me to a potential/hidden bug :) let me fix it.
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
so u mean i should check the send function in order to continue right?
0
 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
tks. for this stage will only check number of byte sent. I will take note on select() .
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now