/* In this program i am trying to Read these info from a text file called unsorted saved in D
These info are
A201456 23.15
B209356 6.58
C201232 7.5
D201172 1.2
E201653 3.68
1)i want to sort them in ascending order of the real number example (23.15 ).

2)I want also to upgrade it to give the user a choice wether to sort according
to the code or the real number followed i must use merge sort algorithm

I did this but when i try to excute it closes the program nothing is done , i dont know
what is wrong Please could any one correct it if it is possible to do it that way
or write a better way if he or she could
Please i just beginner to C

Thanks alot brothers & sisters


*/

//start
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
 
  #define  N 5
  
  
   /* The type representing a student's record */
typedef struct student {
    char code[N];
    int marks;
} Student;
  
  //Functions prototypes
  //To sort a content of a file in ascending order of real numbers
  void sort(Student *arr,int low,int mid,int high);
  //To Rewrite the file sorted
  int WriteF(FILE *f,Student *strup);
  //To check wether a file is open properlly
  int openfile(FILE *f,int mode);
  //To split array
   void partition(Student *arr,int low,int high);
  //int checkF(FILE *f);
 
  
  
                     //The main interface
  int main(){
      
         FILE *fp;
         Student classA[N];
         char temp[15];
         int i;
               if(openfile(fp,0)!=1)
                 printf("Unable to open file");   
           else {
                 //To Laod file to an array of structures
           for(i=0;i<N&& (fgets(temp,13,fp))!=NULL;i++)
              sscanf(temp,"%8s%f",classA[i].code,classA[i].marks);
              
                fclose(fp);
                 }     
         /* To pass the address of the array of structures and sort it */
               partition(classA,0,N-1);   
         /*Write sorted file */        
           if(openfile(fp,1)!=1)
             printf("Unable to open file");
              else 
                    WriteF(fp,classA); 
                    fclose(fp);
                    
                     
                    return 0;                                       
    }                      //End of main
                   
                   
                   
    //function to Write 
 int WriteF(FILE *f, Student *strup){
                     int i;
                 for(i=0;i<N;i++)
                     fprintf(f,"%s  %f\n",strup[i].code,strup[i].marks);             
                        fclose(f);
                       
                 }      
   //function to sort the file 
 void sort(Student *arr,int low,int mid,int high)
     {
              int i,j,k,l;
      Student b[20];
        l=low;
        i=low;
        j=mid+1;
 while((l<=mid)&&(j<=high))
   {
    if(arr[l].marks<=arr[j].marks)
      {
       b[i].marks=arr[l].marks;
       strcpy(b[i].code,arr[l].code);
       l++;
      }
    else
      {
       b[i].marks=arr[j].marks;
       strcpy(b[i].code,arr[j].code);
       j++;
      }
    i++;
   }
 if(l>mid)
   {
    for(k=j;k<=high;k++)
       {
        b[i].marks=arr[k].marks;
        strcpy(b[i].code,arr[k].code);
        i++;
       }
   }
 else
   {
    for(k=l;k<=mid;k++)
       {
        b[i].marks=arr[k].marks;
        strcpy(b[i].code,arr[k].code);
        i++;
       }
   }
 for(k=low;k<=high;k++)
    {
     arr[k].marks=b[k].marks;
     strcpy(arr[k].code,b[k].code);
    }
    
    
}
            
//For partition
 void partition(Student *arr,int low,int high)
    {    
 int mid;
  if(low<high)
         {
    mid=(low+high)/2;
    partition(arr,low,mid);
    partition(arr,mid+1,high);
        //Sort them
   sort(arr,low,mid,high);
         }
}


  //To open file 
int openfile(FILE *f,int mode)
  { 
      //To make sure the file is open for reading 
      if(mode ==0){
         if((f=fopen("D:\\unsortedfile.txt","r"))==NULL) 
               return 0;
           else  
               return 1;
                }
               
               else if(mode==1){
                    if((f=fopen("D:\\unsortedfile.txt","w"))==NULL) 
                return 0;
           else  
                return 1;
             }         
      } 
      //End

Recommended Answers

All 14 Replies

You should read the 'Read me first" thread before you post here. http://www.daniweb.com/forums/thread78060.html
In particular, it is much easier for us to read your code if you use the CODE-tags as described in that thread.

You should read the 'Read me first" thread before you post here. http://www.daniweb.com/forums/thread78060.html
In particular, it is much easier for us to read your code if you use the CODE-tags as described in that thread.

Now you are encouraged to use the "Bad Flag Post" button, to let the moderators deal with that issue of proper code tags. It prevents "chastisement" from regulars or not so regulars, and improves the overall atmosphere of "too many Chiefs and too little Indians"

commented: right. +28

What exactly is the problem ? When you say nothing is happening its really not that descriptive ....
Are you able to open the input/output files ?
Are you getting garbage o/p ?
Are you getting compile time/ run time errors ?

The more detail that you give about the error, the faster it will get solved

The file is open ,but it is not sorted in ascending order , i guess it is Runtime error ,Please!! try to help

What exactly is the problem ? When you say nothing is happening its really not that descriptive ....
Are you able to open the input/output files ?
Are you getting garbage o/p ?
Are you getting compile time/ run time errors ?

The more detail that you give about the error, the faster it will get solved

Thanks alot ..for your reply
The file is open ,but it is not sorted ascendingly .i guess it is a run time error please!! try to compile it i need help !

> The file is open
No, it just seem to be open. Your openfile function does open a file, but does not pass it back to main. The main's fp remains uninitialized.

Use pointer to pointer for passing the FILE pointer

Also you dont need the if condition on line 92.
After the while loop just use the 2 for loops
for(k=l;k<=mid;k++)
{}
for(k=j;k<=high;k++)
{}

that it. If you cant understand why this is sufficient, work it out on a paper and pencil. Also print the array at the end of the sort function. So you can see how the array is getting sorted. So if there is a bug it will be easier for you.
If you still cant debug, paste the input to the sort function and its final output here

> The file is open
No, it just seem to be open. Your openfile function does open a file, but does not pass it back to main. The main's fp remains uninitialized.

Could you tell me how to pass it back to the main ?,i would be grateful ..i am not really very good in programming but trying my best to be good

> The file is open
No, it just seem to be open. Your openfile function does open a file, but does not pass it back to main. The main's fp remains uninitialized.

could you please tell me how to pass it back to the main ??,i would be grateful

Could you tell me how to pass it back to the main ?,i would be grateful ..i am not really very good in programming but trying my best to be good

i deleted the function openfile And i opened the file inside the main but still not working !

//The main interface

		int main(){

		FILE *fp;



		Student classA[N];

		char temp[15];

		int i;

		if((fp=fopen("C:\\Documents and Settings\\Muhammad Bilal\\Desktop\\unsorted.txt","r"))==NULL)

			  printf("Unable to open file");

		else {

		//To Laod file to an array of structures

		for(i=0;i<N&& (fgets(temp,13,fp))!=NULL;i++)

		sscanf(temp,"%8s%f",classA[i].code,classA[i].marks);



		fclose(fp);

		}

		/* To pass the address of the array of structures and sort it */

		partition(classA,0,N-1);

		/*Write sorted file */

		if((fp=fopen("C:\\Documents and Settings\\Muhammad Bilal\\Desktop\\unsorted.txt","r"))==NULL)

		printf("Unable to open file");

		else

		WriteF(fp,classA);

		fclose(fp);

		return 0;

		} //End of main

Don't return an integer, return the open file pointer. (Unless the open failed, in which case return 0). Then, in main, use that value, though you have to test it to see that it is not 0 before you use it.

line 39 is probably wrong (you are opening the same file) (and in 'read' mode, too)

could you please tell me how to pass it back to the main ??,i would be grateful

Strictly speaking "pass it back to the main" would be returning that filepointer.

FILE *open(FILE *opensesame)
{
    opensesame = fopen(/* blah, blah */); /* check return at main */
    return opensesame; /* will return NULL if not successful */
}

where at main: FILE *file_handle = open(file_handle); However you could make use of indirection. And avoid returning.

void open(FILE **opensesame)
{
    *opensesame = fopen(/* blah blah */); /* check NULL at main */
}

where at main:

FILE *file_handle;
open(&file_handle);

Or both

FILE *open (FILE **opensesame)
{
    *opensesame = fopen(/*blah blah */);
    return *opensesame; /* will return NULL if not successful */
}

Where at main: FILE *file_handle = open(&file_handle); If you don't want to return a FILE pointer, but rather another value type and still change the FILE pointer in main

int open (FILE **opensesame)
{ 
    int integer;
    *opensesame = fopen(/*blah, blah */);
    /* blah and more blah */
    /* if blahblah */
    /* else blah blabling */
    return integer;
}

Where at main:

FILE *file_handle;
int check;

check = open(&file_handle);

Did your problem get solved ?
If not where are you stuck now ?

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.