separating mixed list into sorted two lists

This is a discussion on separating mixed list into sorted two lists within the C Programming forums, part of the General Programming Boards category; i have a file which lists people. each person is either type 0 or type 1 people and each person ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    separating mixed list into sorted two lists

    i have a file which lists people.
    each person is either
    type 0 or type 1 people
    and each person has an ID.

    each line in the file represents a person.

    the file is not sorted in any way not by id nor by type.

    how to take the data from that file
    so i will create two files sorted by ID and each file has people with the same type
    ??

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Its easy, read the list from the file and store them in a struct array. Sort the structure array with the ID and write them onto the file and then sort the structure array with the other attribute and write them onto a different file.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am not allowed to rewind.
    only to read the file once

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They didn't mention using rewind. Seriously, you need to start putting some thought into your problems instead of just posting them here for us to do them all for you. We're not here to teach you basic logic, or to do your homework.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am not allowed to copy the whole file in to a struct .

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    define rewind.

    ssharish is saying that you read the file into an array in memory, sort the array, and write out the elements to two separate files.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am looking for some way that as i go by each line
    then it sorts in the write place in it designated file.

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Ohh well, show us how your file looks like?

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i am not allowed to read the whole file into a data structure.

  10. #10
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by transgalactic2 View Post
    i am looking for some way that as i go by each line
    then it sorts in the write place in it designated file.
    Well, you cant do that. You can at the most do one task at a time. Not reading and writing at the same time. The file will be locked to do anything else.

    Well, not excatly you can. But position the file pointer is the biug problem.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    each line in that file represents a person
    in each line i have ID 9 chars
    and type (0 or 1) 1 char

    and i have a list of many people
    mixed up

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by ssharish2005 View Post
    Well, you cant do that. You can at the most do one task at a time. Not reading and writing at the same time. The file will be locked to do anything else.

    Well, not excatly you can. But position the file pointer is the biug problem.

    -ssharish
    i ment writing the line from the mixed file
    into the sorted file which it belongs to by its type.

  13. #13
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Fine! have you written any code so far? Can you show us what you have got so far? Have you tried reading a file?

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    yes i have built a helping function which gets an ID
    and finds the smallest ID amongst the other which is bigger then the intput
    for example:
    if the input is 4 and the list is
    7
    8
    6
    then it returns 6

    this is the whole code
    including the start of the function i want to build
    void balance(FILE * Room1, FILE * Room2, char* course1,char* course2);

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    
     
    struct line {
    	char name[21], address[21];
    	int grade, ID;
    	int code;
    };
    
    void balance(FILE * Room1, FILE * Room2, char* course1,char* course2);
    struct line next_Id(FILE * room , int last_ID);
    int main ()
    {
    FILE* c[2];
     struct line  temp;
    
    c[0]=fopen("c:\\fl1.txt","r");//vote files ,each file represents the votes from a college
    c[1]=fopen("c:\\fl2.txt","r");
    temp=next_Id(c[0] , 666666666);
    
    fclose(c[0]);
    fclose(c[1]);
    
    
    
    	return 0;
    }
    struct line next_Id(FILE * room , int last_ID)
    {
        struct line  temp,min,tmin;
    	int flag;
    
    	do
    	{
    		flag=fscanf(room,"%10s%10s%10d%10d%10d%*c",&temp.name,&temp.address,&temp.ID,&temp.grade,&temp.code);
         
    	}while(temp.ID<last_ID);
    		do
    		{
               flag=fscanf(room,"%10s%10s%10d%10d%10d%*c",&min.name,&min.address,&min.ID,&min.grade,&min.code);
                 
    		   if ((min.ID>last_ID)&&(min.ID<temp.ID)&&(flag==5))
    			 {
                     tmin=min;
    			 }
    		   else
    		   {
    			   if(flag==5)
    			   tmin=temp;
    		   }
    		    
    		}while (flag==5);
    		return tmin;
    }
     
    void balance(FILE * Room1, FILE * Room2, char* course1,char* course2)
    {
          struct line  temp[2];
          int flag[2];
          
           temp[0]=next_Id(Room1,000000000);
    	  flag[0]=fscanf(Room1,"%10s%10s%10d%10d%10d%*c",&temp[0].name,&temp[0].address,&temp[0].ID,&temp[0].grade,&temp[0].code);
          //flag[1]=fscanf(Room2,"%10s%10s%10d%10d%10d%*c",&temp[1].name,&temp[1].address,&temp[1].ID,&temp[1].grade,&temp[1].code);
    	  while()
    	  {
    
    	  }
          
    }
    Last edited by transgalactic2; 04-14-2009 at 04:57 PM.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while read line from file doesn't produce EOF
        append to odd ? odd file : even
    sort odd file
    sort even file
    There.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merging two lists as one
    By Lone in forum C++ Programming
    Replies: 1
    Last Post: 03-17-2005, 02:59 PM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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