Is it possible to arange lines in a file alphabetically?

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

Is it possible to arange lines in a file alphabetically?

 
0
  #1
Nov 20th, 2006
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:

  1. Mike
  2. Carl
  3. John
  4. Babe
  5. Ferrari

the new one should be

  1. Babe
  2. Carl
  3. Ferrari
  4. John
  5. 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:
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: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

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

 
0
  #2
Nov 20th, 2006
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.
Last edited by Lerner; Nov 20th, 2006 at 12:20 pm.
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
  #3
Nov 20th, 2006
Originally Posted by Lerner View Post
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.
Last edited by Sin-da-cat; Nov 20th, 2006 at 12:32 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,600
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: 462
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
  #4
Nov 20th, 2006
Originally Posted by Sin-da-cat View Post
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 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.
Last edited by ~s.o.s~; Nov 20th, 2006 at 12:33 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,339
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #5
Nov 20th, 2006
>>but what is a container?

an array, std::vector, std::list, etc.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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
  #6
Nov 20th, 2006
<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.
Last edited by Ancient Dragon; Nov 20th, 2006 at 3:33 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: 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
  #7
Nov 20th, 2006
Here comes another stupid question: how do you use qsort?
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,600
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: 462
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
  #8
Nov 20th, 2006
For a solved eg. see here.

For documentation look here.
I don't accept change; I don't deserve to live.
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
  #9
Nov 20th, 2006
  1. main(){
  2. FILE *a,*b;
  3. char s[100];
  4. int i=0,k;
  5. char starr[50][101]; /*50 - the number or strings I wanna put in the array*/
  6. a=fopen("C:\\C_fajlovi\\non_sorted.txt","r");
  7. b=fopen("C:\\C_fajlovi\\sorted.txt","w"); /*made a new file, want to keep the previous one too*/
  8. while(fscanf(a,"%s",&s)!=EOF){
  9. starr[i]=s;
  10. i++;
  11. }
  12.  
  13. ...

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?
Last edited by Sin-da-cat; Nov 20th, 2006 at 1:43 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,600
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: 462
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
  #10
Nov 20th, 2006
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.
I don't accept change; I don't deserve to live.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC