Need help !

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void sort(fstream&f)
{int k,min,a,b,i;
 f.open("f1.cpp");
 f.seekg(0,ios::end);int n=f.tellg()/sizeof(a);
 for(i=0;i<n-1;i++) {k=i; f.seekg(k*sizeof(a));
                     f.read((char*)(&min),sizeof(a));
				     for(int j=i+1;j<n;j++) {f.seekg(j*sizeof(a));
				                            f.read((char*)(&a),sizeof(a));
											if(a<min){k=j;min=a;}   
										   }
					 f.seekg(i*sizeof(a));
					 f.read((char*)(&a),sizeof(a));
					 f.seekg(k*sizeof(a));f.read((char*)(&b),sizeof(a));
					 f.seekg(k*sizeof(a));f.write((char*)(&a),sizeof(a));
					 f.seekg(i*sizeof(a));f.write((char*)(&b),sizeof(a));					   
					 }
					 	f.close();
					 }

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 !
Last edited on
Please use code tags when posting code.

Why couldn't you make it work with a structure? Please show what you tried.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
void sortirane(fstream &f)
	{
		int i,k;
		char m[20];
		eu r,min; 
		f.open("euro_cup.cpp");
		f.seekg(0,ios::end); int n=f.tellg()/sizeof(eu); 
		for(i=0;i<n-1;i++) 
			{
				k=i; f.seekg(k*sizeof(eu)); 
				f.read((char*)(&r),sizeof(eu)); strcpy(m,r.ime); 
				for(int j=i+1;j<n;j++)
					{
						f.seekg(j*sizeof(eu));
						f.read((char*)(&r),sizeof(eu));
						if (strcmp(r.ime,m)<0) {k=j;strcpy(m,r.ime);}
					}
				if(k!=i)
					{
						f.seekg(i*sizeof(eu)); f.read((char*)(&r),sizeof(eu));
						f.seekg(k*sizeof(eu)); f.read((char*)(&min),sizeof(eu));
						f.seekg(k*sizeof(eu)); f.write((char*)(&r),sizeof(eu));
						f.seekg(i*sizeof(eu)); f.write((char*)(&min),sizeof(eu));
					}
					
			}
		f.close();
	}


This is what i manage to do but its not working :(
Last edited on
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.

Thank you for the help !
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.

If eu is a typedef of your struct, something like this
1
2
3
4
5
typedef struct eu {
    char name[20];
    char surname[20];
    int age;
} eu;

then sizeof(eu) is correct.

What operating system are you on? You should open the file in binary mode, especially on Windows.

The filename looks odd. It ends .cpp like a C++ source file!
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.
Please post a small sample of your input file.

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.
Last edited on
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.

This is where a vector would come in handy.


I dont know how to use vector.

This is the code i made:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void sort(fstream &f)
	{
		int n=0,k,i;
		char m[20];
		eu r,d[1000],t;
		f.open("euro_cup.cpp");
		while(f.read((char*)(&r),sizeof(eu)))
				{
					d[n]=r;
					n++;
				}
		for(i=0;i<n-1;i++)
			{
				k=i; strcpy(m,d[0].name);
				for(int j=i+1;j<n;j++)
					if(strcmp(d[j].name,m)<0)
						{
							k=j; strcpy(m,d[j].name);
						}
				if(k!=i)
					{
						t=d[k];
						d[k]=d[i];
						d[i]=t;
					}
			}
		for(i=0;i<n;i++) cout<<d[i].name<<" "<<d[i].sur<<" "<<d[i].age<<endl;
		f.close();
	}


Where "eu" is the structure name. It is working perfectly.
Topic archived. No new replies allowed.