Thread: beginners help

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    14

    Question beginners help

    how do i go about sorting a file in order. The file will contain three different sized structures. The sort will be of the second field, the first denoting which type of structure the record is. The question says to "define the three structures, use a standard sort utillity and is quite straightforward." I have done a sort of an array, comparing elements and if need be swap them. Do i do this with a file, if so why does the question say to output to a new "sorted file"?

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Didn't you post the exact same question the other day?
    As I told you use qsort() to sort. If they are strings you can compare them lexicographically with strcmp() or strncmp()
    For sorting your file I would expect you to read the data in to memory and make the required changes then rewrite it to disk again.
    You have had a couple of days so where is your attempt at this?
    Lets see some code if you want real help!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    HI another compu-teach-yourself student, it looks like your at the same stage as i am (((i think))))this may be what your looking for
    as you will see there are a few of us here(jebus save us!)
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    Stage 4. Program 2 "straightforward" ? It would be if you have been on the 2 day course to teach the solutions to this little teaser. Got thru to a tutor a last, said the same as you have, and i should recieve my invite shortly???? Thx for the pointers it'll give me something to read upon

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi, you really should have attended dudley before now. Program 2 is hard to get to grips with if you haven't done the prep course.

    here goes....

    open files - validated file, new sorted file and printer.
    read val file to get number of records on file
    use malloc to allocate memory for number of records
    sort records now in memory in cust code order then
    sort records again so create record not followed by any other record
    write sorted records to sorted file
    close sorted file and re-open to read mode
    read sorted file and send to printer
    close all files.

    if your 1st program produced a text file to get number of records on file you will have to read all records to get number of records.

    if your 1st program produced a binary file use fseek(); and ftell(); to get number of records.

    I havenot told you anything here you would not have got down at dudley.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  6. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    14
    Cheers, been 5 weeks since i sent a draft of prog 1. Still no date "your work is being actioned" At least i can give it a try. Give me a rest from prog 3? Thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exercises for Beginners!
    By alokrsx in forum C++ Programming
    Replies: 11
    Last Post: 10-02-2006, 11:59 AM
  2. Simple Console Input for Beginners
    By jlou in forum C++ Programming
    Replies: 0
    Last Post: 06-21-2005, 01:50 PM
  3. for beginner's or anyone interested
    By Iconoklast in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-25-2004, 02:45 PM
  4. Petzold's Book Good For Beginners?
    By drdroid in forum C++ Programming
    Replies: 7
    Last Post: 10-12-2002, 01:35 PM
  5. What is a good beginners' C++ book?
    By GrNxxDaY in forum C++ Programming
    Replies: 1
    Last Post: 07-29-2002, 09:50 AM