OK, so the part of my course about files is over, but now I am wondering: if you have a file with, say, a bunch of names (each in its own line), is it possible to use "C" to make a new file with the names aranged alphabetically? For example, if the original file is:

Mike
Carl
John
Babe
Ferrari

the new one should be

Babe
Carl
Ferrari
John
Mike

If it can be done...well, explain how.

P.S. I'm asking because I want to arange my list of music albums, because right now, the names are held in a completely sporadic manner in a Word document.:cheesy:

Read all the lines into a container in your program. Sort the container. Then write the contents of the container back to your file, overwriting the previous unsorted contents of the file. What container you use is up to you based on your knowledge and experience. What sorting algorhythm you use is up to you, too, but choices include bubble sort, quick sort, insertion sort, etc.

A Word file probably has a lot of extraneuous gunk in it to make it pretty when you open the file in Word. If you can save the contents in a plain text file, like you would get with Note Pad, that might be easier to read from, unless you know the specific set up of the Word file. Of course, if you have no trouble reading in the Word file, then you don't have to worry about this paragraph.

Read all the lines into a container in your program.

This might sound stupid...but what is a container? Either it's an English word for something I'm familiar with (English isn't my first language) or I simply don't know what it is.

And please name some containers.

Word formatting isn't a problem.

P.S. I'm asking because I want to arange my list of music albums, because right now, the names are held in a completely sporadic manner in a Word document.:cheesy:

Copy and paste the contents of the word file in a clean text file to avoid unnecessary hassles. I dont think there would be a requirement of your course to read specificallly from a *word file* .

Word File is not a text editor but used to present text and such tools are not used for storing data. Data files of user either have a header format which the user has designated (like a game's save file) and he knows or they are just pure text with custom extension (names.sdc) .

This might sound stupid...but what is a container? Either it's an English word for something I'm familiar with (English isn't my first language) or I simply don't know what it is.
And please name some containers.

Simply put a container is something which is used to store loads of data or records and which can be accssed in O (1) time if the index of that element is know.

THe basic containers are well -- arrays :D in C and Vectors in C++.
If you using arrays you can use the inbuilt qsort( ) method to sort the array and if you usign vectors you can use the inbuilt sort( ) algorithm.

<snip>, it was a stupid question.:rolleyes:

I know about Word not being a pure text editor, I was gonna paste it into a notepad file.

Thanks.

main(){
FILE *a,*b;
char s[100];
int i=0,k;
char starr[50][101];                      /*50 - the number or strings I wanna put in the array*/
a=fopen("C:\\C_fajlovi\\non_sorted.txt","r");
b=fopen("C:\\C_fajlovi\\sorted.txt","w");    /*made a new file, want to keep the previous one too*/
while(fscanf(a,"%s",&s)!=EOF){
 starr[i]=s;
    i++;
    }
 
...

Why do I get 'Lvalue required' when I try to put a string in the array (starr=s)?

And what should I use instead if this brings up an error?

Dont use direct assignment operator. You can't treat strings like normal data types in terms of assignment.

You need to use strcpy to do somthing like that.

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.

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

#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?

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

Hmm looks like the code sample is broken.

Try this:

int compare_ints( const void* a, const void* b ) {
   char* arg1 = (char*) a;
   char* arg2 = (char*) b;
   return strcmp( arg1, arg2 ) ;
 }
#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.

starr expects a 2d array

What do you think this is...

strcpy(starr,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.

Comments
He's an asshole.

fscanf( ) accpets the addr of the variable where the data is to be stored.

What you have is:

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.

>If you cant help atleast keep your bad sense of humour to yourself.


I'm helping by offering him good advice. You on the other hand have given him a piece of code that don't work. And you're leading him down the garden path.

If you don't like the truth then I'm sorrie.

I'm helping by offering him good advice. You on the other hand have given him a piece of code that don't work. And you're leading him down the garden path.

If you don't like the truth then I'm sorrie.

Yeah I like truth but not twisted truth.

I gave him google links of how qsort is applied. I didnt write that tutorial so any problems he is facing I am trying to help him out unlike you who only says this is wrong and that is wrong without giving a shred of help.

Actions speak louder than words. All you do is just say this is wrong and that is wrong but never attempt to help someone out.

>I didnt write that tutorial so any problems he is facing I am trying to help him out unlike you

Well, ok maybe you might have posted that c code attachment without realising it was bad. Frankly any code which uses gets() would scream out bad to me :) However, instead of tampering with bad code, why not just show him how to do it properly.

I just got an infraction for saying '<snipped>' :lol: :lol: :lol: .I feel like Cartman in the South Park movie.

I posted my entire code a few posts above, and here is the file I was trying to sort (if that's what you asked for):

Afu Ra-State Of The Arts
Deltron 3030-Deltron 3030
Eminem-Infinite
JMT-Servants In Hell,...
Black Market Militia- Black Market Militia
Outerspace-Blood Brothers
Kunsealed Weppin-The Monarchy Begins
Guano Apes-Don't Give Me Names
Guano Apes-Walking On A Thin Line
Sticky Fingaz-Decade
The Roots-Illadelph Halflife
The Roots-Do You Want More
Roots Manuva-Awfully Deep
Immortal Technique & friends
Digable Planets-Reachin
Longshot-Sacrifice
Cage-Movies
Cage-Weatherproof
Ill Bill-I'm a goon
RZA-The Protector OST
The Roots-Phrenology
The Roots-Things Fall Apart
BIG-Ready To Die
Black moon-Enta D Stage
Smif N Wessun-Dah Shinin
Skillz-Got Skillz
AZ-The Format
Mad Skillz-Ain't Mad No More
OC-Jewelz
Molemen-CCL
MOP-Firing Squad
Wordsworth-Mirror Music
Heltah skeltah-Nocturnal
Eric B. & Rakim-Paid In Full
Eric B. & Rakim-Follow The Leader
Sean Price-Monkey Barz
Diana Krall-The Look Of love
Papoose-The Best Of Papoose (The Mixtape)
Gorillaz-Gorillaz
Atmosphere-God Loves Ugly
OC-Starchild

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.

I love BIG's tracks. They rock.

What do you think this is...

strcpy(starr,s);

Is that a 2d array?

In hindsight I think that might be ok.

In hindsight I think that might be ok.

Hehehe look before you leap. ;)

Here is the workiing code. I just replaced fscanf( ) with fgets( ) and it was fine.

#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' } ;                            /*starr - string array*/
    int i=0;
    char starr[numofstr][strleng] = { '\0' } ;

    a=fopen("non_sorted.txt","r");

    while( 1 )
    {
        if (! fgets( s, 100, a ) )
            break ;

        if( s[strlen( s ) - 1] == '\n' )
            s[strlen( s ) - 1] = '\0' ;

        strcpy(starr[i], s) ;
        i++;
    }

    qsort(starr,numofstr,strleng,compare);

    b=fopen("sorted.txt","w");

    for(i=0;i<numofstr;i++)
        fprintf(b,"%s\n",starr[i]);

    return 0 ;
}

int compare( const void* a, const void* b )
{
   char* arg1 = (char*) a;
   char* arg2 = (char*) b;
   return strcmp( arg1, arg2 ) ;
 }
This question has already been answered. Start a new discussion instead.