Thread: Alphabetizing a file?

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    25

    Alphabetizing a file?

    Another quick thing I need some assistance with is alphabetizing a file created from the program. So, the program will create a file like:

    Namath Joe
    Lugo Julio
    Favre Brett
    Jordan Michael

    And I need to put the file in terms order of last name:

    Favre Brett
    Jordan Michael
    Lugo Julio
    Namath Joe

    I can only store one line of data at a time though in the program.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    So store each name in memory and then compare them and reorder them.

    There are many ways of sorting, including some sorting algorithms already implemented in C. All you would have to do is write a function to compare one string with another, and that's even done for you, too.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by TitoMB345 View Post
    I can only store one line of data at a time though in the program.
    You will have to change that because holding more than one line in memory at a time is the ONLY way to do it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by iMalc View Post
    You will have to change that because holding more than one line in memory at a time is the ONLY way to do it.
    Couldn't he just store the first sentence in the file in memory and then look at the first letter in the next to determine if it has to move or stay?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Neo1 View Post
    Couldn't he just store the first sentence in the file in memory and then look at the first letter in the next to determine if it has to move or stay?
    Yeah, you could STORE one line, and read in partials of the next line. However, unless it's GUARANTEED that all names start with different letters, you can't just read one letter. You also have to read the file multiple times to eventually come out with the lines in the right order, because the first thing you read may well be the LAST thing to output, and you'd have to have some sort of indication of which lines have already been processed [opening the same file twice with different FILE pointers MAY work - but some environments require special tricks to open the same file twice].

    Consider:
    Zzz
    Aab
    Aaa

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    Yeah, I still havent been able to find a way to do this. It seems nearly impossible without the ability to store all the data in the program, it would be easy if this were possible. But I am not allowed. I am completely stuck.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by iMalc View Post
    You will have to change that because holding more than one line in memory at a time is the ONLY way to do it.
    Nah. He could just use merge sort, using temporary files. That's how big tape-drive machines used to sort things without needing memory.

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    25
    So, reading through my C text, it seems somehow I will have to use fseek(). I think my original approach to the problem was incorrect. I am going to start over and try another way.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by TitoMB345 View Post
    So, reading through my C text, it seems somehow I will have to use fseek(). I think my original approach to the problem was incorrect. I am going to start over and try another way.
    You should not have to seek. In fact, I don't see how you could, since this data is line-based and lines can be of different lengths.

    I still suggest using a file-based merge sort. It's not hard. Same as any other merge sort, except that the spot in the algorithm where you allocate memory, you just open a file instead.

    EDIT: And with a small adjustment to account for the fact you don't know how much data you have.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    Quote Originally Posted by brewbuck View Post
    You should not have to seek. In fact, I don't see how you could, since this data is line-based and lines can be of different lengths.
    u can seek in the file with variable string lengths..... just use ftell() and store the position before and after seeking and store them in a long int..... string compare and rewrite......

    a part of u r code would be......
    Code:
    c  =  ''  ;/*nocharacter*/
    
    while(  ! feof (  file_poin  )  )
    {
    
                 first_word_start  =   ftell ( file_poin ) ; 
                 
                 fgets ( first_name , file_poin ) ;   /*first name*/
                 
                 first_word_end   =   ftell( file_poin  )  ;
    
                 fgets ( second_name , file_poin ) ;   /*second name*/
    
                 second_word_end = ftell(  file_poin  ) ;
                 
                  if (    strcmp   ( first_name , second_name )   >  0  )
    
                                      {
    
                                           fseek(  file_poin , first_word_start  ,  0   )  ;  
    
                                           fputs (  first_name , file_poin );
    
                                                           while(   ftell (  file_poin  )  !=  first_word_end  ) 
                                                              {
                                                                   putc( c , file_poin );
                                                               }            
                                                              
                                            fputs ( second_name , file_poin ) ;  
    
                                                            while(   ftell  ( file_poin  )  !=  second_word_end )  
                                                               {
                                                                   putc( c , file_poin );
                                                               }  
    
                                           rewind( file_poin );
                                       }
    }

    as only swapping is taking place there wont be extra spaces or alphabets after each swap
    Last edited by ElemenT.usha; 11-07-2007 at 03:28 PM. Reason: replacing one char variables with understanable ones

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    gets() is a bad idea. (Besides, gets() reads from stdin, and I think you want to read from p. Consider fgets().) You're missing a semicolon. Your indentation is non-existent. You're using while(!foef()). All of your variable names are an unreadable one character long. Oops.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    73
    Quote Originally Posted by dwks View Post
    gets() is a bad idea. (Besides, gets() reads from stdin, and I think you want to read from p. Consider fgets().) You're missing a semicolon. Your indentation is non-existent. You're using while(!foef()). All of your variable names are an unreadable one character long. Oops.
    ya code has been changed......sorry for inconvenience
    im used to one characters...... and indenting is way toooo far.......

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, well, there's no need to use quite that much whitespace. Too much is as bad as too little. Well, maybe not quite as bad.

    Oh, one other thing: passing fseek() 0 for the whence parameter is a little unportable, not to mention difficult to understand. SEEK_SET is much easier to understand than 0, don't you think?

    im used to one characters...... and indenting is way toooo far.......
    You should get out of the habit of using one-character names, then. And I'm not surprised that you think that's too far. I think it's too far, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    73

    Smile

    Quote Originally Posted by dwks View Post
    Yeah, well, there's no need to use quite that much whitespace. Too much is as bad as too little. Well, maybe not quite as bad.

    Oh, one other thing: passing fseek() 0 for the whence parameter is a little unportable, not to mention difficult to understand. SEEK_SET is much easier to understand than 0, don't you think?


    You should get out of the habit of using one-character names, then. And I'm not surprised that you think that's too far. I think it's too far, too.
    yes SEEK_SET or SEEK_BEG would be better.....
    but i dont think 0 should be a problem.....
    thanxxx for the suggestion.....


    and............


    is your name DWK????

  15. #15
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    For fseek(), one should use the ANSI-standard SEEK_SET, not SEEK_BEG, which as far as I can tell is for seek().

    On most compilers, 0 wouldn't be a problem. But I'm not sure if SEEK_SET is guaranteed to be 0 or not. It probably is -- I've seen lots of code like yours.

    No, my name isn't DWK. Is yours "ElemenT.usha"?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM