471,092 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

sorting dates

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
8 13747
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
how can i do this..

i am just a beginner in c.

not sure how should i do it
Sep 19 '06 #3
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
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
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
the date is in the following format;

date month year
ie: 12 feb 2006
Sep 19 '06 #7
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
kuldeep0110
1 Bit
You are given a list of dates as an array of strings. Write a program to
sort the dates chronologically, and print the sorted list.
(i) The dates are provided in the EU format DD-MMM-YYYY, like 20-Jun-2020.
(ii) The dates are provided in the US format MMM DD, YYYY, like aug 02, 2022.

can anybdy help me with the c program for above problem
1 Week Ago #9

Post your reply

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

Similar topics

9 posts views Thread by p0wer | last post: by
22 posts views Thread by mike | last post: by
18 posts views Thread by Scott | last post: by
3 posts views Thread by melanieab | last post: by
19 posts views Thread by Owen T. Soroke | last post: by
5 posts views Thread by Mike | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.