0

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.

:)

0

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. :twisted:

0

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.

0

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

0

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:

// 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);
}

Also added a few test printf(). Hope you are feeling better!

Just in case you are interested, here is the matching Python code:

# 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()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.