954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Is it possible to arange lines in a file alphabetically?

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:

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 
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.

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>>but what is a container?

an array, std::vector, std::list, etc.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

, 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.

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Here comes another stupid question: how do you use qsort?

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

For a solved eg. see here.

For documentation look here.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
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[i]=s)?

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

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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 music.

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

Study the links which I have given you, my writing the code wont help you in any way.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

>This isn't even part of my course, I just want to sort my music.

I don't believe you. I think this is homework and you are trying to trick me.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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?

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 
>This isn't even part of my course, I just want to sort my 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:

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

I wouldn't use strlen as a variable since it is an actual function.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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 ) ;
 }
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 
#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.

Sin-da-cat
Newbie Poster
23 posts since Oct 2006
Reputation Points: 10
Solved Threads: 0
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You