Thread: Sorting text file problem...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    2

    Question Sorting text file problem...

    Hey,
    I am very new to C & only started a few days ago. I have read a few tutorials & have got to the point where I can do loops, pointers etc.

    But I have come across something that I want to create a program to do, but I don't yet have the knowledge to do it. So I want to seek your help.

    I need a program that can sort a text file of about 3,000 phone numbers, the entries look like this:

    Name: Mark Phone: 02-0121-234353
    Country State Surname
    Blah Blah Blah

    Name: Lee Phone: 02-0121-2234453
    Country State Surname
    Blah Blah Blah

    Name: Chris Phone: 02-0121-657353
    Country State Surname
    Blah Blah Blah

    Like I said this goes on for about 3,000 entries , so what I want this program to do is extract the phone numbers & just the phone numbers & put them into another text file in order so it would look like this:
    02-0121-234353
    02-0121-2234453
    02-0121-657353

    I am not asking for you to write this program for me but just to point me in the right direction on where should I go to learn to do this? Also, what functions could probably help me in doing this?

    Thank you,
    John

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's time you learn about pesudocode!

    Code:
    open input file
    open output file
    while not at the end of the input file
        read one record
        read phone number from record
        write phone number to output file
    close input file
    close output file
    Break each step you need help with into smaller steps.

    Code:
    read one record( )
    {
        while not at end of file and not at end of record
            read a line into a buffer
            do something with it
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    137
    How are the data entries stored? As arrays, stuctures etc. We'd need to know how the data is stored before it can be accesed. Can you give an example of the code?
    http://uk.geocities.com/ca_chorltonkids

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, you might need some of the following:

    - fopen() to open the file
    - fgets() to a get a line of data
    - strncmp() to compare strings
    - strchr() to find a character within a string
    - fclose() to close the file

    You'll need to decide how you are going to hold the data in memory. You could choose an array, a list or binary tree. The numbers in a binary tree would be sorted by default, the same for a sorted link list. An array can be sorted using qsort().

    [edit]Doh! Beaten twice, crikey I'm slow tonight
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Text File (spaces problem)
    By kekewong in forum C Programming
    Replies: 1
    Last Post: 04-15-2009, 03:34 PM
  2. read from file problem
    By Martin Kovac in forum C Programming
    Replies: 1
    Last Post: 04-13-2009, 08:33 AM
  3. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 03:50 PM
  4. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 03:17 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM