egen.h
typedef struct postTyp
{
char namn[30];
char efternamn[30];
char klubb[30];
}postTyp;
FILE *openfil();
startordning.h
#define MAX_SKIERS 100
typedef struct nummer
{
int skiers[MAX_SKIERS];
int pos1;
int pos2;
int temp;
}nummer;
FILE *openfil();
tid.h
typedef struct tid
{
float tid[10];
}tid;
FILE *openfil();
main.c
#include "egen.h"
#include "startordning.h"
#include "tid.h"
void registrera(int x) {
FILE *fp;
char filnamn[] = "test.dat";
postTyp post;
fp = openfil(filnamn, filnamn, filnamn);
if(fp==NULL){
exit(1);
}
fseek(fp, 0, SEEK_SET); // går först i filen
fread(&post, sizeof(postTyp), 1, fp); // läser av filen
while(!feof(fp)){
fread(&post, sizeof(postTyp), 1, fp); //läser av filen om det är sant
x++;
}
while (x < 3) {
fseek(fp, 0, SEEK_END);
printf("\nAnge namn: ");
gets(post.namn);
printf("Ange efternamn: ");
gets(post.efternamn);
printf("Ange klubb: ");
gets(post.klubb);
fwrite(&post, sizeof(postTyp), 1, fp);
fseek(fp, 0, SEEK_SET);
fread(&post, sizeof(postTyp), 1, fp);
while(!feof(fp)){
//printf("\n%s %s %s\n", post.namn, post.efternamn, post.klubb);
fread(&post, sizeof(postTyp), 1, fp);
}
x++;
}
fclose(fp);
}
void Copy() {
FILE *fptr1, *fptr2;
char c;
// Open one file for reading
fptr1 = fopen("test.dat", "r");
if (fptr1 == NULL)
{
exit(0);
}
// Open another file for writing
fptr2 = fopen("test2.dat", "w");
if (fptr2 == NULL)
{
exit(0);
}
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
{
fputc(c, fptr2);
c = fgetc(fptr1);
}
fclose(fptr1);
fclose(fptr2);
//printf("\ncopy succesful\n");
}
void startordning(int x) {
FILE *fp;
char filnamn[] = "test.dat";
postTyp post;
fp = openfil(filnamn, filnamn, filnamn);
if(fp==NULL){
exit(1);
}
fseek(fp, 0, SEEK_SET); // går först i filen
fread(&post, sizeof(postTyp), 1, fp); // läser av filen
while(!feof(fp)){
fread(&post, sizeof(postTyp), 1, fp); //läser av filen om det är sant
x++;
}
nummer j;
srand(time(NULL));
int i;
for (i = 0; i <= x; i++){
j.skiers[i] = i;
}
for (i = 0; i < x*2; i++) {
// Generate two random positions
j.pos1 = rand() % x + 1;
j.pos2 = rand() % x + 1;
// Swap the skiers at the two positions
j.temp = j.skiers[j.pos1];
j.skiers[j.pos1] = j.skiers[j.pos2];
j.skiers[j.pos2] = j.temp;
}
fseek(fp, 0, SEEK_SET);
fread(&post, sizeof(postTyp), 1, fp);
i=1;
while(!feof(fp)){
printf("\n\n%s %s %s %d", post.namn, post.efternamn, post.klubb, j.skiers[i]);
fread(&post, sizeof(postTyp), 1, fp);
//printf(" %d", j.skiers[i]);
i++;
}
}
void aktider(int x) {
FILE *fp;
char filnamn[] = "test2.dat";
postTyp post;
nummer j;
tid w;
fp = openfil(filnamn, filnamn, filnamn);
if(fp==NULL){
exit(1);
}
int i;
fseek(fp, 0, SEEK_SET);
fread(&post, sizeof(postTyp), 1, fp);
i=1;
while(!feof(fp)){
printf("\n\n%s %s %s %d", post.namn, post.efternamn, post.klubb, j.skiers[i]);
fread(&post, sizeof(postTyp), 1, fp);
//printf(" %d", j.skiers[i]);
i++;
x++;
}
int p = 0;
int number;
while(p < 3) {
printf("\nEnter startnumber on the one you wanna give a time to!\n");
scanf("%d", &number);
for (i = 0; i < x + 1; i++)
{
if (j.skiers[i] == number) /* If required element is found */
{
printf("What time do you wanna give the person?\n");
scanf("%f", &w.tid[i]);
p++;
break;
}
}
}
fseek(fp, 0, SEEK_SET);
fread(&post, sizeof(postTyp), 1, fp);
i=1;
while(!feof(fp)){
printf("\n\n%s %s %s %d %.2f", post.namn, post.efternamn, post.klubb, j.skiers[i], w.tid[i]);
fread(&post, sizeof(postTyp), 1, fp);
//printf(" %d", j.skiers[i]);
//printf(" %.2f ", w.tid[i]);
i++;
}
}
int main(int argc, const char* argv[]){
int x = 0;
registrera(x);
Copy();
startordning(x);
aktider(x);
return 0;
}
FILE *openfil(char namn[], char efternamn[], char klubb[]) {
FILE *fpLokal;
if((fpLokal = fopen(namn, "r+b")) == NULL)
if ((fpLokal = fopen(namn, "w+b")) == NULL) {
printf("fel\n");
return NULL;
}
return fpLokal;
}
Above is my code and it works completly fine.It is currently printing this for example from a textfile
Hanna Svensson FCB 2 35.37
Patrik Svensson FCB 3 56.23
Oscar Svensson FCB 1 30.20
What I wanted to do next is sort the textfile so it printed like this instead
Oscar Svensson FCB 1 30.20
Hanna Svensson FCB 2 35.37
Patrik Svensson FCB 3 56.23
But whatever i do i smiply can't accheive this. Any tips on how I may accomplish this? Sry for the long post.