Vous êtes sur la page 1sur 16

File Processing

File Processing is ability to create, store and

retrieve the content of a file from medium such as hard drive, floppy disk, DVD-ROM,CDROM 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 function


Member CFile Open Close Read Write Flush GetLength GetPosition Remove Rename seek SeekToBegin SeekToEnd Meaning Construct0r Open a file Closes file , delete object Reads data from file Write to current file position Flush data Gets length of the file Gets file pointer Delete specified file Rename specified file Moves file Pointer Moves file Pointer beginning of file Moves file Pointer to the end of file

CFile Open() Options


CFile::modeCreate CFile::modeRead CFile::modeWrite CFile::modeReadWrite CFile::modeNoInherit CFile::typeBinary CFile::typeText CFile::shareDenyWrite CFile::shareDenyRead CFile::shareDenyNone CFile::shareExclusive Create a new file and truncate to zero length Open the file for reading only Open the file for writing only Open the file for reading writing File cannot inherited by child processes Sets binary mode Set text mode Open the file and deny others write access. Open the file and deny others read access Open the file without deny any access Open the file and deny others all access.

Add member variable for Edit box and Event handler for Button
Member variable IDC_EDIT1m_text1 IDC_EDIT2m_text2 Event Handler
IDC_BUTTON1OnButton1() IDC_BUTTON2OnButton2()

Creating &Writing a File


void CFilehandingDlg::OnButton1() { UpdateData(true); CFile file("Hello.txt",CFile::modeCreate| CFile::modeWrite); file.Write(m_text1,m_text1.GetLength()); file.close(); }

Reading a File
void 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 File


Sequential file Write file from beginning to end Cannot point at specified location Like Tape Random Access File
Write and read in form of records Point to specified location through seek function

Random Access File


This is a test

Add member variable for Edit box and Event handler for Button
Member variable IDC_EDIT1m_text1 IDC_EDIT2m_text2 Event Handler
IDC_BUTTON1OnButton1() IDC_BUTTON2OnButton2()

Writing a ramdom access File


void 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() function
Seek(offset,method)
Offset in byte you want to move Method: how you move CFile::begin CFile::current CFile::end

Reading a File
void 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(); }

Vous aimerez peut-être aussi