Hello !
Im a not so good with files, especially with Direct Access Files. So i need your help. I want sort structure in alphabetical order using Selection Sort.
My teacher give me this example sorting file with numbers:
But unfortunately for me, i couldnt make it work for structure. My structure contains:
char name[20];
char surname[20];
int age;
Thank you in advance !
You really really need to start using meaningful variable and function names. This will really help others read the code and make it easier to follow your logic.
What do you mean by "but its not working"?
Does the program compile without errors or warnings?
If not post the complete warning/error messages, all of them, exactly as they appear in your development environment.
What is an eu?
Why are you using C style casts instead of the safer C++ style casts? Maybe this is a question for your instructor?
Have you verified that sizeof(eu) is the correct value for your read and write calls?
Lastly (for now) your indentation needs serious work to make the program easier to read.
There arent any errors/warnings. The program is starting normally, but the print isnt correct(the first 2 names are random, with random age, the second 2 names that i write are the same) . "eu" is the name of the structure and "ime" is the name of the person.
I write it that way because i followed the algorithm that my teacher gave us with sorting numbers.
I am 70% sure that the error is that "sizeof(eu)" isnt correct value, but i dont know how to be sure.
I will be very thankful if you have code that sorts structures in file or some help how to fix mine.
It would be better if you posted a small complete program that illustrates the problem. Then we can compile and run the program to better see where the problems are occurring.
I am 70% sure that the error is that "sizeof(eu)" isnt correct value, but i dont know how to be sure.
So have you tried printing out the value returned from sizeof(eu)?
"eu" is the name of the structure and "ime" is the name of the person.
But how is "eu" defined? What type of variable is "ime"? You need to show more content.
At the beginning i said that im bad with files. My teacher wrote us the code that sorts numbers and i just rewrite it for structure. The problem is that i dont know whar is wrong and if someone has example program that sort structure will be perfect.
I suggest you start by sorting the "array" in memory before you try to sort the file using seekg()/etc. Once you get the logic of the sort correct it will be simpler to directly sort the file.
Also:
My teacher give me this example sorting file with numbers:
Did you verify that the given example actually worked correctly with a file of numbers?
Yes the example worked perfectly.
I made that:
Read the file and i made structure array with the data. Then i sorted the array alphabetically and it worked.
But if there are more than array's size data it wont work. I know the method of sorting but dont know how to transform it to work for a file.
Read the file and i made structure array with the data. Then i sorted the array alphabetically and it worked.
But if there are more than array's size data it wont work.
This is where a vector would come in handy.
I know the method of sorting but dont know how to transform it to work for a file.
Show how you sorted the structure in memory. The only real difference is the positioning of the file pointer.