#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2017
    Posts
    1
    Rep Power
    0

    Code to sort a txt file line by line by order of increasing value


    I am trying to write a code to read in a text file of data and reorder it so that the first column is in order of increasing size.
    Currently, I am trying a 2 loop structure to compare two lines, and writing the smaller of the lines to a temporary file.
    However this is not coming out right and i know that my code does not correctly run through and write the lines.
    Any help on where i am going wrong would be greatly appreciated

    Code:
    #include <string.h>
    #include<math.h>
    #include<stdio.h>
    #define MAXLINE 100
    
     int main() 
    {
           printf("enter file name: ");
           char filename[MAXLINE];
           scanf("%s", filename);
     
           FILE *newfile = fopen(filename, "r");
           FILE *tempfile = fopen("tempfilename", "w");     
     
           int ch, nlines = 0;
           while ((ch = fgetc(newfile)) != EOF)
        {
                if (ch == '\n')
            {
                     nlines++;
            }
        }
     
            float doub[nlines];
            char line[MAXLINE], linej[MAXLINE], rest[nlines][MAXLINE];
     
            rewind(newfile);
     
        for (int i=0; i<nlines; i++)
        {
            fgets(line, MAXLINE, newfile);
                   sscanf(line, "%f %s", &doub[i], rest[i]);
            rewind(newfile);
            for (int j=0; j<nlines; j++)
            {
                fgets(linej, MAXLINE, newfile);
                       sscanf(linej, "%f %s", &doub[j], rest[j]);
                if (doub[j]<doub[i])
                {
                    fputs(linej, tempfile);
                    
                }
                else
                {
                    fputs(line, tempfile);
                    j=nlines;
                }
            }
                    
        }
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,824
    Rep Power
    509
    Use qsort (found in stdlib). If you need help with a sorting algorithm investigate Search results for "sort" - Rosetta Code .
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    #define FOREVER for(;;)
    #define CHUNK 16		/* number of records to allocate at a time */
    #define MAXLINE 100
    
    #define RECORD struct record
    RECORD {
      double a;			/* field used for comparison */
      char line[MAXLINE];		/* field used for output */
    };
    
    unsigned load(FILE*inf, RECORD**r) { /* load the file parsing into records, returning the number of records */
      RECORD*pr;			     /* read the file only once at the expense of reallocating memory */
      unsigned lines, room = 0;
      for (lines = 0; ; ++lines) {
        /* make space */
        if ((room <= lines) && (NULL == (*r = realloc(*r, (room += CHUNK) * sizeof(RECORD))))) {
          fprintf(stderr, "\nunable to allocate space for %u records.\n", room);
          return 1;
        }
        /* load the record */
        if (NULL == fgets((pr = (*r)+lines)->line, MAXLINE, inf))
          break;
        else
          pr->a = atof(pr->line);
      }
      return lines;
    }
    
    void display(RECORD*r, unsigned n, FILE*ouf) { /* write the n lines onto ouf */
      for (unsigned i = 0; i < n; ++i)
        fputs(r[i].line, ouf);
    }
    
    int compare(const void*A, const void*B) {
      const RECORD*a = A, *b = B;
      return (b->a < a->a) + (b->a <= a->a) - 1;
    }
    
    int main(int ac, char*av[]) {
      FILE*inf;
      RECORD*records = NULL;	/* dynamic array of records */
      unsigned lines;
      if (ac < 2) {
        fprintf(stderr, "\nUse: %s file\n", *av);
        exit(1);
      }
      if (NULL == (inf = fopen(av[1], "r"))) {
        fprintf(stderr, "\ncannot open %s\n", av[1]);
        exit(1);
      }
      lines = load(inf, &records);
      qsort(records, lines, sizeof(RECORD), compare);
      display(records, lines, stdout);
      free(records);
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo