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