944,117 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6675
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 20th, 2006
0

Is it possible to arange lines in a file alphabetically?

Expand Post »
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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 20th, 2006
0

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

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 20th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by Lerner ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 20th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by Sin-da-cat ...
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) .

Quote ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 20th, 2006
0

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

>>but what is a container?

an array, std::vector, std::list, etc.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 20th, 2006
0

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

<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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 20th, 2006
0

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

Here comes another stupid question: how do you use qsort?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 20th, 2006
0

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

For a solved eg. see here.

For documentation look here.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 20th, 2006
0

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

  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Sin-da-cat is offline Offline
23 posts
since Oct 2006
Nov 20th, 2006
0

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

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: pthread
Next Thread in C Forum Timeline: Visual Studio Team System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC