Not to mention anything. let's first talk about how C ++ creates two-dimensional arrays. This method is dynamic and can be filled in with any number.
[Cpp]
Int ** M = new int * [hang]; // initialize the row
For (int I = 0; I {
M [I] = new int [lie]; // initialize the column. The array is successfully initialized here.
For (int j = 0; j <lie; j ++) // assign a value to each of the two-dimensional arrays.
{
Cin> M [I] [j];
}
}
The above is such a process, of course, the value assignment of the initial value does not have to be input in command line mode. You can input files.
The following describes the input and output operations of a file.
Basic knowledge: The file operation stream fstream has two sub-classes: ifstream and ofstream. ifstream opens the file as input by default, while ofstream opens the file as output by default. However, you can add the mode when constructing the SDK.
1. First, ofstream outputs the content in the Program (memory) to the file (secondary storage.
[Cpp]
Ofstream outfile ("DATA.txt ");
If (! Outfile) return-1;
Outfile <"hello file! "; Www.2cto.com
Output to a file. The directory of the output file is the directory of your C source program. Of course, you can also use the following method to output:
[Cpp]
Ofstream outfile;
Outfile. open ("DATA.txt ");
If (! Outfile) return-1;
Outfile <"hello file! ";
If you want to output the file to an absolute address, for example, under the D directory, you should change the DATA.txt to D: \ DATA.txt. Remember that there are two slashes. The first one is used for escape. If you want to change the default output mode, you can use the following mode to change it.
Ios: in open an input file. Use this flag as the ifstream opening method to prevent the truncation of a ready-made file.
Ios: out open an output file. When it is used for an ofstream without ios: app, ios: ate, or ios: in, ios: trunc is implicitly
Ios: append an output file.
Ios: ate open a ready-made file (whether input or output) and find the end
Ios: nocreate only opens an existing file (otherwise, it fails)
Ios: noreplace: open only one nonexistent file (otherwise, the file fails)
Ios: trunc if a file exists, open it and delete the old file
Ios: binary open a binary file. The default value is text file.
The above is found in some libraries of C/C ++, and some are given below.
Ios: in = 0x01, // for reading. If the file does not exist, create it (ifstream is enabled by default)
Ios: out = 0x02, // for writing. If the file does not exist, it is created. If the file already exists, the original content is cleared (default open mode for ofstream)
Ios: ate = 0x04, // when the file is opened, the pointer is at the end of the file. The position of the pointer can be changed. It is often used together with in and out.
Ios: app = 0x08, // for writing. If the file does not exist, it is created. If the file already exists, it is written to the new content after the original file content, and the pointer position is always at the end.
Ios: trunc = 0x10, // cut the file length to 0 before reading and writing (default)
Ios: nocreate = 0x20, // an error occurs when the file does not exist. It is often used together with in or app.
Ios: noreplace = 0x40, // an error occurs when a file exists. It is often used in combination with out.
Ios: binary = 0x80 // binary format file
Whether constructed or open, you can add a mode later. Take the constructor as an example:
[Cpp]
Ofstream outfile ("DATA.txt", ios: out | ios: trunc );
Two modes are used. You need to know that these two modes are actually two 8-digit digits, which can be used to connect them.
After use, close the file. outfile. close.
2. Use ifstream to input the content in the file (secondary storage) into the program (memory.
The following is the content of a DATA.txt file:
[Plain]
Hello file!
Hi David!
Three rows are written. Then, output this item to the screen through the following program:
[Cpp]
Int main ()
{
String s;
Ifstream infile ("DATA.txt", ios: in );
If (! Infile) return-1;
While (infile)
{
// Istream & getline (istream & is, string & str );
Getline (infile, s); // Get line from stream
Cout <s <endl;
}
Infile. close ();
}
The output result is:
[Plain]
Hello file!
Hi David!
Press any key to continue...
Next, modify data.txt to solve the problem we mentioned at the beginning. You do not need to input data in command line mode or use text. The following is the content of data.txt:
[Plain]
12 33 44
22 463 93
63 48 9
Well, write the program below and upload the content in the file into the array. Then the screen is output.
[Cpp]
# Include <iostream>
# Include <fstream>
# Include <iomanip> // set the Bit Width because the setw () statement is used.
Using namespace std;
Int main ()
{
Int num = 3; // you can enter this num on the console. This function is not intended for the program.
Int ** M = new int * [num]; // initialize the row
Ifstream infile ("DATA.txt", ios: in); // initialize the file operation
If (! Infile) return-1; // an error is returned if creation fails.
For (int I = 0; I <num; I ++)
{
M [I] = new int [num]; // initialize the column. The array is successfully initialized here.
For (int j = 0; j <3; j ++) // assign a value to each of the two-dimensional arrays.
{
If (infile) infile> M [I] [j]; // read the value in infile. if there is a space, the cursor stops.
Cout <setw (5) <M [I] [j]; // set the bit width to 5 and output to the screen.
}
Cout <endl; // output a line with a new line
}
Infile. close (); // close the file
}
Output result:
[Plain]
12 33 44
22 463 93
63 48 9
Italian key continues...
The above is a formatted output ~~ The Bit Width must be 5. It looks much better. You can write this into the file again. Haha ~~