Solved

Searching a file based on dates

Posted on 1998-08-18
13
196 Views
Last Modified: 2010-04-01
Hi,
I have task like this.
I have a text file which has lines like this.
ProbNO, StartedBy,StartDate, Processed By,ProcessDate,EndedBy,EndDate.
The above is a general form of a single line.
The example entries in the file are like
1,John,1998/09/12,Jock,1998/09/13,Jill,1998/09/14
2,John,1998/09/13,Jock,1998/09/14,Jill,1998/09/15
3,John,1998/09/13,Jock,1998/09/14,Jill,1998/09/15
4,John,1998/09/14,Jock,1998/09/15,Jill,1998/09/16
5,John,1998/09/17,Jock,1998/09/18,Jill,1998/09/19
6,John,1998/09/20,Jock,1998/09/21,Jill,1998/09/22  

Like above.
The user from the inter face enters
Start Date :  1998/09/12
End Date   :  1998/09/16
and says OK.
I have to return a datewise problem numbers started and ended. i.e.
Date        Started   Ended
1998/09/12     1        0
1998/09/13     2        0
1998/09/14     1        1
1998/09/15     0        2
1998/09/16     0        1

That is the number of problems started on the date and the number of problems ended on that date for all dates.
 Hope this is clear.      

0
Comment
Question by:ardney
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 3

Expert Comment

by:Norbert
ID: 1170677
Now I believe I understand what you want
0
 

Expert Comment

by:vikaj
ID: 1170678
what you can do is to treat the entries as separate records each having 6 fields
1>starter
2>start date
3>processed by
4>process date
5>ended by
6>end date
now you can sort the records on start date  field and within start date on end date field
since you don't need the other 4 fields here they are getting destroyed.however you can save the original file beforehand.NOW you have the input start date.search the table
on start date until you find a match.
here i give you an example
input   start date      end date
              14               15

    the sorted file
     start date          end date   otherfields
       14                     15
       14                      16
        14                     17
         15                   16
        15                     17
   and so on
       each time you meet 14 have s(14)++ if the end date is within the range then
have  e(15)++ you don't have to search any longer since end day>startday!!
  tell me whether you understand????
0
 

Author Comment

by:ardney
ID: 1170679
I know the logic. But I am not in a position to keep it in the form of a c++ code.
some code will help.
0
 
LVL 3

Expert Comment

by:Norbert
ID: 1170680
Use strtok to seperate the strings to the fields you have
because you have a fixed format you know which string belongs to which filed
0
 

Author Comment

by:ardney
ID: 1170681
Some code will help
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170682
ardney, the problem is that we can only provide limited help on a schoolwork assignment.  We can answer SPECIFIC questions, like you might ask your teacher.  We can make general suggestions, and we can review your work and make criticisms.  If you would like to start the work and submit what you get, we can ofter some suggestions.   Or ask us SPECIFC questions.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ardney
ID: 1170683
If I have a string and want to extract the characters after I encounter a comma, What I have to do?

0
 

Expert Comment

by:gkibble
ID: 1170684
String parsing, one of my favorite things about C++.  I like the Left and Mid CString functions but there are other ways to parse.

CString csMyString = ???;
int nPos = csMyString.Find( ',' );    // search for comma
CString csPreComma, csPostComma;

if ( nPos != -1 )
{
    csPreComma = csMyString.Left( nPos );
    csPostComma = csMyString.Mid( nPos + 1 );
}
else
{
   // comma not found
}

0
 
LVL 22

Expert Comment

by:nietod
ID: 1170685
As norbert suggested  (spelling okay?  : - ) )

The strtok() procedure would be perfect for your purposes.  It returns a pointer to the next token (part of the string seperated by ',' in this case) in a string

You would use it like

char *FstTknPtr;
char *SecTknPtr;
char SrcStr[] = "first,second";

FstTknPtr = strtok(SrcStr,",");
SecTknPtr = strtok(SrcStr,',');

This makes FstTknPtr point to a string with "first" and SecTknPtr point to one with "second".

0
 

Accepted Solution

by:
vikaj earned 100 total points
ID: 1170686
say you have the following string :
           I DO NOT LIKE STUDYING , PRICKING,HACKING.
           NOW YOU WANT TO EXTRACT THE PORTION "PRICKING"
        CHAR    X[100],Y[100];
        INT  A,B;
        A=0;
       B=0;
        L=STRLEN(X);
       WHILE ((X[A] !=','AND (A<L))
{
          A++
}
A++
DO WHILE (X[A] !=',')
         {
             Y[B]=X[A];
              B++;
              A++;
        }
       NOW YOU HAVE "PRICKING" IN Y.
        THIS IS ONLY THE REQ PART NOT THE WHOLE PROGRAM.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1170687
I was about to go on and suggest that strtok() is not the best way to do this (it is the easiest, but it is can cause you headaches), but I think gkibble is putting you on the right track.
0
 

Expert Comment

by:gkibble
ID: 1170688
I'll modify my code to mimick strtok on one line of data

CString csDataField[7];
CString csDataLine;
int nPos;
int nField = 0;

while ( (nPos = csDataLine( ',' ) ) != -1 )    // search for comma
{
   // get data before comma
    csDataField[ nField++ ] = csMyString.Left( nPos );

   // chop off field and comma from data line
     csDataLine = csDataLine.Mid( nPos + 1 );
}

This will break data line into 7 comma seperated fields
Note you should add null checks and make sure nField does not go above 6!

0
 
LVL 22

Expert Comment

by:nietod
ID: 1170689
gkibble, you are using CString.  That is not standard C++.  I can assure you that this is not an MFC application.  You need to use the C++ string class.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now