| | |
Is it possible to arange lines in a file alphabetically?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
•
•
Hehehe look before you leap
•
•
•
•
~s.o.s~ : Take my life away and let me never be born in this sick sad little world.
*Voted best profile in the world*
•
•
•
•
Not wrong though, since I corrected my mistake before anyone else did. Plus I don't have a compiler to test anything.
•
•
•
•
If you've got a free basement I've got some chicken wire.
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
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
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
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
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
•
•
•
•
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.
c Syntax (Toggle Plain Text)
// qsort of an array of strings #include <stdio.h> #include <stdlib.h> #include <string.h> #define numofstr 50 #define strleng 101 int compare(const void *,const void *); int main() { FILE *a, *b; char s[100] = { '\0' }; // preload temp string array with '\0' int array_size; int i = 0; char starr[numofstr][strleng] = { '\0' }; // dito // open text file, one data item per line a = fopen("non_sorted.txt", "r"); while( 1 ) { if (! fgets( s, 100, a )) break ; // remove trailing newline char if(s[strlen(s) - 1] == '\n') s[strlen(s) - 1] = '\0'; strcpy(starr[i], s) ; printf("%d --> %s\n", i, starr[i]); // test print i++; } // set true array size array_size = i; qsort(starr, array_size, strleng, compare); printf("----------- sorted ----------------\n"); // for test print // open text file to write to b = fopen("sorted.txt", "w"); for(i = 0; i < array_size; i++) { // might add a space after string since ...\n does odd things in Pelles C! fprintf(b, "%s\n", starr[i]); printf("%d --> %s\n", i, starr[i]); // test print } getchar(); // wait return 0 ; } int compare(const void* a, const void* b) { char* arg1 = (char*) a; char* arg2 = (char*) b; // use stricmp() for case insensitive compare return strcmp(arg1, arg2); }
Just in case you are interested, here is the matching Python code:
python Syntax (Toggle Plain Text)
# read a list of strings from a file, sort and write back out # language: Python # load the data lines fin = open("non_sorted.txt", "r") data_list = fin.readlines() fin.close() # show raw lines for names in data_list: print names, # inplace sort, case insensitive, much faster than qsort data_list.sort(key=str.lower) print "----------- sorted ----------------" # show sorted lines for names in data_list: print names, # write sorted data lines back out fout = open("sorted.txt", "w") fout.writelines(data_list) fout.close()
Last edited by vegaseat; Nov 20th, 2006 at 7:25 pm.
May 'the Google' be with you!
![]() |
Other Threads in the C Forum
- Previous Thread: pthread
- Next Thread: Visual Studio Team System
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue number oddnumber odf opensource overwrite owf pdf performance pointer posix problem probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






