Thread: I was wondering if anyone could help me with a read and write function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You really should be doing the sort and the duplicate check at the same time as you read the data in, which will be much more efficient, and also way less code.

    Since both the sort and the duplicate check are based on the name which is the first word in each string, you don't need a struct as Adak claims, but you only need one 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

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    2

    Unhappy Could you visually show that?

    Thanks for the help, I really appreciate it.


    Since I am somewhat new to programming in c, I was wondering if you could show me visually in the code exactly what you are talking about.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Wolfman View Post
    Since I am somewhat new to programming in c, I was wondering if you could show me visually in the code exactly what you are talking about.
    Oops, I forgot what a pain it is to reorder an array in C. So the BEST WAY might be to use a linked list -- which means a struct, as Adak mentioned. But if you haven't done that before, it will probaly take you at least a few hours to figure it out.

    So, you could still stick with a single array (just read in one file, then the other, and sort it as you are doing (presuming your sort works, of course). You can find the duplicates during the sort because they will be ties (eg, does Parker go before, or after Parker?)

    However, if you are doing this partially to learn C programming, I would really very strongly recommend finding a linked list tutorial that you like (there's lots of them, and also people here every day asking about them) and doing it that way. This is EXACTLY what they are for.

    Actually, since you are using pointer, it might not be so hard to sort as you go. Here's the pseudo code
    Code:
    while (read line from file)
         start at the first element, 
         and keep going until you hit one where this name should be before that one
         then move all the pointers down and insert your name
         if there is a match, print the name to the second file
    I guess that's not even pseudo-code! But you get the point.
    Last edited by MK27; 04-17-2009 at 03:20 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

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's a function that will insert a string into a pointer array:
    Code:
    void insert (char *string, char **list, int len, int pos) {
    	int i;
    	char *tmp=list[pos], *tmp2;
    	list[pos]=string;
    	for (i=pos+1; i<=len; i++) {
    		tmp2 = list[i];
    		list[i]=tmp;
    		tmp = tmp2;
    	}
    }
    where string is the item to insert, **list is the array, len is the current length of the array (not including the new item), and pos is which element to bump down. It does not allocate 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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read write lock in C#
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-16-2008, 08:49 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM