Is it possible to arange lines in a file alphabetically?

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

Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

 
0
  #11
Nov 20th, 2006
Did that...works...tried to use qsort..getting a bunch of errors...losing temper.

Can you please just type the rest of the code for me? This isn't even part of my course, I just want to sort my <snip> music.
Last edited by Ancient Dragon; Nov 20th, 2006 at 4:00 pm.
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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
  #12
Nov 20th, 2006
Study the links which I have given you, my writing the code wont help you in any way.
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
  #13
Nov 20th, 2006
>This isn't even part of my course, I just want to sort my <snip> music.

I don't believe you. I think this is homework and you are trying to trick me.
Last edited by Ancient Dragon; Nov 20th, 2006 at 4:02 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

 
0
  #14
Nov 20th, 2006
I have studied the links and written a code that seems identical to the one in the 'solved example' page. Still, I'm getting a 'cannot convert 'int(*)(char *,char *)' to 'int(*)(const void *,const void*)' message, and I don't see why.

Here's the code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define numofstr 50
  5. #define strlen 101
  6. int compare(char *,char *);
  7. main(){
  8. FILE *a,*b;
  9. char s[100]; /*starr - string array*/
  10. int i=0;
  11. char starr[numofstr][strlen];
  12. a=fopen("C:\\C_fajlovi\\non_sorted.txt","r");
  13. b=fopen("C:\\C_fajlovi\\sorted.txt","w");
  14. while(fscanf(a,"%s",&s)!=EOF){
  15. strcpy(starr[i],s);
  16. i++;
  17. }
  18. qsort(starr,numofstr,strlen,compare);
  19. }
  20. int compare(char*a,char*b){
  21. return strcmp(a,b);
  22. }

Whyyyyy?
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

 
0
  #15
Nov 20th, 2006
Originally Posted by iamthwee View Post
>This isn't even part of my course, I just want to sort my <snip> music.

I don't believe you. I think this is homework and you are trying to trick me.
I'm studying at an electrical engineering university. I don't have homework and I'm not trying to trick you. If you're gonna post I'd rather you tell me what's wrong with my code.
Last edited by Ancient Dragon; Nov 20th, 2006 at 4:02 pm.
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
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
  #16
Nov 20th, 2006
I wouldn't use strlen as a variable since it is an actual function.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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
  #17
Nov 20th, 2006
Hmm looks like the code sample is broken.

Try this:
  1. int compare_ints( const void* a, const void* b ) {
  2. char* arg1 = (char*) a;
  3. char* arg2 = (char*) b;
  4. return strcmp( arg1, arg2 ) ;
  5. }
Last edited by ~s.o.s~; Nov 20th, 2006 at 2:52 pm.
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 2006
Posts: 23
Reputation: Sin-da-cat is an unknown quantity at this point 
Solved Threads: 0
Sin-da-cat Sin-da-cat is offline Offline
Light Poster

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

 
0
  #18
Nov 20th, 2006
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define numofstr 50
  5. #define strleng 101
  6. int compare_ints(const void *,const void *);
  7. main(){
  8. FILE *a,*b;
  9. char s[100]; /*starr - string array*/
  10. int i=0;
  11. char starr[numofstr][strleng];
  12. a=fopen("C:\\C_fajlovi\\non_sorted.txt","r");
  13. b=fopen("C:\\C_fajlovi\\sorted.txt","w");
  14. while(fscanf(a,"%s",&s)!=EOF){
  15. strcpy(starr[i],s);
  16. i++;
  17. }
  18. qsort(starr,numofstr,strleng,compare_ints);
  19. for(i=0;i<numofstr;i++)
  20. fprintf(b,"%s\n",starr[i]);
  21. }
  22. int compare_ints( const void* a, const void* b ) {
  23. char* arg1 = (char*) a;
  24. char* arg2 = (char*) b;
  25. return strcmp( arg1, arg2 ) ;
  26. }

That would be the whole thing with the last change. Now it doesn't report any errors but I get a Access violation at address...' pop-up and it creates a file called 'sorted.txt' but it stays empty.
Last edited by Sin-da-cat; Nov 20th, 2006 at 3:04 pm.
In the twilight zone we disperse cowards//
vampires that stalk Earth on reverse hours - Jus Allah

And on top of that they still wanna take me to prison//
just cause I won't trade humanity for patriotism - Immortal Technique
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?

 
-1
  #19
Nov 20th, 2006
starr expects a 2d array

What do you think this is...

strcpy(starr[i],s);

Is that a 2d array?

No offence but I'd scrap that and try again. SOS has given you a crappy example ha ha.
Last edited by iamthwee; Nov 20th, 2006 at 3:08 pm.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
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
  #20
Nov 20th, 2006
fscanf( ) accpets the addr of the variable where the data is to be stored.

What you have is:
  1. while(fscanf(a,"%s",&s)!=EOF)


Try replacing &s by &s[0] or s and then see.

No offence but I'd scrap that and try again. SOS has given you a crappy example ha ha.
If you cant help atleast keep your bad sense of humour to yourself.

Last edited by ~s.o.s~; Nov 20th, 2006 at 3:12 pm.
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  
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