请问下,在EVC中写入文本,如果位数不够怎样补空格。
struct COMMOD
{
TCHAR code[16];
TCHAR sl[5];
};
BOOL CommodWrite(struct COMMOD commod,CString file) //fuc_write
{
HANDLE hFile = CreateFile(file,
GENERIC_WRITE, FILE_SHARE_READ,NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Could not create the file!"));
return FALSE;
}
else
{
int handle;
handle = int(hFile);
CFile myFile(handle);
myFile.SeekToEnd();
myFile.Write((char *)&commod,sizeof(COMMOD));
TCHAR * tmp = TEXT("\r\n");
myFile.Write((char *)tmp,4); //加回车换行
myFile.Close();
return TRUE;
}
}
_tcscpy(com.code,com_code);
_tcscpy(com.sl,tmp_sl);
_tLTrim(_T("%-16s"),com.code);
file="\\My Documents\\aa.txt";
ret=CommodWrite(com,file);
我现在就是如果CODE 和SL 如果分别没有20和5位的话就出现乱码,各位老大这个怎么解决?急啊
struct COMMOD
{
TCHAR code[16];
TCHAR sl[5];
};
BOOL CommodWrite(struct COMMOD commod,CString file) //fuc_write
{
HANDLE hFile = CreateFile(file,
GENERIC_WRITE, FILE_SHARE_READ,NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T("Could not create the file!"));
return FALSE;
}
else
{
int handle;
handle = int(hFile);
CFile myFile(handle);
myFile.SeekToEnd();
myFile.Write((char *)&commod,sizeof(COMMOD));
TCHAR * tmp = TEXT("\r\n");
myFile.Write((char *)tmp,4); //加回车换行
myFile.Close();
return TRUE;
}
}
_tcscpy(com.code,com_code);
_tcscpy(com.sl,tmp_sl);
_tLTrim(_T("%-16s"),com.code);
file="\\My Documents\\aa.txt";
ret=CommodWrite(com,file);
我现在就是如果CODE 和SL 如果分别没有20和5位的话就出现乱码,各位老大这个怎么解决?急啊
改成
myFile.Write((char *)commod,sizeof(COMMOD));
不知道行不行commod是一个结构,在内存中的表示方式和指针变量差不多(也是存放一个内存地址),你取这个结构的地址,转换成(char*),最终的指向位置就错了.
D:\1113\复件 intimednb\list\listDlg.cpp(97) : error C2440: 'type cast' : cannot convert from 'struct COMMOD' to 'char *'
我现在就是想如果不够20或者5的话,用空格补足就可以了,因为我现在如果写入的是20位和5位的话就写入是正常的。我就是不知道怎么添加空格