CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 1999
    Posts
    13

    how to sort array in this code

    date.txt file contains:
    0001688239=11:02:40:12/12/1999
    1163533198=03:20:01:02/07/1963
    0728661599=10:00:00:11/23/1976
    0583230589=15:30:10:07/04/1981
    0133185599=12:00:00:10/11/1995

    append.c

    // append.c appends files to a file
    //User enters this on Run line: append.c <file1> <app2file>
    // User enters append date.txt sortdate to have text from date.txt appended to sortdate.txt file
    // I WANT THE LINES IN DATE.TXT SORTED SO THEY ARE IN CHRONOLOGICAL ORDER when placed in the output file.
    //HOW DO I DO THIS????
    //I will offer a $25 Amazon.com gift cert. to the first person to write this code for me. I’ve run out of time.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define BUFSIZE 1024
    char temp[BUFSIZE];
    void append(FILE *source, FILE *dest);

    int main(int argc, char *argv[])
    {
    FILE *fa, *fr;
    int file;

    if (argc < 3)
    {
    fprintf(stderr, "Usage: %s source-file(s) destination-file\n", argv[0]);
    exit(1);
    }
    if ((fa = fopen(argv[argc - 1], "a")) == NULL)
    {
    fprintf(stderr, "Can't open %s]n", argv[argc -1]);
    exit(2);
    }
    if (setvbuf(fa, NULL, _IOFBF, BUFSIZE) !=0)
    {
    fputs("Can't create output buffer\n", stderr);
    exit(3);
    }
    for (file = 1; file < argc - 1; file++)
    {
    if (strcmp(argv[argc -1], argv[file]) == 0)
    fputs("Can't append file to itself\n", stderr);
    else if ((fr = fopen(argv[file], "r")) == NULL)
    fprintf(stderr, "Can't open %s\n", argv[file]);
    else
    {
    if (setvbuf(fr, NULL, _IOFBF, BUFSIZE) !=0)
    {
    fputs("Can't create output buffer]n", stderr);
    continue;
    }
    append(fr, fa);
    if (ferror(fr) !=0)
    fprintf(stderr, "Error in reading file %s.\n", argv[file]);
    if (ferror(fa) !=0)
    fprintf(stderr, "Error in writing file %s.\n", argv[argc -1]);
    fclose(fr);
    }
    }
    fclose(fa);
    return 0;
    }

    void append(FILE *source, FILE *dest)
    {
    size_t bytes;
    extern char temp[]; // use the external temp array

    while ((bytes = fread(temp, sizeof(char), BUFSIZE,source)) > 0)
    fwrite(temp, sizeof (char), bytes, dest);
    }






  2. #2
    Guest

    Re: how to sort array in this code

    Hi Jody,

    I prepared a little listing for you. (Borland C++ 2.0).
    It's a function to sort your file.I added no error handling.
    Surely there is a better way to do thisjob. Maybe you will
    improve it.

    If it's possible store the data as:
    YYYY:MMD:HH:MM:SS=0001688239

    So you can use the DOS command "SORT".

    Good Luck !!!
    Peter


    void sortfile(char *filename)
    {
    FILE *f;
    typedef struct{
    char s[ROW_LENGTH];
    char t[TIME_LENGTH];
    } rowtype;
    rowtype row[100];
    int rowcount=0;
    int x,y;
    char h[ROW_LENGTH];

    if ((f=fopen(filename,"rt"))==0)
    {
    fputs("Can't open file.",stderr);
    return;
    }

    while(!feof(f))
    {
    //read string from file
    fscanf(f,"%s",row[rowcount].s);
    if(strlen(row[rowcount].s)<30) break;

    //make a temporary string for sorting
    //year
    row[rowcount].t[ 0]=row[rowcount].s[26];
    row[rowcount].t[ 1]=row[rowcount].s[27];
    row[rowcount].t[ 2]=row[rowcount].s[28];
    row[rowcount].t[ 3]=row[rowcount].s[29];
    //month
    row[rowcount].t[ 4]=row[rowcount].s[20];
    row[rowcount].t[ 5]=row[rowcount].s[21];
    //day
    row[rowcount].t[ 6]=row[rowcount].s[23];
    row[rowcount].t[ 7]=row[rowcount].s[24];
    //hour
    row[rowcount].t[ 8]=row[rowcount].s[11];
    row[rowcount].t[ 9]=row[rowcount].s[12];
    //minute
    row[rowcount].t[10]=row[rowcount].s[14];
    row[rowcount].t[11]=row[rowcount].s[15];
    //second
    row[rowcount].t[12]=row[rowcount].s[17];
    row[rowcount].t[13]=row[rowcount].s[18];
    //End of string
    row[rowcount].t[14]='\0';

    ++rowcount;
    }
    fclose(f);

    //sort the array
    for(x=0;x<rowcount;++x)
    for(y=0;y<rowcount;++y)
    {
    if(strcmp(row[x].t,row[y].t)<0)
    {
    strcpy(h,row[x].s);
    strcpy(row[x].s,row[y].s);
    strcpy(row[y].s,h);

    strcpy(h,row[x].t);
    strcpy(row[x].t,row[y].t);
    strcpy(row[y].t,h);
    }

    }

    //output in file
    if((f=fopen(filename,"wt"))==0)
    {
    fputs("Can't open temporary file.",stderr);
    return;
    }

    for(x=0;x<rowcount;++x)
    {
    fputs(row[x].s,f);
    if(x<rowcount-1) fputs("\n",f);
    }
    fclose(f);
    }


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured