VC++中文件讀、寫和其他相關操作匯總


在軟件設計中,對文件系統的利用往往是必不可少的,它能幫助我們存儲許多比較重要的數據,保存過程數據和備份數據,以備軟件出現不可預知的偶然異常時,恢復測試數據和測試過程使用。

下面結合實例來講述文件相關的一些操作(完整的實例程序可在我的CSDN資源中下載:http://download.csdn.net/detail/margin1988/4239839):

1)創建目錄(文件夾):

   1)方法1

1 CString strDir;  
2 strDir.Format("%sdir1",g_BasePath);  
3 ::CreateDirectory(_T(strDir),NULL);  

  2)方法2 

1 #include <direct.h>  
2 strDir.Format("%sdir2",g_BasePath);  
3 _mkdir(strDir);  

2)創建及寫文件(不追加模式、追加模式):

  1)不追加模式:

 1 CStdioFile file;  
 2 CString str;  
 3 str.Format("%sdir1\\data1.txt",g_BasePath);  
 4 if (!file.Open(_T(str),CFile::modeCreate |CFile::modeWrite | CFile::typeText))  
 5 {  
 6   MessageBox(_T("未打開文件"));  
 7 }  
 8 else 
 9 {  
10   for (int i=1;i<11;i++)  
11   {  
12      str.Format("%d-%d\n",i,i*i); 
13      file.WriteString(str);  
14   }  
15   file.Close();  
16 }  

   2)追加模式:

 1 CStdioFile file;  
 2 CString str;  
 3 str.Format("%sdir1\\data2.txt",g_BasePath);  
 4 if (!file.Open(_T(str),CFile::modeCreate |CFile::modeWrite | CFile::typeText | CFile::modeNoTruncate))  
 5 {  
 6   MessageBox(_T("未打開文件"));  
 7 }  
 8 else 
 9 {  
10   for (int i=1;i<11;i++)  
11   {  
12      str.Format("%d\n",i);  
13      file.SeekToEnd();//定位至文件末尾  
14      file.WriteString(str);  
15   }  
16   file.Close();  
17 }  

3)讀文件:

 1 CStdioFile file;  
 2 CString str,str1,str2;  
 3 str.Format("%sdir1\\data1.txt",g_BasePath);  
 4 if (file.Open(_T(str),CFile::modeRead |CFile::typeText))  
 5 {  
 6   file.SeekToBegin();//定位到文件開頭  
 7   while(file.ReadString(str))//讀取文件中的一行  
 8   {  
 9      if(AfxExtractSubString(str1,str,0,'-'))//截取字符串  
10      {  
11         if(AfxExtractSubString(str2,str,1,'-')) 
12         {  
13            MessageBox(str1+"的平方="+str2);  
14         }  
15      }  
16   }  
17   file.Close();  
18 }  
19 else 
20   MessageBox(_T("data1.txt文件打開失敗"));  

4)計算文件行數:

 1 CStdioFile file;  
 2 CString str;  
 3 str.Format("%sdir1\\data2.txt",g_BasePath);  
 4 int lineNum=0;//統計文件行數的變量  
 5 if (file.Open(_T(str),CFile::modeRead |CFile::typeText))  
 6 {  
 7   file.SeekToBegin();//定位到文件開頭  
 8   while (file.ReadString(str)) //讀取文件中的一行  
 9   {  
10      lineNum++;  
11   }  
12   file.Close();//關閉文件  
13   str.Format("data2.txt共計%d 行",lineNum);  
14   MessageBox(str);  
15 }  
16 else 
17   MessageBox(_T("data2.txt文件打開失敗")); 

5)計算目錄下文件個數: 

 1 //SDK方式統計指定目錄下的文件個數  
 2 HANDLE hFind;   
 3 WIN32_FIND_DATA dataFind;   
 4 BOOL bMoreFiles=TRUE;   
 5 int iCount=0;//統計文件數的變量  
 6 CString str;  
 7 str.Format("%sdir1\\",g_BasePath);//str是指定的路徑  
 8 hFind=FindFirstFile(str+"*.*",&dataFind);//找到路徑中所有文件  
 9 //遍歷路徑中所有文件  
10 while(hFind!=INVALID_HANDLE_VALUE&&bMoreFiles==TRUE)
11 {     if(dataFind.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)//判斷是否是文件  
12   {   
13      iCount++;   
14   }   
15   bMoreFiles=FindNextFile(hFind,&dataFind);   
16 }   
17 FindClose(hFind);  
18 str.Format("dir1目錄下文件個數共計%d 個",iCount);  
19 MessageBox(str);  

6)刪除文件:

1 CFileFind finder;  
2 CString str;  
3 str.Format("%sdir1\\data1.txt",g_BasePath);  
4 if (finder.FindFile(_T(str)))  
5 {  
6   ::DeleteFile(_T(str));  
7 }  

7)刪除目錄:

   RemoveDirectory_rmdir兩者都只能刪除空文件夾,若要刪其下有文件的文件夾,需先刪除其下的所有文件,再刪除文件夾。

   1)方法1 

1 CString strDir;  
2 strDir.Format("%sdir2",g_BasePath);  
3 ::RemoveDirectory(strDir);  

   2)方法2 

1 #include <direct.h>  
2 strDir.Format("%sdir1",g_BasePath);  
3 _rmdir(strDir); 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM