-3

i want to print the number of line printed in C++ by using Cfile. i want to write the number of line according to the date. for example in the same file it is written for two days ,

Nr   date   time     code     value
1 10/6/2019  09:10   220      55
2 10/6/2019  10:16    33       23
3 10/6/2019  10:50    55       11
1 11/6/2019  03:55    11       15
2 11/6/2019  08:22    18       20

everything is right except the line number

CString strDate, strYearShort, strTime;

strYearShort = (CString)Datum.year;
strYearShort = strYearShort.Right(2);
strDate = (CString)Datum.day + '.' + (CString)Datum.month + '.' + strYearShort;
strTime = (CString)Zeit.hour + ':' + (CString)Zeit.minute;

buf.Format(_T("%s\t%s\t%s\t%s\t%s\t%d\r\n\r\n"), strDate, strTime, (CString)number, (CString)cardnumber, value);

enter code hereif (CTime::GetCurrentTime().GetDay() != lastWriteDay) {
        for (i = 1, i < 100, i++) {
            int Nu = i,
        }
    }
    else if (CTime::GetCurrentTime().GetDay() = lastWriteDay) {
        Nu == 0;
            or (i = 1, i < 100, i++) {
            Nu = i,
        }
    }
abd.ch
  • 1
  • 2
  • The code and the example output don't seem to match. Can you clarify what you are trying to do? – 001 Jun 18 '19 at 12:03
  • it is a small example, the output has more information. my question is how can print the number of line and start from 1 in the next day – abd.ch Jun 18 '19 at 12:04
  • Are you saying that you want a `4` and not a `1` on the last line of the output? – 001 Jun 18 '19 at 12:06
  • 1 in the new date, exactly like i wrote – abd.ch Jun 18 '19 at 12:08
  • i spent long time when i was looking in the internet how i can solve this issue. but i have not found anything – abd.ch Jun 18 '19 at 12:11
  • Does the software run constantly and you want to reset the line counter at midnight? – 001 Jun 18 '19 at 12:21
  • exactly, this is what i am looking for. this software is working constantly and reset the counter at midnight – abd.ch Jun 18 '19 at 12:27
  • 1
    Sounds simple enough. Just keep a variable of the last write time/date. When writing a new line, compare last write time to the current time. If the dates are different, reset the line number. – 001 Jun 18 '19 at 12:32
  • Could you please give me an example in MFC ? – abd.ch Jun 18 '19 at 12:37

1 Answers1

0

Keep a variable of the last write date:

// Should be persistent for lifetime of app:
// eg: global, static, or class member
int lastWriteDay = 0;

When writing a new line, compare last write date to the current date. If the dates are different, reset the line number.

....
if (CTime::GetCurrentTime().GetDay() != lastWriteDay) {
    number = 1;
    lastWriteDay = CTime::GetCurrentTime().GetDay();
}
// Do write....
....

Then you can update the number when you create the string:

buf.Format(_T("%s\t%s\t%d\t%d\t%s\t%d\r\n\r\n"),
    strDate,
    strTime,
    number++,    // Increment for next time
    cardnumber,
    value);

Also, the example in your question does not have enough parameters. And it seems unnecessary to convert the integers to CString. Just use the %d format specifier.

TODO: You might want to add some code that runs on startup that opens the file, reads the last line and parses the line to get the last write day. Then initialize lastWriteDay to that. This is needed for when the app has quit and restarted on the same day for some reason.

001
  • 13,291
  • 5
  • 35
  • 66
  • if (CTime::GetCurrentTime().GetDay() != lastWriteDay) { for (i = 1, i < 100, i++) { int number = i, } } else if (CTime::GetCurrentTime().GetDay() = lastWriteDay) { number =0 or (i = 1, i < 100, i++) { number = i, } } i want to try this code but i could not check the result now could i take your opinon if this code is right in general @Johnny Mopp – abd.ch Jun 18 '19 at 13:47
  • I'm not sure what you are doing with the loops. Just add my code to right before the code where you call `buf.Format(....` – 001 Jun 18 '19 at 14:09
  • Hallo again, today i tried this code with the machine. number does not increased it gave me 1 in each time. i have tired several times but is not working – abd.ch Jun 27 '19 at 08:20
  • Could you help me please – abd.ch Jun 27 '19 at 08:59
  • this is the code i have used, the only value for n is 1 – abd.ch Jun 27 '19 at 11:43
  • @abd.ch `if (Datum.day != lastWriteDay) { n = 1; lastWriteDay = Datum.day; }` – 001 Jun 27 '19 at 13:02
  • after i tried this new code, the result is not changed it gave just number 2 , 2 the number is not 1 , 2 @Johnny Mopp – abd.ch Jun 27 '19 at 14:22