How to read and write files in C ++ Builder

Source: Internet
Author: User

When programming in C ++, code file operations are a common problem. In C ++ Builder, you can use multiple methods to operate files, in C ++ Builder, we will introduce this in detail in the following sections. The following will be a one-to-one list:

1. C-based file operations;

2. File Operations Based on C ++;

3. WINAPI-based file operations;

4. File Operations Based on the BCB library;

5. Special file operations.

In ANSIC, file operations are divided into two methods: stream file operations and I/O file operations, which are described below.

1) stream File Operations

This FILE operation has an important structure FILE, which is defined in stdio. h as follows:

 
 
  1. typedef struct {   
  2. int level; /* fill/empty level of buffer */   
  3. unsigned flags; /* File status flags */   
  4. char fd; /* File descriptor */   
  5. unsigned char hold; /* Ungetc char if no buffer */   
  6. int bsize; /* Buffer size */   
  7. unsigned char _FAR *buffer; /* Data transfer buffer */   
  8. unsigned char _FAR *curp; /* Current active pointer */   
  9. unsigned istemp; /* Temporary file indicator */   
  10. short token; /* Used for validity checking */   
  11. } FILE; /* This is the FILE object */ 

The C ++ Builder structure contains the basic attributes of file operations. Operations on files must be performed through the pointer of this structure. Common functions for such file operations are as follows:

 
 
  1. Fopen () Open stream
  2.  
  3. Fclose () Close the stream
  4.  
  5. Fputc () writes a character to the stream
  6.  
  7. Fgetc () reads a character from the stream
  8.  
  9. Fseek () locates the specified character in the stream
  10.  
  11. Fputs () writes a string to a stream
  12.  
  13. Fgets () reads a row or a specified character from the stream.
  14.  
  15. Fprintf () Outputs Data to the stream in the format
  16.  
  17. Fscanf () reads data from the stream in the format
  18.  
  19. Returns the true value when feof () reaches the end of a file.
  20.  
  21. Returns the value of ferror () When an error occurs.
  22.  
  23. Rewind () resets the file locator to the beginning of the file
  24.  
  25. Remove () delete an object
  26.  
  27. Fread () reads a specified number of characters from a stream
  28.  
  29. Fwrite () writes a specified number of characters to the stream.
  30.  
  31. Tmpfile () generates a temporary file stream
  32.  
  33. Tmpnam () generates a unique file name
  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.