Thread: file sorting strategy question..

  1. #1
    Banned
    Join Date
    Jul 2009
    Posts
    46

    file sorting strategy question..

    i have two person files which are sorted by ID
    one is old and the second is new(the updated person list)

    i need to create 2 files:
    the first is all the new persons which do not appear in the old fle
    sorted by id

    and the second file is the same as file one but sorted by the name of that person.
    i am not allowed to use rewind.

    i know how to create the first file
    i just compare the lines and put the smaller id's of the new file .
    and it will make a sorted by id file.

    but how to sort the new people by name.

    i need to do some how a paralel proccess

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Put the whole file into an array:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LLEN 256
    
    char **readfile(char *file, int *i) {
    	FILE *fh = fopen(file,"r");
    	char **lines = malloc(sizeof(char*));
    	*i = 0;
    	lines[0] = malloc(LLEN);
    	while (fgets(lines[*i],LLEN,fh)) {
    		(*i)++;
    		lines = realloc(lines,((*i)+1)*sizeof(char*));
    		lines[*i] = malloc(LLEN);
    	}
    	fclose(fh);
    	return lines;
    }
    	
    
    int main(int argc, char *argv[]) {
    	int len, i;
    	char **lines;
    	if (!argv[1]) puts("No file!");
    	lines=readfile(argv[1],&len);
    	for (i=0;i<len;i++) printf("%s",lines[i]);
    	return 0;
    }
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Jul 2009
    Posts
    46
    i am not allowed to do that
    i only can copy one line at a time
    from each file
    and i cant do rewind

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cfan View Post
    i am not allowed to do that
    i only can copy one line at a time
    from each file
    and i cant do rewind
    If you cannot read each file more than once, and you cannot store more than one line, you cannot compare that file to another one in the way you describe. Period.

    You have misunderstood something about your assignment.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If each entry within both arrays maintain a unique user ID, then you should be good. As that should be the key that you will base your sorting and collecting logic of names that are not contained in the first file. If they are unique, user IDs, then combining the files into one array will do the trick as MK27 mentioned.

  6. #6
    Banned
    Join Date
    Jul 2009
    Posts
    46
    the general way which i am allowed to solve such problems is:
    having a big while loop which goes till EOF
    and in every iteration we read some part of a file to local variables

    and to the desired comparing operation

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by slingerland3g View Post
    If each entry within both arrays maintain a unique user ID
    The OP claimed s/he is not allowed to use an array and may only work with one line at a time.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Banned
    Join Date
    Jul 2009
    Posts
    46
    yes
    for example if i want to check if the current line is a new person

    then i compare it with the old file current line
    if the old id is bigger then its a new person
    if its equal then its not a new person

    if the new id line is bigger
    then i do fscanf of the old file over and over till i get a line wich id is equal
    or bigger
    or eof
    if i get eof or bigger id line then its a new person
    else its not

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cfan View Post
    then i do fscanf of the old file over and over till i get a line wich id is equal
    You are not allowed to rewind, or store the file in memory, but you can read the file over and over again with fscanf?

    Actually, you cannot read the file over and over again with fscanf unless you use rewind, or open a new handle on the file. Over and over again.

    I really think you should make sure that you cannot read the file once, and store the entire file in memory -- either in an array, or a single string. Otherwise, what you want is simply not possible*, which is why I believe you have misinterpreted something about the assignment.

    *unless you can re-open a file, but since you are not allowed to rewind, this seems unlikely.
    Last edited by MK27; 07-30-2009 at 01:44 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Banned
    Join Date
    Jul 2009
    Posts
    46
    i am allowed to read the files line by line and store them in a temporary variable


    i am not allowed to copy the whole file
    i am not allowed to use rewind
    i showed the general way of solution which i am being expected

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cfan View Post
    i am allowed to read the files line by line and store them in a temporary variable


    i am not allowed to copy the whole file
    i am not allowed to use rewind
    i showed the general way of solution which i am being expected
    Okay, as long as you understand that it is not possible to "fscanf" through a file over and over again without using rewind. Try it yourself.

    Also, consider there is a difference between "copying a file" (making another copy of it on disk) and "storing an entire file in memory". So being told you cannot copy the file DOES NOT MEAN YOU CAN'T PUT IT IN AN ARRAY.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Banned
    Join Date
    Jul 2009
    Posts
    46
    i dont need to copy the whole file at once (and i am not allowed to)
    the lines in files are sorted.

    do you understand the general strategy that i have being presented
    (and which i am asked to do a solution in that manner)

    ??
    Last edited by cfan; 07-30-2009 at 02:28 PM.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cfan View Post
    do you understand the general strategy that i have being presented
    (and which i am asked to do a solution in that manner)

    ??a
    If it is the way you describe, it is not possible, so it cannot be the way you describe:

    Quote Originally Posted by cfan View Post
    if the new id line is bigger
    then i do fscanf of the old file over and over till i get a line wich id is equal
    or bigger
    or eof
    That will mean you have to read the file more than once. fscanf picks up where the file pointer has read to so far. You cannot rewind fscanf to the beginning of the file without using rewind() on the file pointer. Or, you open the file again. If you are allowed to keep fopen()ing the file over and over again, that's fine.

    I'm just warning you that is probably not what is meant. You could read the *entire* file into one string, of course. That may work for what you want, since all you have to do is check if a name was in the other file. But that means you will be saving the entire file into memory.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by cfan View Post
    i have two person files which are sorted by ID
    one is old and the second is new(the updated person list)

    i need to create 2 files:
    the first is all the new persons which do not appear in the old fle
    sorted by id

    and the second file is the same as file one but sorted by the name of that person.
    i am not allowed to use rewind.

    i know how to create the first file
    i just compare the lines and put the smaller id's of the new file .
    and it will make a sorted by id file.

    but how to sort the new people by name.

    i need to do some how a paralel proccess
    Somehow, you have to make some memory of the names in the file - or loop back through the file.

    You don't *have* to use rewind. You could explicitly re-position the file pointer in binary file mode, for instance.

    Or you could use some memory device (array, linked list, auxilliary file, etc.), for this.

    A motorcycle must have two wheels - no matter how badly we want it to use just one, all the time.

    When a teacher gives out an assignment like this, they nearly always give you a hint of how to get started - perhaps in your notes, in lecture, in hand outs - something of a hint.

    If this is from a book, the author nearly always puts the assignments that relate to that chapters topics, so there's a hint.

    You need to find that hint.

  15. #15
    Banned
    Join Date
    Jul 2009
    Posts
    46
    so what problem do you see in this general way

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. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM