Thread: alphabetically sort textfile

  1. #16
    Registered User
    Join Date
    Nov 2012
    Posts
    38
    ok so I can get realloc work for a 1d array.

    but I'm fully confused with 2d arrays...

    this is waht I need:
    array[n][255]
    while n iterates every single time a new line is read via
    "while(fgets(puffer,LENGTH,filein))"


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <String.h>
    #define LENGTH 255
    
    int main()
    {
        int i=0;
        char **str2d = malloc(sizeof(char*));
        str2d[0] = malloc(LENGTH*sizeof(char));
        FILE *filein;
        FILE *fileout;
        char puffer[LENGTH];
        filein=fopen("namein.txt","r");
        fileout=fopen("nameout.txt", "a");
    
        fgets(puffer,LENGTH,filein);
        strcpy(str2d[0], puffer);
    
        while(fgets(puffer,LENGTH,filein))
        {
    
            //SOMEHOW REALLOCATE str2d as of str2d[i+1][255] :)
    
            strcpy(str2d[i], puffer);
            i++;
        }
    
        return 0;
    }

    could you by any chance give me a hint or pseudo code for the realloc part? :/

    can I use realloc at all? or do I need a new function in which I write a complete new array and copy paste the old one?

    if that's the case, guess it's better to use lists then no? :/

    thanks in advance!

  2. #17
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Could you use something like this, or are you totally unsure of how many rows the array should have? (can't guess within 5,000 or so?)


    It uses very little memory if you make the "count" variable, extra big - it's just a pointer. You can add extra rows up to the "count" you use,
    any time you need them - just malloc them, as shown, inside the for loop, below.


    Code:
       words=malloc(count * sizeof(char *));
       for(i=0;i<count;i++) {
          words[i]=malloc(LENGTH * sizeof(char));  
       }
    I don't know what the problems would be using realloc on a 2D array.

  3. #18
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by coffee_cup View Post
    ok so I can get realloc work for a 1d array.

    but I'm fully confused with 2d arrays...

    can I use realloc at all?
    Sure you can realloc(). Your array is basically just a 1D array of pointers. So just resize the array as usual. You have to additionally allocate memory for each pointer as soon as you need it.

    But in general it's more efficient to start with a reasonable sized array and if you need to resize it then you should use a constant factor (e.g. double it each time). See also Wikipedia: Dynamic array/Geometric expansion and amortized cost

    Bye, Andreas

  4. #19
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    As an alternative to using a 2d array, you could read in the entire file into memory, then scan memory for newlines, creating an array of pointers to the first characters for each line. If line size is variable, but always less than 255 bytes line (including new line character(s)), you could convert the lines into p strings, which is a count followed by data. In order to do this, you'll need to leave space for the first count for the first line, so read the file at some small offset such as 4 byte or 8 bytes from the start of your allocated memory, and the first count would be at offset 3 or 7.

    Then instead of sorting the data, you sort the array of pointers (using a second array if doing a merge sort). You can then write a file one line at a time (don't forget to put the newlines back), using the sorted array of pointers.
    Last edited by rcgldr; 04-14-2013 at 01:06 AM. Reason: update

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. funny output at the end of textfile after sort.
    By csharp100 in forum C Programming
    Replies: 7
    Last Post: 10-21-2012, 05:11 PM
  2. Sort alphabetically and by number
    By krakatao in forum C Programming
    Replies: 9
    Last Post: 04-12-2012, 01:15 PM
  3. Sort A String Alphabetically.
    By wantsree in forum C Programming
    Replies: 2
    Last Post: 02-05-2011, 02:14 AM
  4. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  5. How do I heap sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 7
    Last Post: 12-12-2007, 01:00 AM