•
•
•
•
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
![]() |
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
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.
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.
language Syntax (Toggle Plain Text)
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);
Last edited by Alex_ : Jun 16th, 2008 at 5:11 pm.
How's t and t2 declared?
Upon success fread() returns the amount of elements read in a form of size_t object; why don't you check that?
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
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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).
language Syntax (Toggle Plain Text)
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);
Last edited by Alex_ : Jun 16th, 2008 at 6:25 pm.
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
•
•
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.
http://www.c-faq.com/stdio/fupdate.html
[edit]Hm. From n1124:
[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.
•
•
•
•
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.
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
•
•
Join Date: Jun 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
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

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.
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Getting IP using a flat file wont work properly (PHP)
- function returns address out of bounds (C)
- fread, fwrite issues (C)
- Sorting structures (C)
- Bubble sort & File output jibrish errors??? (C)
- c program to extract system info (C)
- change spaces in fields submitted (PHP)
- C help calculate grand total (C)
- reading a file into code (Java)
Other Threads in the C Forum
- Previous Thread: Pointers errors and file handling
- Next Thread: Complete C Program to draw a sin curve without any application



Linear Mode