File Handling in VC
-
Upload
mandeep-malik -
Category
Documents
-
view
66 -
download
3
Transcript of File Handling in VC
![Page 1: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/1.jpg)
![Page 2: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/2.jpg)
File ProcessingFile Processing is ability to create, store and
retrieve the content of a file from medium such as hard drive, floppy disk, DVD-ROM,CD-ROM etc.
C/C++ : File functions are stream functions like fopen(), fread(), fwrite(), fclose().
VC++: Windows is multitasking environment , support non stream functions .
MFC Libraries------CFile Class.
![Page 3: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/3.jpg)
C file operations#include <stdio.h>void main(){
File *fp;if((fp=fopen(“hello.txt”, ”w”))!=NULL){
fwrite(“hello” , strlen(“hello”),0,fp);fclose(fp);
}}
![Page 4: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/4.jpg)
CFile class Member functionMember Meaning
CFile Construct0r
Open Open a file
Close Closes file , delete object
Read Reads data from file
Write Write to current file position
Flush Flush data
GetLength Gets length of the file
GetPosition Gets file pointer
Remove Delete specified file
Rename Rename specified file
seek Moves file Pointer
SeekToBegin Moves file Pointer beginning of file
SeekToEnd Moves file Pointer to the end of file
![Page 5: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/5.jpg)
CFile Open() OptionsCFile::modeCreate Create a new file and truncate to zero
length
CFile::modeRead Open the file for reading only
CFile::modeWrite Open the file for writing only
CFile::modeReadWrite Open the file for reading writing
CFile::modeNoInherit File cannot inherited by child processes
CFile::typeBinary Sets binary mode
CFile::typeText Set text mode
CFile::shareDenyWrite Open the file and deny others write access.
CFile::shareDenyRead Open the file and deny others read access
CFile::shareDenyNone Open the file without deny any access
CFile::shareExclusive Open the file and deny others all access.
![Page 6: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/6.jpg)
![Page 7: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/7.jpg)
Add member variable for Edit box and Event handler for ButtonMember variable
IDC_EDIT1m_text1IDC_EDIT2m_text2
Event HandlerIDC_BUTTON1OnButton1()IDC_BUTTON2OnButton2()
![Page 8: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/8.jpg)
Creating &Writing a Filevoid CFilehandingDlg::OnButton1() {
UpdateData(true);CFile file("Hello.txt",CFile::modeCreate|CFile::modeWrite);file.Write(m_text1,m_text1.GetLength());file.close();
}
![Page 9: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/9.jpg)
Reading a Filevoid CFilehandingDlg::OnButton2() {
const max=20;char ss[max];CFile file("Hello1.txt",CFile::modeRead);
UINT num_read=file.Read(ss,max);m_text2=ss;UpdateData(false);file.Close();
}
![Page 10: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/10.jpg)
Output
![Page 11: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/11.jpg)
Sequential and Random Access FileSequential file
Write file from beginning to endCannot point at specified locationLike Tape
Random Access FileWrite and read in form of recordsPoint to specified location through seek
function
![Page 12: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/12.jpg)
Random Access File“This”
“is”
“a”
“test”
![Page 13: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/13.jpg)
Add member variable for Edit box and Event handler for ButtonMember variable
IDC_EDIT1m_text1IDC_EDIT2m_text2
Event HandlerIDC_BUTTON1OnButton1()IDC_BUTTON2OnButton2()
![Page 14: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/14.jpg)
Writing a ramdom access Filevoid CFile1handlingDlg::OnButton1() {const MAX_LEN=20;const MAX_ITEMS=4;char output[MAX_ITEMS][MAX_LEN];strcpy(output[0],"This");strcpy(output[1],"is");strcpy(output[2],"a");strcpy(output[3],"test");
CFile randomfile("data.dat",CFile::modeCreate|CFile::modeWrite);
for(int i=0;i<MAX_ITEMS;i++){
randomfile.Write(output[i],MAX_LEN);}randomfile.Close();}
![Page 15: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/15.jpg)
Seek() functionSeek(offset,method)
Offset in byte you want to moveMethod: how you move
CFile::begin CFile::current CFile::end
![Page 16: File Handling in VC](https://reader033.fdocuments.us/reader033/viewer/2022051110/54fd690c4a7959fc798b4603/html5/thumbnails/16.jpg)
Reading a Filevoid CFile1handlingDlg::OnButton2() {
const max=2;char ss[max];CFile randomfile("data.dat",CFile::modeRead);randomfile.Seek(max,CFile::begin);int num_read=randomfile.Read(ss,2);
m_text2=ss;UpdateData(false);randomfile.Close();
}