User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 427,378 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,042 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 487 | Replies: 9 | Solved
Reply
Join Date: Jun 2008
Posts: 14
Reputation: Alex_ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Alex_ Alex_ is offline Offline
Newbie Poster

fread() doesn't work when 're-used' in a for( ; ;) function

  #1  
Jun 16th, 2008
Hey, i'm trying to sort direct in a file.
The problem is that when i read the first time an int from it,it works! doh.
but when it reads for the second time, it doesn't. ( Neither the swap works.
No signs of errors from the compiler.


  1. FILE *fp=fopen("element.txt","rw+");
  2. fseek(fp, SEEK_SET, 0);
  3.  
  4.  
  5. for(i=0;i<n;i++){
  6. fgetpos(fp, &filepos);
  7. fread(&t, sizeof(int), 1, fp);
  8. for(j=i+1;j<n-1;j++){
  9. fgetpos(fp, &filepos2);//t2=NULL;
  10. fread(&t2, sizeof(int), 1, fp); //<< 'broken', t2's value does not change
  11. if(t>t2){
  12. fsetpos(fp, &filepos); //
  13. fwrite(&t2,sizeof(t2),1,fp); // <<All of this doesn't work also.
  14. fsetpos(fp, &filepos2); //File remains the same.
  15. fwrite(&t,sizeof(t),1,fp);} //
  16. }
  17. }
  18. fclose(fp);
Last edited by Alex_ : Jun 16th, 2008 at 5:11 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,647
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #2  
Jun 16th, 2008
You could try checking return values such that the code itself tells you what the problem is.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,437
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 10
Solved Threads: 100
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #3  
Jun 16th, 2008
How's t and t2 declared?

FILE *fp=fopen("element.txt","rw+"); How about checking that the file was opened?
if ( fp == NULL )
{
     <error...>
}

Upon success fread() returns the amount of elements read in a form of size_t object; why don't you check that?

size_t result = 0;
result = fread(&t, sizeof(int), 1, fp);
if( result > 0 )
{
     <do something...>
}
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Jun 2008
Posts: 14
Reputation: Alex_ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Alex_ Alex_ is offline Offline
Newbie Poster

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #4  
Jun 16th, 2008
Originally Posted by Dave Sinkula View Post
You could try checking return values such that the code itself tells you what the problem is.

Done did that.
The function returns 0;which means no good. On succes it gives 1.
I have modified the code.Now on the third time the function does not work.(Works only twice).

  1. int i,j,t,t2;
  2. fpos_t filepos,filepos2;
  3. FILE *fp=fopen("element.txt","rw+");
  4. fseek(fp, SEEK_SET, 0);
  5.  
  6. do{filepos+=2*i;
  7. pos=ftell(fp);
  8. fread(&t, sizeof(int), 1, fp);
  9. do{j=i+1;
  10. filepos2+=2*j;
  11. fread(&t2, sizeof(int), 1, fp);//<<Broken when the compiler hits it for the 3rd time.
  12. pos=ftell(fp);
  13. if(t>t2){
  14. fsetpos(fp, &filepos);
  15. fwrite(&t2,sizeof(t2),1,fp);
  16. fsetpos(fp, &filepos2);
  17. fwrite(&t,sizeof(t),1,fp);t=t2;}
  18. j++;fclose(fp);FILE *fp=fopen("element.txt","rw+");}while(j<n-1);
  19. i++;}while(i<n);
Last edited by Alex_ : Jun 16th, 2008 at 6:25 pm.
Reply With Quote  
Join Date: Jun 2008
Posts: 14
Reputation: Alex_ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Alex_ Alex_ is offline Offline
Newbie Poster

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #5  
Jun 16th, 2008
Originally Posted by Aia View Post
How's t and t2 declared?

FILE *fp=fopen("element.txt","rw+"); How about checking that the file was opened?
if ( fp == NULL )
{
<error...>
}


If fread worked for 1st and 2nd time, then surely the file was opened.

t and t2 are integers.
Maybe the problem is in wich mode i open the file? like "wr+"
Last edited by Alex_ : Jun 16th, 2008 at 6:21 pm.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,647
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 144
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #6  
Jun 16th, 2008
http://www.c-faq.com/stdio/fupdate.html

[edit]Hm. From n1124:
When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters endof- file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.
Last edited by Dave Sinkula : Jun 16th, 2008 at 6:32 pm.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,437
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 10
Solved Threads: 100
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #7  
Jun 16th, 2008
Originally Posted by Alex_ View Post
If fread worked for 1st and 2nd time, then surely the file was opened.

Regardless, I was giving you a suggestion towards good code practice.

Originally Posted by Alex_ View Post
Maybe the problem is in wich mode i open the file? like "wr+"

fread() and fwrite() requires you to open the file in binary mode, appending a `b' to it.
i.e. "r" would be "rb"
Last edited by Aia : Jun 16th, 2008 at 6:42 pm.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Jun 2008
Posts: 14
Reputation: Alex_ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Alex_ Alex_ is offline Offline
Newbie Poster

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #8  
Jun 16th, 2008
Evrica! It works!
Thank you Deave Sinkula and Aia! Without you i wouldn't have understood the mistake...by now.
Tomorrow i'll get a 9 (nine) for this program!

One last question: Why does the sorting goes well till n-1,but at n it just copies n-1?
I.e: 5 4 6 3 7 2 8 1 9
sorted: 1 2 3 4 5 6 7 8 8

FILE *fp=fopen("element.txt","rb+");
fseek(fp, SEEK_SET, 0);

while(i<n-1){filepos=2*i;
  fsetpos(fp, &filepos);
  fread(&t, sizeof(int), 1, fp);

  j=i+1;
 while(j<n){
	filepos2=2*j;
	fsetpos(fp, &filepos2);
	fread(&t2, sizeof(int), 1, fp);
  if(t>t2){
	 fsetpos(fp, &filepos);
	 fwrite(&t2,sizeof(t2),1,fp);
	 fsetpos(fp, &filepos2);
	 fwrite(&t,sizeof(t),1,fp);t=t2;}
 ++j;}
++i;}
Last edited by Alex_ : Jun 16th, 2008 at 7:34 pm.
Reply With Quote  
Join Date: Dec 2006
Posts: 1,437
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 10
Solved Threads: 100
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #9  
Jun 16th, 2008
You would have to post your modified code, to answer that last question.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: Jun 2008
Posts: 14
Reputation: Alex_ is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
Alex_ Alex_ is offline Offline
Newbie Poster

Re: fread() doesn't work when 're-used' in a for( ; ;) function

  #10  
Jun 16th, 2008
Topic Solved by Aia
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC