| | |
Is it possible to arange lines in a file alphabetically?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
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.

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
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
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
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:
Whyyyyy?
Here's the code:
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define numofstr 50 #define strlen 101 int compare(char *,char *); main(){ FILE *a,*b; char s[100]; /*starr - string array*/ int i=0; char starr[numofstr][strlen]; a=fopen("C:\\C_fajlovi\\non_sorted.txt","r"); b=fopen("C:\\C_fajlovi\\sorted.txt","w"); while(fscanf(a,"%s",&s)!=EOF){ strcpy(starr[i],s); i++; } qsort(starr,numofstr,strlen,compare); } int compare(char*a,char*b){ return strcmp(a,b); }
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
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
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
•
•
•
•
>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.
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
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
Hmm looks like the code sample is broken.
Try this:
Try this:
C Syntax (Toggle Plain Text)
int compare_ints( const void* a, const void* b ) { char* arg1 = (char*) a; char* arg2 = (char*) b; return strcmp( arg1, arg2 ) ; }
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
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
•
•
Join Date: Oct 2006
Posts: 23
Reputation:
Solved Threads: 0
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define numofstr 50 #define strleng 101 int compare_ints(const void *,const void *); main(){ FILE *a,*b; char s[100]; /*starr - string array*/ int i=0; char starr[numofstr][strleng]; a=fopen("C:\\C_fajlovi\\non_sorted.txt","r"); b=fopen("C:\\C_fajlovi\\sorted.txt","w"); while(fscanf(a,"%s",&s)!=EOF){ strcpy(starr[i],s); i++; } qsort(starr,numofstr,strleng,compare_ints); for(i=0;i<numofstr;i++) fprintf(b,"%s\n",starr[i]); } int compare_ints( const void* a, const void* b ) { char* arg1 = (char*) a; char* arg2 = (char*) b; return strcmp( arg1, arg2 ) ; }
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
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
fscanf( ) accpets the addr of the variable where the data is to be stored.
What you have is:
Try replacing &s by &s[0] or s and then see.
If you cant help atleast keep your bad sense of humour to yourself.
What you have is:
C Syntax (Toggle Plain Text)
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.
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
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
![]() |
Other Threads in the C Forum
- Previous Thread: pthread
- Next Thread: Visual Studio Team System
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






