By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

sorting dates

P: 25
how do i sort dates in c.
i have dates in a file in the following format:

dd/mm/yyyy
ie 12 aug 2006

i want to sort the dates in the file in an assending way.

how can i do this?
Sep 19 '06 #1
Share this Question
Share on Google+
7 Replies


P: 61
Read the dates one by one and parse the line to get each field of the line in separate strings and then convert them to integers and keep on storing in an a two dimensional array and then apply the sort mechanism.
Sep 19 '06 #2

P: 25
how can i do this..

i am just a beginner in c.

not sure how should i do it
Sep 19 '06 #3

P: 61
Go through the following APIs in C.....

fopen
fclose
fgetc
fputc
fscanf
fprintf

By using these functions you can retrieve the characters, words and even lines from a file.

You have to know the how to parse a string and your string contains the character / as the delimeter. So you will get three different strings out of this.

Convert the strings to integers by using the function atoi().

And proceed.
Sep 19 '06 #4

P: 25
can u give me an example;

say that the dates are:
12 aug 2006
13 aug 2006
11 aug 2006
09 aug 2006
Sep 19 '06 #5

P: 61
In the first post you siad that you have the data in the dd/mm/yyyy format. Exacly in what format the dates are there in the file. And in the next 6 hrs you will get the code.
Sep 19 '06 #6

P: 25
the date is in the following format;

date month year
ie: 12 feb 2006
Sep 19 '06 #7

P: 61
Actually all the filenames are hard coded. So make a file date.txt with the following data in the directory where the program is....

24 may 2010
12 aug 2007
12 aug 2006
15 aug 2006
07 aug 2006
12 aug 2005
24 may 2003


Before running the code below take the backup of the above file.


#include<stdio.h>
#include<string.h>

void parse_date(char *, int *, char *, int *, int *);
void sort_date(int, char *, int, int);

int main()
{
FILE *fp;
char ch, dstr[20], month[5];
int i=0, dd, mm, yy, j;

fp=fopen("date.txt", "r");

while((ch=fgetc(fp)) != EOF)
{
if(ch != '\n')
{
dstr[i++]=ch;
continue;
}

dstr[i]='\0';

parse_date(dstr, &dd, month, &mm, &yy);

sort_date(dd, month, mm, yy);

i=0;
}

fclose(fp);

remove("date.txt");
rename("date1.txt", "date.txt");

return 0;
}




void parse_date(char *dstr, int *dd, char *month, int *mm, int *yy)
{
char temp[5], mon[12][5]={"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec"};
int i, j;

j=0;
i=0;

while(dstr[i] != ' ') /* This loop will give the day */
temp[j++]=dstr[i++];

temp[j]='\0';

*dd=atoi(temp);

j=0;
i++;
while(dstr[i] != ' ') /* This loop will give the month */
month[j++]=dstr[i++];

month[j]='\0';

for(j=0; j<12; j++)
{
if(strcasecmp(month, mon[j]) == 0)
{
*mm=j+1;
break;
}
}

j=0;
i++;

while(dstr[i] != '\0') /* This loop will give the year */
temp[j++]=dstr[i++];

temp[j]='\0';

*yy=atoi(temp);
}




void sort_date(int dd, char *month, int mm, int yy)
{
static int flag1;
FILE *fp, *ft;
char ch, dstr[20], month1[5];
int i=0, flag2, dd1, mm1, yy1;

if(flag1 == 0)
{
fp=fopen("date1.txt", "w");
fprintf(fp, "%d %s %d\n", dd, month, yy);
fclose(fp);
flag1=1;
}
else
{
flag2=0;
fp=fopen("date1.txt", "r");
ft=fopen("temp.txt", "w");

while((ch=fgetc(fp)) != EOF)
{
if(ch != '\n')
{
dstr[i++]=ch;
continue;
}

dstr[i]='\0';

if(flag2 == 0)
{
parse_date(dstr, &dd1, month1, &mm1, &yy1);

if(yy < yy1)
flag2=1;
else if(mm < mm1 && yy == yy1)
flag2=1;
else if(dd < dd1 && mm == mm1 && yy == yy)
flag2=1;

if(flag2 == 1)
fprintf(ft, "%d %s %d\n", dd, month, yy);

fprintf(ft, "%s\n", dstr);
}
else
fprintf(ft, "%s\n", dstr);

i=0;
}

if(flag2 == 0)
fprintf(ft, "%d %s %d\n", dd, month, yy);

fclose(fp);
fclose(ft);

remove("date1.txt");
rename("temp.txt", "date1.txt");
}
}
Sep 19 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.