Read and write operations on files and opening and saving of files

Source: Internet
Author: User
Tags fread readfile rewind save file

Through the recent study, summed up about the file read and write operations and the file opening and saving methods.

One, the file read and write operation:

(1) C language to read and write the file operation

In the C language, the read and write operation of the file is implemented by the file structure and the commonly used functions of files operation, the following summarizes the common functions of file manipulation in C language:

fopen () to open the file specified by file name

fwrite () write file

fread () Read the file

fseek () moves the pointer of the file to the new position by setting the offset and initial position of the function's parameters

Rewind () moves the pointer of the file to the beginning of the file stream and, under normal circumstances, fseek () implements the same functionality, but there is a difference

Ftell () gets the position of the current file pointer, which is used to get the length of the file stream

fflush () flushes the contents of the buffer, writes the contents of the file stream directly from the buffer to the disk when the file is written, and writes the disk file directly to the buffer when the file is read, without having to wait for the program to end or close the program.

fclose () close file

memset () fills a given value in a block of memory

The sample code is as follows:

/**********************************************************
* C Language Implementation file write operation *
***********************************************************/

FILE *pfile=fopen("CLanguage.txt", "w");
fwrite ("Clanguage", 1,strlen("Clanguage"), pFile);
fseek(pfile,0,seek_set);
fwrite("Implement file Write operation", 1,strlen ("Implement file Write operation"), PFile);
fclose (PFile);

 /**********************************************************
 *                 C language for file read operation                                    *
 *************************************************** ********/

FILE *pfile=fopen("CLanguage.txt", "R");
Char ch[100];
memset(ch,0,100);
fread(ch,1,100,pfile);
MessageBox(CH);
char* PBuf;
int length;
fseek (Pfile,0,seek_end);
Length=ftell(pFile);
Pbuf=new char[length+1];
Fseek (Pfile,0,seek_set);
Rewind (PFile);
fread (Pbuf,1,length,pfile);
pbuf[length]=0;
MessageBox (PBUF);
fclose (PFile);

Note: When you implement a read operation for a file in C, be aware of the pointer position of the file . Also note the difference between a text file and a binary file .

(2) C + + implementation of the file read and write operations

In the C + + language implementation of the file read and write operations, to use the Ofstream class and the Ifstream class , with the object of the class call its member function write () and read () Implement file read and write.

The sample code is as follows:

/**********************************************************
 *                 C + + implementation of file write operations                                    *
 ********************************************************* **/
  ofstream  ofs ("C++language.txt");
 ofs. Write ("C++language", strlen ("C++language"));
 ofs. Close ();

/**********************************************************
* C + + implementation of file read operation *
***********************************************************/
ifstream IFS ("C++language.txt");
Char ch[100];
memset (ch,0,100);
Ifs. Read (ch,100);
MessageBox (CH);
Ifs. Close ();

Note: When using C + + to implement file read and write operations, the Ofstream class and the Ifstream class are used, so the header file of the class is included:

#include <fstream>
using namespace Std;

(3) Win32 API function for file read and write operation

Using the Win32 API function to implement file read and write operations commonly used functions are as follows:

CreateFile ()

WriteFile ()

ReadFile ()

CloseHandle ()

The sample code is as follows:

/**********************************************************
 *                 Win32 API for file write operations                                    *
 *************************************************** /
 handle FileHandle;
 filehandle= CreateFile ("Win32 API.txt", Generic_write,0,null,create_new,
   file_attribute_normal,null);
 dword dwwrites;
  WriteFile (FileHandle, "Win32 API function", strlen ("Win32 API function"),
   &dwwrites,null);
  closehandle (filehandle);

/**********************************************************
* Win32 API for file read operation *
***********************************************************/
HANDLE FileHandle;
filehandle=CreateFile("Win32 API.txt", generic_read,0,null,open_existing,
File_attribute_normal,null);
DWORD dwreads;
Char ch[100];
ReadFile (Filehandle,ch,100,&dwreads,null);
ch[dwreads]=0;
MessageBox (CH);
CloseHandle (FileHandle);

(4) MFC implementation of file read and write operations

Using MFC to implement the file read and write operations, to use the CFile class , the class constructor and member functions will complete a series of file operations related work.

The sample code is as follows:

**********************************************************
* MFC implements file write operations *
***********************************************************/
CFile file ("Mfc.txt", cfile::modecreate| Cfile::modewrite);
File. Write ("Hello! Awa", strlen ("hello! awa"));
File. Close ();

/**********************************************************
* MFC implements file read operation *
***********************************************************/
CFile file ("Mfc.txt", Cfile::moderead);
DWORD Filelenth;
Filelenth=file. GetLength ();
char* Pbuf=new char[filelenth+1];
pbuf[filelenth]=0;
File. Read (Pbuf,filelenth);
MessageBox (PBUF);
File. Close ();
Delete PBuf;

Ii. opening and saving of documents

Opening and saving the file will pop up the file Open and Save dialog, so use the CFileDialog class to implement the file opening and saving. In the application of the Cfieldialog class, the OPENFILENAME structure is often used, in which there are related variables to set up the file opening and saving dialog (for example: setting of the dialog box title, adding the default file extension, Filter settings, etc.).

The sample code is as follows:

/**********************************************************
* File Dialog action--Save file *
***********************************************************/
CFileDialogFiledlg (FALSE);
Filedlg.m_ofn.Lpstrtitle= "My File Save dialog box";//dialog box title
Filedlg.m_ofn.Lpstrfilter= "Textfile (*.txt) \0*.txt\0allfile (*. *) \0*.*\0\0";//Filter settings
Filedlg.m_ofn.Lpstrdefext= "txt";//Save file default extension
if (Idok==filedlg.DoModal())
{
CFileFile (Filedlg.GetFileName(), Cfile::modecreate | Cfile::modewrite);
File.Write("File Save Dialog", strlen ("File Save dialog box");
File.Close();
}

/**********************************************************
* File Dialog action--Open file *
***********************************************************/
CFileDialogFiledlg (TRUE);
Filedlg.M_ofn.lpstrtitle= "My file opens the dialog";
Filedlg.M_ofn.lpstrfilter= "Textfile (*.txt) \0*.txt\0allfile (* *) \0*.*\0\0";

if (Idok==filedlg.DoModal())
{
CFile file (Filedlg.GetFileName(), cfile::moderead);
DWORD filelength;
Filelength=file.GetLength();
char* Pbuf=new char[filelength+1];
pbuf[filelength]=0;
File.Read(Pbuf,filelength);
MessageBox (PBUF);
Delete PBuf;
File.Close();
}

Read and write operations on files and opening and saving of files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.