hello all,
I have to write the code to read the file line by line and sort its words in alphabetical order n store into another file, i have done with the feathing the line from one file but strugling with the sorting part can anybody suggest how should i move forward..

here is code i have written:

#include<stdio.h>
#include<conio.h>
#define max_size 80

int main()
{

 FILE *fp,*fp1;
 int i;
 char file1[50],file2[50],buffer[80];

 int c;
 clrscr();
 printf("\n Enter File 1:\n");
 scanf("%s",file1);

 printf("\n Enter file 2: ");
 scanf("%s",file2);

 fp = fopen(file1,"r");
 fp1 = fopen(file2,"w" );

 if(fp == NULL)
 {
 printf("\n Error in opening the file 1.");
 }
 if(fp1==NULL)
 {
 printf("\n Error in opening the file 2.");
 }

//fatching line frm file one n storing in to the array  Buffer.
 if (fgets(buffer,max_size,fp)!=NULL); 
 {

 printf("%s",buffer);
/******************************************************From Here I am strugling ...
*****************************************************/

/* for (i=0;i<80 ;i++)
 {
  if(buffer[i] == " ")
  {
  printf("\n harshal");
  break;
  }
 puts(temp);

 printf("%s\n",buffer);
*/
 }
 printf("%s\n",temp);
 //fputs(buffer,fp1);
 //c=getc(fp);

 }

 fclose(fp);
 fclose(fp1);
 getch();
}

}

Read this article and pick an algorithm to use. To sort strings, the easiest way is to compare with the strcmp function and swap with the strcpy function (both found in the string.h header).

Note that to sort all of the strings, you'll want to keep them all in memory at one time, such as with an array of strings. It's possible to sort without having everything in memory, but it's probably more complicated to do so than you're ready for.

temp is not declare upside, so puts(temp); function gets error, and for files ,buffer , we have to use gets() function , after printf("buffer\n"); but not %s in printf(" ");

^ WTF did you just say?

:confused:

here's a simple algorithm you can try.

1) read your values into an array. lets say you have ten values, randomly located in array[0] - array[9]
2) decide which end will be lowest value, which will be highest. say array[0] will be low, and array[9] will be high.
3) start with the element in array[0] and compare it to each of the others, 1 - 9
4) if one of the elements is smaller than array[0], swap them. compare each of the remaining elements in turn. once you're done comparing to all nine other elements, array[0] will be the lowest value.
5) next look at array[1] and do the same thing, comparing against elements 2 - 9, swapping if its value is smaller. leave array[0] alone since it's already the smallest. once youre done, array[1] will then have the second-smallest value.
6) then go and get array[2] and compare it to each of the remaining elements, 3 - 9, leaving elements in array[0] and array[1] alone.
7) ....
8) and so on until you're done.

you'll need a nested loop to accomplish this. an example, but not necessarily a completely accurate example, is like such:.

for (j = 0; j < NUMBER_OF_SAMPLES; j++) 
{
    for (k = j; k < NUMBER_OF_SAMPLES; k++)
    {
        // do that voodoo that you do
        // ...
        // ...
    }
}

some questions for you to consider:

(1) how will you read all of the file into an array to begin with? hint: you're on the right track with "fgets()"...
(2) this example is for values. can alphabetical strings be treated as a value? how?
(3) in line #1, should i use (NUMBER_OF_SAMPLES - 1) or (NUMBER_OF_SAMPLES + 1) or leave it as is? why?
(4) in line #3, should i use k = (j+1) or k = (j-1) or leave it as is? why?

.

If possible (this case) better sort an array of pointers to words but not an array of words. In actual fact it's unclear what to do with multiple occurences of the same word in OP assignment. An optimal algorithm (and data structure) depends on this specification. For example, if you want to print text dictionary, better place all scanned words in some kind of (semi) balanced tree (AVL tree is a good candidate) then close file and make this tree traversal - all in one (pass), simple (no need in a special sort step) and fast O(n*log(n)) algorithm.

yeah, that's nice and all, but i think this is just a CSC 101 assignment to learn how to sort strings alphabetically.

i mean your 400-level solution is impressive, but i doubt fast tree algorithms is what a 100-level beginning student needs, when they're still struggling with loops and arrays.

:P


.

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.