Help with sorting an char array

I need help with this code:
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
fstream f;
char s[81];
void in()
{
	n=0;
	f.open("file.txt",ios::out);
	while(cin.getline(s,81))
	{
		f<<s<<endl;
		n++;
	}
	cin.clear();
	f<<n-1;
	f.close();
}
void out()
{
	f.open("file.txt",ios::in);
	while(f.getline(s,81))
		cout<<s<<endl;
	f.close();
}
void menu()
{
	cout<<"\nMenu: ";
	cout<<"\n1. Zapisvane na tekst v faila.";
	cout<<"\n2. 4etene na tekst ot faila.";
	cout<<"\n3. Sortirane na informaciqta v faila.";
	cout<<"\n4. Izhod";
}
void sort()
{
	char min[81];
	int i,j,k;
	f.open("file.txt",ios::in|ios::out);
	f.seekg(-1,ios::end);
	f>>n;
	for (i=0;i<n-1;i++)
	{
		k=i;
		f.seekg(i,ios::beg);
		f.getline(s,81);
		strcpy(min,s);
		for (j=i+1;j<n;j++)
		{
			f.seekg(j,ios::beg);
			f.getline(s,81);
			if(strcmp(min,s)>0)
			{
				k=j;
				strcpy(min,s);
			}
		}
		
		if(k!=i)
		{
			f.seekg(i,ios::beg);
			f.getline(s,81);
			f.seekp(i,ios::beg);
			f<<min;
			f.seekp(k,ios::beg);
			f<<s;
		}
	}
	f.close();
}
void main()
{
	int k;
	while (1)
	{
		cout<<"\nMenu: ";
		cout<<"\n1. Zapisvane na tekst v faila.";
		cout<<"\n2. 4etene na tekst ot faila.";
		cout<<"\n3. Sortirane na informaciqta v faila.";
		cout<<"\n4. Izhod";
		cout<<"\n\nIzberi opciq: ";
		cin>>k;
		while(k<1||k>4)
		{
			cout<<"\nERROR!\n\nIzberi opciq: ";
			cin>>k;
		}
		switch(k)
		{
			case 1: in(); break;
			case 2: out(); break;
			case 3: sort(); break;
			case 4: exit(0);
		}
	}
}


Specifically the sort function. When I use the function the array remains unsorted. The must be some mistake but I can't find it. Also i have to use the .seekp() and .seekg() function for this assinment. I have tried sorting it through a dynamic array and now i have to do it wihout one with the use of the .seekp() and .seekg() functions. Thank you.
Topic archived. No new replies allowed.