Help with sorting a text file database

This is a discussion on Help with sorting a text file database within the C Programming forums, part of the General Programming Boards category; Hi, I've tried searching several resources for info on this but I still don't quite understand what I need to ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    Help with sorting a text file database

    Hi, I've tried searching several resources for info on this but I still don't quite understand what I need to do here. I'm new to C programming so please keep that in mind.

    Basically what I want to do is read from a text file containing X number of lines, each with 3 elements where the characters are a 'name' for the ints, for example:

    abc 123 45678
    sdf 32002 12323
    xcvssd 23 2234
    adssp 324 1023

    I then need to sort the data by int first and name second, and print it out in 2 tables

    Field 1:
    23 xcvssd
    123 abc
    324 adssp
    32002 sdf

    Field 2:
    1023 adssp
    2234 xcvssd
    12323 sdf
    45678 abc

    I think what I need to do is create a struct, something like:

    Code:
    struct entry {
    	str field1[20];   /* Field lengths of 20 */   
    	int field2[20];
    	int field2[20];
    } database;		/* Creates database variable of type entry*/
    From there, I am confused as to how to sort the data. My thoughts are to to make a bubble sort function that will take in each element of the struct in one at a time and sort it. Should I just write over the sorted array/struct, or store it in a new array? Can I sort the entire struct at once?

    I can't find any good examples of passing struct array element things into another function. Do I need to use pointers for this?


    Does this sound like the right idea, or am I going about it all wrong? I'm not concerned about sorting speed or anything, I just want it to work for now.

    Thanks!

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,074
    If you're going to use a struct, then consider making the members single datatypes and creating an array of objects. The way your doing it kind of defeats the purpose of the object. Of course your character still must be an array. Then all you need to do is make your sort routine comparing the int members, then swap the whole objects.
    Sent from my iPadŽ

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by bds824
    Basically what I want to do is read from a text file containing X number of lines, each with 3 elements where the characters are a 'name' for the ints, for example:
    First, get a million dollars...
    Quote Originally Posted by Steve Martin
    You.. can be a millionaire.. and never pay taxes! You can be a millionaire.. and never pay taxes! You say.. "Steve.. how can I be a millionaire.. and never pay taxes?" First.. get a million dollars.
    Or rather, first find out the number of lines in a file. This must have been Googled several hundreds of thousands of times (finding the number of lines in a file), so searching ought to reveal more than one hit -- albeit probably 2/3 of which will contain questionable advice. [If you go line by line, life will probably suck a little bit more.]

    But yes, you dynamically create an array of structs of however many lines of data you've got. Then you sort the data. And the C standard library supplies the function qsort -- all you need to do is tell qsort how to compare things -- which means you need to write a comparison function for it.

    Anyways, this post does not provide a solution. It is merely here with a couple of key phrases mentioned for you to begin searching for one. This is not an attempt to be conceited, merely to help you learn to learn how to find the answer.
    Last edited by Dave_Sinkula; 02-13-2006 at 10:31 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    just use a hash table and any of the sorting algorithmns.

    The hash table can easily be implemented using your structure,
    get a function to scan each of the lines and store them in your struct array,then if u want to be sorting wrt the second element of each structure,just use the sortin algorithmn on the 2nd element of each structure,get them sorted and then once u got them sorted,print out the remaining elements of their corresponding structures in the same sequence.

    Code:
    typedef sturct {
               char  field1[20];
                char field2[2];
                char field3[20];
             }var;
    
    
    
    assign(var *);
    
    sort(var *);
    u can use fscanf,just take in teh data as strings. Even "123" comes as a string,it has an int value tho.

    if u want to be using the dynamic allocation approach and u dont want to declare a struct array of like 200 elements,u can use a linked list ,do u know how to do that?
    Last edited by qqqqxxxx; 02-14-2006 at 12:18 AM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    assign(var *);
    Is that a prototype of some kind?

    Take Dave's suggestion and search the board and the internet.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    dont tell me u cant make out what that meant,did u expect me to write the whole program for him,was just guiding him in the right direction.

    assign(var *)


    assign is a function that takes a structure pointer as its arguement .


    And a problem as simple as that,u dont have to waste time searchin the internet for code that u will never get,try doing the program,thats hardly about 10 lines in code.

    __________________________________________________ __
    "i dont like relational databasaes,i like object oriented databases"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 04:53 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Function is called and I am trying to open a file
    By tommy69 in forum C Programming
    Replies: 88
    Last Post: 05-06-2004, 09:33 AM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21