There are many options to access files on the Windows platform. If you use pure C, you need to use File * and so on. Of course, you can also directly call the Windows API; if C ++ is used, the first thing that comes to mind is the file stream fstream. Although at the COM level, we can also use IStream to read and write files, which is highly efficient. However, this article only makes a simple discussion of C ++ stream operations, which is more universal than Windows API or IStream and C ++ stream operations, because you can easily port code to other platforms. Fstream has two Derived classes, ifstream and ofstream, which correspond to the input file stream and output file stream respectively. Before using them, you must include their header files in your cpp file. The method for creating a file stream is simple: ifstream fin; fin. open ("C: \ filename.txt"); in this example, an input file stream is created, and the corresponding file is filename.txt under the C-root directory. In fact, the open method also contains a parameter mode to specify its open mode. Ios: in open the file in Read mode ios: out open the file in write mode ios: ate access pointer at the end of the file ios: app when writing ios :: when trunc writes data, the old data is erased. ios: binary accesses the above Code in binary mode without specifying any open mode. The default parameter is used: input file stream, that is, ios: in, the output file stream is ios: out. It is generally explicitly specified when a special mode needs to be combined, for example, ios: in | ios: binary; // The file is read in binary mode, you can also specify the corresponding file path and name during the construction so that the creation process can be completed in one step. The above code can be rewritten as: ifstream fin ("C: \ filename.txt"); the opposite of the open method is the close method, which is opposite to open. Open is to associate the file stream object with the file in the peripherals, and close is to remove the association between the two. However, it should be noted that close also plays the role of clearing the cache. It is best to pair the open method with the close method. After creating and opening a file stream, you can use the stream insertion operator (<) and stream extraction operator (>) Just like operating standard I/O ). For the input file stream, you can call the getline function to read a whole line of data from the file stream, so that you can read strings containing spaces. The following is an example. The function of this example is to read a file in STLA format. STL is a common fast imaging file format, which is very simple, especially the ASCII version (STLA ). The Code is as follows: stdafx. h // stdafx. h: include file for standard system include files, // or project specific include files that are used frequently, but // are changed infrequently // # pragma once # include "targetver. h "# include <stdio. h> # include <tchar. h> // added # include <iostream> # include <sstream> # include <fstream> # include <string> # include <vector> using namespace std; // TODO: reference addition Al headers your program requires here readstla. cpp // readstla. cpp: Defines the entry point for the console application. // # include "stdafx. h "struct facet {float normal [3]; float vertex [3] [3] ;}; int _ tmain (int argc, _ TCHAR * argv []) {if (argc <2) {printf ("specify an input file! \ N "); return 1;} ifstream in (argv [1]); if (! In. is_open () {printf ("fail to open file! \ N "); return 1 ;}// var vector <facet> solid; string line; string word; // check format getline (in, line); if (line. find ("solid ")! = 0) {printf ("wrong file format! \ N "); in. close (); return 1;} while (getline (in, line) {if (line. find (" facet normal ")! = String: npos) {facet f; // read normal stringstream ns (line); ns> word; // eat "facet" ns> word; // eat "normal" ns> f. normal [0]> f. normal [1]> f. normal [2]; // read vertices getline (in, line); // "outer loop" for (int I = 0; I <3; I ++) {getline (in, line); stringstream vs (line); vs> word; // eat "vertex" vs> f. vertex [I] [0]> f. vertex [I] [1]> f. vertex [I] [2];} getline (in, line); // "endloop" getline (in, line); // "endfacet" solid. push_back (f) ;}} in. close (); // output int cnt = solid. size (); printf ("read % d facet \ n", cnt); for (int I = 0; I <cnt; I ++) {facet & f = solid [I]; printf ("\ nfacet % d: \ nnormal = (% f, % f, % f) \ n", \ I + 1, f. normal [0], f. normal [1], f. normal [2]); for (int j = 0; j <3; j ++) {printf ("vertex [% d] = (% f, % f, % f) \ n ", \ j + 1, f. vertex [j] [0], f. vertex [j] [1], f. vertex [j] [2]) ;}} return 0 ;} the test file is: cube_corner.stl solid cube_corner facet normal 0.0-1.0 0.0 outer loop vertex 0.0 0.0 0.0 vertex 1.0 0.0 0.0 vertex 0.0 0.0 endloop endfacet facet normal 1.0 0.0-0.0 outer loop vertex 1.0 0.0 0.0 vertex 0.0 0.0 1.0 0.0 vertex 1.0 0.0 0.0 endloop endfacet facet normal 0.0 0.0-1.0 outer loop vertex 0.0 0.0 0.0 vertex 0.0 0.0 1.0 vertex 0.0 1.0 endloop endfacet facet normal 0.0 0.577 0.577 outer loop vertex 0.577 1.0 0.0 vertex 0.0 1.0 0.0 vertex 0.0 0.0 1.0 endloop endfacet endsolid: read 4 facet 1: normal = (0.000000,-1.000000, 0.000000) vertex [1] = (0.000000, 0.000000, 0.000000) vertex [2] = (1.000000, 0.000000, 0.000000) vertex [3] = (0.000000, 0.000000, 1.000000) facet 2: normal = (0.000000, 0.000000,-1.000000) vertex [1] = (0.000000, 0.000000, 0.000000) vertex [2] = (0.000000, 1.000000, 0.000000) vertex [3] = (1.000000, 0.000000, 0.000000) facet 3: normal = (0.000000, 0.000000,-1.000000) vertex [1] = (0.000000, 0.000000, 0.000000) vertex [2] = (0.000000, 0.000000, 1.000000) vertex [3] = (0.000000, 1.000000, 0.000000) facet 4: normal = (0.577000, 0.577000, 0.577000) vertex [1] = (1.000000, 0.000000, 0.000000) vertex [2] = (0.000000, 1.000000, 0.000000) vertex [3] = (0.000000, 0.000000, 1.000000) Press any key to continue...