繁体   English   中英

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

[英]the number of line in text file Cfile C++

我想使用 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,
        }
    }

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

// 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初始化为那个。 当应用程序出于某种原因在同一天退出并重新启动时,需要这样做。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM