文件讀寫(xiě):
C語(yǔ)言實(shí)現:
文件讀:
//方法1:
FILE *pFile=fopen("1.txt","r");
char content[20];
memset(content,0,20); //將數組中的內容全部設置為\0,下一行語(yǔ)句中遇到\0就不讀取了
fread(content,1,20,pFile); //讀取文件內容,直到遇見(jiàn)\0字符
//方法2:根據文件大小動(dòng)態(tài)聲明數組大小
char *content;
fseek(pFile,0,SEEK_END); //將文件指針移動(dòng)到末尾
long size=ftell(pFile);
//fseek(pFile,0,SEEK_SET); //再此將指針移動(dòng)到開(kāi)始位置,從而讀取
rewind(pFile); //用途和上一行語(yǔ)句一樣
content=new char[size+1]; //多一個(gè)用來(lái)存放結尾操作符
fread(content,1,size,pFile);
content[size]=0;
MessageBox(content);
fclose(pFile);
文件寫(xiě):
//如果缺少fflush或這fclose函數,程序exe不關(guān)閉的話(huà),則輸出內容不會(huì )從緩沖區中輸出到文件中
FILE *pFile=fopen("1.txt","w"); //以讀的方式打開(kāi)
char test[3];
test[0]='a';
test[1]=10; //換行
test[2]='b';
fwrite(test,1,3,pFile); //將輸出文件用Utraledit的16機制打開(kāi),發(fā)現中間多了一個(gè)回車(chē)0D(13)
//fwrite("hello world",1,strlen("hello world"),pFile);
fseek(pFile,0,SEEK_SET); //將文件指針移動(dòng)到文件開(kāi)始位置,用來(lái)跳轉寫(xiě)入
fflush(pFile); //系統會(huì )將緩沖區中的內容立 即寫(xiě)入到文件中,無(wú)需fclose操作也能成功保存
fclose(pFile); //關(guān)閉文件
思考題:將數字12345寫(xiě)到到文本文件中
FILE *pFile=fopen("1.txt","w");
char test[5];
//0的ASSII碼是48,因為在文本文件中存放的都是字符,所以必須得到指定數字對應的字符
test[0]=1+48;
test[1]=2+48;
test[2]=3+48;
test[3]=4+48;
test[4]=5+48;
fwrite(test,1,5,pFile);
//也可以按以下方式實(shí)現:
int test=12345;
char *str;
itoa(test,str,10);
fwrite(str,1,5,pFile);
C++語(yǔ)言實(shí)現:
文件讀:
ifstream ifs("1.txt");
char ch[100];
memset(ch,0,100);
ifs.read(ch,100);
ifs.close();
MessageBox(ch);
文件寫(xiě):
ofstream os("1.txt");
os.write("hello world",strlen("hello world"));
os.close();
Win 32 API實(shí)現:
文件讀:
HANDLE hFile=CreateFile("1.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
char result[100];
DWORD readed; //實(shí)際讀取到的字節數
ReadFile(hFile,result,100,&readed,NULL);
result[readed]=0; //結束字符
CloseHandle(hFile);
MessageBox(result);
文件寫(xiě):
HANDLE hFile=CreateFile("1.txt",GENERIC_WRITE,0,NULL,CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,NULL);
DWORD writen;
WriteFile(hFile,"hello world",strlen("hello world"),&writen,NULL);
CloseHandle(hFile);
MFC實(shí)現:
文件讀:
CFile file("1.txt",CFile::modeRead);
char *buffer;
DWORD length=file.GetLength();
buffer=new char[length+1];
buffer[length]=0;
file.Read(buffer,length);
file.Close();
MessageBox(buffer);
文件寫(xiě):
CFile file("1.txt",CFile::modeCreate|CFile::modeWrite);
file.Write("hello world!",strlen("hello world!"));
file.Close();
文件打開(kāi)保存對話(huà)框:
文件打開(kāi)對話(huà)框:
CFileDialog dlg(TRUE);
dlg.m_ofn.lpstrTitle="我的文件打開(kāi)";
dlg.m_ofn.lpstrFilter="Text Files(.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
if(IDOK==dlg.DoModal())
{
CFile file(dlg.GetFileName(),CFile::modeRead);
char *buffer;
DWORD length=file.GetLength();
buffer=new char[length+1];
buffer[length]=0;
file.Read(buffer,length);
file.Close();
MessageBox(buffer);
}
文件保存對話(huà)框:
CFileDialog dlg(FALSE);
dlg.m_ofn.lpstrTitle="我的文件保存";
dlg.m_ofn.lpstrFilter="Text Files(.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
dlg.m_ofn.lpstrDefExt="txt";
if(IDOK==dlg.DoModal())
{
CFile file(dlg.GetFileName(),CFile::modeCreate|CFile::modeWrite);
file.Write("hello world!",strlen("hello world!"));
file.Close();
}
注冊表讀寫(xiě):
系統默認位置(在NT之前是記錄在Win.ini文件中):
讀:
CString strSection = "My Section";
CString strStringItem = "My String Item";
CWinApp* pApp = AfxGetApp();
CString result=pApp->GetProfileString(strSection, strStringItem, NULL);
MessageBox(result);
寫(xiě):
//由MSDN得知,在NT以后,WriteProfileString函數將設置寫(xiě)在注冊表中
CString strSection = "My Section";
CString strStringItem = "My String Item";
CWinApp* pApp = AfxGetApp();
pApp->WriteProfileString(strSection, strStringItem, "test");
用戶(hù)自定義位置:
讀:
LONG strLenth;
//RegQueryValue就是為了得到?jīng)]有名稱(chēng)或默認的Value的值的
//第一次調用為了得到值的大小
RegQueryValue(HKEY_LOCAL_MACHINE,"software\\VS\\Test",NULL,&strLenth);
//在MSDN中已經(jīng)指出RegQueryValue函數的最后一個(gè)參數lpcbValue已經(jīng)包含了終止符號,
//所以不需要在此添加終止符號了,即char *pBuffer=new char[strLenth+1];
char *pBuffer=new char[strLenth];
//第二次調用,才真正是為了得到值
RegQueryValue(HKEY_LOCAL_MACHINE,"software\\VS\\Test",pBuffer,&strLenth);
MessageBox(pBuffer);
// RegQueryValueEx 函數的使用為了獲得指定名稱(chēng)的值
HKEY hKey;
DWORD dwLength; //接收值的長(cháng)度
DWORD dwType; //接收值的類(lèi)型
DWORD dwValue; //接收值本身
RegOpenKey(HKEY_LOCAL_MACHINE,"software\\VS\\Test",&hKey); //打開(kāi)子項
RegQueryValueEx(hKey,"age",NULL,&dwType,(LPBYTE)&dwValue,&dwLength); //打開(kāi)值
CString result;
result.Format("%d",dwValue);
MessageBox(result);
寫(xiě):
HKEY hKey;
RegCreateKey(HKEY_LOCAL_MACHINE,"software\\VS\\Test",&hKey);
//RegSetValue就是為了給沒(méi)有名稱(chēng)或默認的Value設置值的
RegSetValue(hKey,NULL,REG_SZ,"HELLO WORLD",strlen("HELLO WORLD"));
RegCloseKey(hKey);
//RegSetValueEx 函數為了設置指定名稱(chēng)值存在,當然該函數也可以實(shí)現RegSetValue的功能
DWORD dwAge=30;
RegSetValueEx(hKey,"age",0,REG_DWORD,(BYTE*)&dwAge,4);
RegCloseKey(hKey);