could any 1 tell me why this code isnt working right??
two.txt is not wriiten good...
this is the code
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 30
#define LENST 6
int *saveid[5];
char *savename[5];
unsigned *savegrd[5];
unsigned *saveavg[5];
int j=0,k=0;
int min=0;
int skip[6] = {0};
typedef struct
{
int id; //מספר הזהות של הסטודנט
char full_name[LEN]; //שם ושם משפחה
struct
{
unsigned sem_a; //ממוצע הציונים בסמסטר ראשון
unsigned sem_b; //ממוצע הציונים בסמסטר שני
}grades; //משתנה של ציונים מטיפוס של מבנה
}Student;
Student A[]={
{10," SasonSasoniBenSason ",100,100},
{5," Pinokio ",70,90},
{20," JohnRambo ",50,87},
{3,"Elvis",3,77},
{50," JamesBond", 17,1},
{40,"BillGates",91,8}
};
FILE *f;
FILE *to_f;
char first;
char second;
char c,t,g,s;
void write_2_file( char first) //writing the first file. after that im not allowed to touch A struct again.
{
int a=1, i;
f=fopen("one.txt", "w");
if (f==NULL) exit(1);
else
{
printf("the file one has been opened \n\r");
fflush(stdin);
for(i=0;i<LENST;i++)
{
fprintf(f, "%d %s %u %u\n\r", A[i].id, A[i].full_name, A[i].grades.sem_a, A[i].grades.sem_b);
}
if (fclose(f)) exit(1);
else
printf("\nthe file has been closed\n\r");
}
}
int min_index ( int saveid[], int skip[], size_t n ) // בודקת מה הערך הכי קטן
{
int min = 0;
size_t k;
while ( min < n && skip[min] == 1 )
++min;
for ( k = 1; k < n; k++ )
{
if ( skip[k] != 1 && saveid[k] < saveid[min] )
min = k;
}
return min;
}
void coping(char first, char second) // this function need to copy the first file to a second file sorted with no sort function.
{
f=fopen("one.txt", "r");
to_f=fopen("two.txt", "w");
if ((f== NULL) || (to_f==NULL)) exit(1);
else
{
printf("the files have been opened\n\r");
while ((!feof(f)) && (j<6)) // כותב את קובץ 1 למערכים
{
fscanf(f, "%d %s %u %u", &saveid[j],&savename[j],&savegrd[j],&saveavg[j]);
flushall();
printf("%s\n", &savename[j]);
j++;
}
for ( k = 0; k < 6; k++ )// מתחיל את מציאת הקטן
{
int min = min_index ( saveid, skip, 6 );
flushall();
fprintf (to_f, "%d %s %u %u\n", saveid[min], &savename[min], savegrd[min], saveavg[min] );
skip[min] = 1;
}
if((fclose(to_f)) || (fclose(f))) exit(1);
else
printf("\n\rthe files had been closed\n\r");
}
}
void main()
{
write_2_file(first);
coping(first,second);
}