如何在vc ++ mfc中将数据存储到文件中 [英] How to store data in to the file in vc++ mfc

查看:560
本文介绍了如何在vc ++ mfc中将数据存储到文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些错误和警告消息保存到日志文件中,我希望创建日志文件取决于日期,如果数据相同,我不应该创建新文件,需要打开现有文件并保存数据。 />
无法将所有数据保存到文件中,只保存前4个字符。

你可以帮我纠正吗



我尝试过:



 time_t timeVal = 0; 

time(& timeVal);
char szTimeBuff [128];
struct tm tmVal;
localtime_s(& tmVal,& timeVal);
size_t zCount = strftime(szTimeBuff,_countof(szTimeBuff),%Y_%m_%d_,& tmVal);
cstring data Leror data请输入正确的用户名和密码

CString FileName = Llog_error_;
FileName + = szTimeBuff;
FileName + =_ Log;
CFile myFile;

if(myFile.Open((FileName),CFile :: modeCreate |
CFile :: modeReadWrite))
{
myFile.Write(data,sizeof( data)); //它没有显示文件日志中的所有数据,它只显示前4个字符,即erro
myFile.Flush();
myFile.Seek(0,CFile :: begin);
}

解决方案

要将数据附加到现有文件并创建该文件(如果该文件尚不存在),请使用 CFile :: modeNoTruncate 标志而不是 CFile :: modeCreate 。后者只会在不存在的情况下创建文件,否则抛出异常(参见 CFile Class [ ^ ]) 。



sizeof operator - cppreference.com [ ^ ]在编译期间处理获取对象的大小。它不会像字符串那样返回动态运行时数据的长度。



写入时必须以字节为单位传递数据长度:

 myFile.Write(data.GetString(),data.GetLength()* sizeof(TCHAR)); 

GetLength()返回字符串中的字符数。因为 Write()函数是基于字节的,所以字符数必须乘以每个字符的字节数(使用Unicode应用程序时为2)。



另请注意,如果您的应用程序是Unicode,则创建Unicode文本文件(字符串文字的 L 前缀假定为什么)


i want to save the some error and warning messages into the log file and i want create th log file depends on date if data is same i should not create new file and need to open existing file and save the data.
not able to save the all the data into the file and its saving only first 4 characters.
could you please correct me

What I have tried:

time_t timeVal = 0;

    time(&timeVal);
    char szTimeBuff[128];
    struct tm tmVal;
    localtime_s(&tmVal, &timeVal);
    size_t zCount = strftime(szTimeBuff, _countof(szTimeBuff), "%Y_%m_%d_", &tmVal);
cstring data L"eror data please enter the correct user name and password"
	
    CString FileName = L"log_error_";
    FileName += szTimeBuff;
    FileName += "_Log";
    CFile	myFile;

    if (myFile.Open((FileName ), CFile::modeCreate |
        CFile::modeReadWrite))
    {
        myFile.Write(data, sizeof(data));// its not showing all the data in the file log its showing only first 4 character i.e erro
        myFile.Flush();
        myFile.Seek(0, CFile::begin);
    }

解决方案

To append data to an existing file and create the file if it does not exists already, use the CFile::modeNoTruncate flag instead of CFile::modeCreate. The latter will only create files if they did not exist and throw an exception otherwise (see the CFile Class[^]).

The sizeof operator - cppreference.com[^] is processed during compilation time to get the size of an object. It will not return the length of dynamic runtime data like strings.

You have to pass the length of the data in bytes when writing:

myFile.Write(data.GetString(), data.GetLength() * sizeof(TCHAR));

GetLength() returns the number of characters in the string. Because the Write() function is byte based, the number of characters has to be multiplied with the number of bytes per character (2 with Unicode applications).

Note also that you are creating a Unicode text file if your application is Unicode (what the L prefix of the string literal assumes).


这篇关于如何在vc ++ mfc中将数据存储到文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆