Is it possible to arange lines in a file alphabetically?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Is it possible to arange lines in a file alphabetically?

 
0
  #31
Nov 20th, 2006
Hehehe look before you leap
Not wrong though, since I corrected my mistake before anyone else did. Plus I don't have a compiler to test anything.


~s.o.s~ : Take my life away and let me never be born in this sick sad little world.
If you've got a free basement I've got some chicken wire.

*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Is it possible to arange lines in a file alphabetically?

 
0
  #32
Nov 20th, 2006
Originally Posted by iamthwee View Post
Not wrong though, since I corrected my mistake before anyone else did. Plus I don't have a compiler to test anything.
Agreed...


If you've got a free basement I've got some chicken wire.
Its a good thing that I am non English plus I dont get to understand 99% of your humour, so all is well.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Is it possible to arange lines in a file alphabetically?

 
0
  #33
Nov 20th, 2006
I was once like you. I would write the working code for anyone. Then when my crown jewels dropped and started to grow hairs in places I never knew existed, I realised I never did really care.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,648
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Is it possible to arange lines in a file alphabetically?

 
0
  #34
Nov 20th, 2006
Naa here I didnt write the code buddy, just replaced the one function call with another thats it.

Also the MOD rules state that I am not allowed to post entire code to someone so I wouldnt dare go against them.

Oh.. wait.. damn..my crown jewels are dropping down..hehe
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Is it possible to arange lines in a file alphabetically?

 
0
  #35
Nov 20th, 2006
Originally Posted by Sin-da-cat View Post
All the words have one CAP in the beginning and then all lowercase so I don't see how it matters. It's not like there are gonna be caps in the middle of words that will interrupt the comparing.

EDIT: No, wait...some don't. But that's not the basic issue here.
Sin-da-cat, I can see how you got frustrated! To do a qsort on an array of strings is not the easiest coding chore. Arrays of strings in C drove me to adopt Python long ago! Well, here is modified code that actually works with your file, and does not throw in a bunch of empty lines into the sort that then creates havoc with your output file:
  1. // qsort of an array of strings
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define numofstr 50
  8. #define strleng 101
  9.  
  10. int compare(const void *,const void *);
  11.  
  12. int main()
  13. {
  14. FILE *a, *b;
  15. char s[100] = { '\0' }; // preload temp string array with '\0'
  16. int array_size;
  17. int i = 0;
  18. char starr[numofstr][strleng] = { '\0' }; // dito
  19.  
  20. // open text file, one data item per line
  21. a = fopen("non_sorted.txt", "r");
  22.  
  23. while( 1 )
  24. {
  25. if (! fgets( s, 100, a ))
  26. break ;
  27.  
  28. // remove trailing newline char
  29. if(s[strlen(s) - 1] == '\n')
  30. s[strlen(s) - 1] = '\0';
  31.  
  32. strcpy(starr[i], s) ;
  33. printf("%d --> %s\n", i, starr[i]); // test print
  34. i++;
  35. }
  36. // set true array size
  37. array_size = i;
  38.  
  39. qsort(starr, array_size, strleng, compare);
  40.  
  41. printf("----------- sorted ----------------\n"); // for test print
  42.  
  43. // open text file to write to
  44. b = fopen("sorted.txt", "w");
  45.  
  46. for(i = 0; i < array_size; i++)
  47. {
  48. // might add a space after string since ...\n does odd things in Pelles C!
  49. fprintf(b, "%s\n", starr[i]);
  50. printf("%d --> %s\n", i, starr[i]); // test print
  51. }
  52.  
  53. getchar(); // wait
  54. return 0 ;
  55. }
  56.  
  57. int compare(const void* a, const void* b)
  58. {
  59. char* arg1 = (char*) a;
  60. char* arg2 = (char*) b;
  61. // use stricmp() for case insensitive compare
  62. return strcmp(arg1, arg2);
  63. }
Also added a few test printf(). Hope you are feeling better!

Just in case you are interested, here is the matching Python code:
  1. # read a list of strings from a file, sort and write back out
  2. # language: Python
  3.  
  4. # load the data lines
  5. fin = open("non_sorted.txt", "r")
  6. data_list = fin.readlines()
  7. fin.close()
  8.  
  9. # show raw lines
  10. for names in data_list:
  11. print names,
  12.  
  13. # inplace sort, case insensitive, much faster than qsort
  14. data_list.sort(key=str.lower)
  15.  
  16. print "----------- sorted ----------------"
  17.  
  18. # show sorted lines
  19. for names in data_list:
  20. print names,
  21.  
  22. # write sorted data lines back out
  23. fout = open("sorted.txt", "w")
  24. fout.writelines(data_list)
  25. fout.close()
Last edited by vegaseat; Nov 20th, 2006 at 7:25 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC