File Handling in VC

Post on 09-Mar-2015

63 views 3 download

transcript

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.

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

}}

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

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.

Add member variable for Edit box and Event handler for ButtonMember variable

IDC_EDIT1m_text1IDC_EDIT2m_text2

Event HandlerIDC_BUTTON1OnButton1()IDC_BUTTON2OnButton2()

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

}

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

}

Output

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

Random Access File“This”

“is”

“a”

“test”

Add member variable for Edit box and Event handler for ButtonMember variable

IDC_EDIT1m_text1IDC_EDIT2m_text2

Event HandlerIDC_BUTTON1OnButton1()IDC_BUTTON2OnButton2()

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

Seek() functionSeek(offset,method)

Offset in byte you want to moveMethod: how you move

CFile::begin CFile::current CFile::end

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

}