file handling in vc

16

Upload: mandeep-malik

Post on 09-Mar-2015

65 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: File Handling in VC
Page 2: File Handling in VC

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

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

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

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
Page 7: File Handling in VC

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

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

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

Output

Page 11: File Handling in VC

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

Random Access File“This”

“is”

“a”

“test”

Page 13: File Handling in VC

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

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

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

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();

}