void CMFC_fileDlg::OnBnClickedButton3()
{
// TODO: 在此添加控制元件通知處理程式代碼
char szname[41];
char enter[5];
CString strfilter, strfilename, strtext,str1;
CString strEnter;
strEnter = "\r\n";
strfilter = "BOOKINFO Files(*.*)|*.*||";
CFileDialog dlg(FALSE, NULL, NULL, OFN_EXPLORER | OFN_HIDEREADONLY | OFN_ENABLESIZING | OFN_FILEMUSTEXIST, strfilter);
if (dlg.DoModal()==IDOK)
{
strfilename = dlg.GetPathName();
CFile m_file(strfilename, CFile::modeCreate | CFile::modeWrite);
if (m_list.GetCount()==0)
{
AfxMessageBox(_T("資料為空"));
return;
}
else
{
for (int i = 0; i < m_list.GetCount(); i++)
{
m_list.GetText(i, strtext);
memset(szname, '\0', sizeof(char) * 41);
memset(enter, '\0', sizeof(char) * 5);
USES_CONVERSION;
char * str = W2A(strtext);
char *enter1 = W2A(strEnter);
strcpy_s(szname, str);
strcpy_s(enter, enter1);
m_file.Write(szname,sizeof(szname));
m_file.Write(enter, sizeof(enter));
}
}
m_file.Close();
}
}
寫入情況如下:

怎樣能把寫入的整齊
uj5u.com熱心網友回復:
m_file.Write(szname,strlen(szname));uj5u.com熱心網友回復:

開頭還是這樣,每次寫入檔案的位元組要確定的,要不按次不好讀取
uj5u.com熱心網友回復:
sizeof 改為 strlen 試試看m_file.Write(szname, strlen(szname));
m_file.Write(enter, strlen(enter));
uj5u.com熱心網友回復:
如果上面幾位大神的不行。你試試我剛寫的代碼。我只能保證我寫的是對的一種寫法。其實關鍵是你用的Windows的記事本打開。。。記事本的換行"\r\n"就是坑爹的!!!
#include "stdafx.h"
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
vector<string> vstr;
vstr.push_back("畫畫"); vstr.push_back("喝茶");
vstr.push_back("問號"); vstr.push_back("什么");
char chPath[256] = { 0 };
sprintf(chPath, ("D:\\select.txt"));
//輸出檔案測驗
ofstream ofs(chPath, ios::out | ios::trunc);
if (ofs.is_open())
{
for (auto it = vstr.begin(); it != vstr.end(); it++)
{
ofs << it->c_str() << endl;
}
ofs.close();
}
//寫入檔案測驗
vector<string> vSelect;
ifstream ifs(chPath, ios::in | ios::binary);
if (ifs.is_open())
{
string strLine = "";
while (std::getline(ifs, strLine))
{
int pos = strLine.size() - 1;
if (strLine[pos] == 0x0d)
strLine.erase(pos, 1);
vSelect.push_back(strLine);
}
ifs.close();
}
return 0;
}
uj5u.com熱心網友回復:
換行="\r\n" 0D 0A 是window 風格。換行="\n" 0A 是Linux 風格。
windows 有
CFile::typeText Sets text mode with special processing for carriage return–linefeed pairs (used in derived classes only).
CFile::typeBinary Sets binary mode (used in derived classes only).
就是用來 轉換 他們的
uj5u.com熱心網友回復:
另外 Unicode 文本// if UTF8 file the 1st 3 chars are: EFh,BBh,BFh
// if UNICODE file the 1st 2 chars are :FFh,FEh
uj5u.com熱心網友回復:
其實還得看你原檔案是否是這樣的寫法。。。或者你可以做 空格 處理。。
uj5u.com熱心網友回復:
// TODO: Add your specialized code here and/or call the base class
CFile m_file("xxxx.txt", CFile::modeCreate | CFile::modeWrite);
USES_CONVERSION;
char *enter = W2A(L"\r\n");
char *str = W2A(L"畫面");
m_file.Write(str,strlen(str));
m_file.Write(enter, strlen(enter));
str = W2A(L"歷史");
m_file.Write(str,strlen(str));
m_file.Write(enter, strlen(enter));
str = W2A(L"圖書");
m_file.Write(str,strlen(str));
m_file.Write(enter, strlen(enter));
str = W2A(L"小說");
m_file.Write(str,strlen(str));
m_file.Write(enter, strlen(enter));
uj5u.com熱心網友回復:
CStdioFile file(...)file.WriteString(str);
file.WriteString("\n");
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/125113.html
標籤:界面
上一篇:一個執行緒下的問題
