文本文件Cfile C ++中的行数

时间:2019-06-18 11:56:17

标签: c++ mfc cfile

我想使用Cfile打印在C ++中打印的行数。 我想根据日期写行数。 例如在同一个文件中写入两天,

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

除行号外,其他所有信息都正确

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,
        }
    }

1 个答案:

答案 0 :(得分:0)

保留上次写入日期的变量:

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

编写新行时,将上次写入日期与当前日期进行比较。如果日期不同,请重设行号。

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

然后,您可以在创建字符串时更新数字:

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);

此外,您问题中的示例没有足够的参数。而且似乎没有必要将整数转换为CString。只需使用%d格式说明符即可。

TODO:您可能想要添加一些在启动时运行的代码,以打开文件,读取最后一行并解析该行以获得最后写入日期。然后初始化lastWriteDay。当应用出于某种原因退出并在同一天重新启动时,需要使用此功能。