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.

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


for(i=0;i<n;i++){
  fgetpos(fp, &filepos);
  fread(&t, sizeof(int), 1, fp);
 for(j=i+1;j<n-1;j++){
   fgetpos(fp, &filepos2);//t2=NULL;
   fread(&t2, sizeof(int), 1, fp); //<< 'broken', t2's value does not change
   if(t>t2){
	 fsetpos(fp, &filepos);          //
	 fwrite(&t2,sizeof(t2),1,fp);  // <<All of this doesn't work also.
	 fsetpos(fp, &filepos2);        //File remains the same.
	 fwrite(&t,sizeof(t),1,fp);}   //
 }
}
fclose(fp);

Recommended Answers

All 9 Replies

You could try checking return values such that the code itself tells you what the problem is.

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...>
}

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).

int i,j,t,t2;
fpos_t filepos,filepos2;
FILE *fp=fopen("element.txt","rw+");
fseek(fp, SEEK_SET, 0);

do{filepos+=2*i;
  pos=ftell(fp);
  fread(&t, sizeof(int), 1, fp);
 do{j=i+1;
	filepos2+=2*j;
   fread(&t2, sizeof(int), 1, fp);//<<Broken when the compiler hits it for the 3rd time.
   pos=ftell(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++;fclose(fp);FILE *fp=fopen("element.txt","rw+");}while(j<n-1);
i++;}while(i<n);

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+"

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.

commented: Make the rep count +17

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.

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"

commented: Make the rep count +17

Evrica! It works! :P
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;}

You would have to post your modified code, to answer that last question.

Topic Solved by Aia

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.