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);
	}

Recommended Answers

All 3 Replies

lol i can't read hebrew

could any 1 tell me why this code isnt working right??
two.txt is not wriiten good...

Because it's written poorly?

It's your job to tell us
1) what the program is supposed to do
2) what exactly goes wrong
3) why it's wrong
4) what is supposed to happen instead

I assume you haven't read Read Me: Read This Before Posting yet...

typedef struct
{
	int id;		//מספר הזהות של הסטודנט
	char full_name[LEN];		//שם ושם משפחה
    	struct
	{
		unsigned sem_a;		//ממוצע הציונים בסמסטר ראשון
		unsigned sem_b;		//ממוצע הציונים בסמסטר שני
	}grades;					//משתנה של ציונים מטיפוס של מבנה
}Student;

I believe you can benefit of reading this conversation. Click here
Nevertheless:

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 } }
	};

That's how you need to initialized grades inside Student A.

char first; 
char second;
void write_2_file( char first) /* first is passed without been initialized with meaningful value */
void coping(char first, char second) /* first and second are still not initialized with meaningful values */
int min_index ( int saveid[], int skip[], size_t n )
int min = min_index ( saveid, skip, 6 );  /* passing wrong saveid argument */

In fact you are passing wrong arguments in:

fscanf(f, "%d %s %u %u", &saveid[j],&savename[j],&savegrd[j],&saveavg[j]);
fprintf (to_f, "%d %s %u %u\n", saveid[min], &savename[min], savegrd[min], saveavg[min] );

main can not be void. Always returns an int.
fflush( stdin ) is incorrect as well. You can use fflush( stdout ), but fflush( stdin ) is in the land of undefined behavior.

while ((!feof(f)) && (j<6))

feof should never been used as the condition to exit a loop. It will loop at the end one more time that is desired.

Did I miss something else. Ah..., yes, what the heck are you thinking?. Just joking. ;)
I suggest you write small piece of the code at a time, and test them outside of the implementation first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.