C++ language application project (array) – terrain height survey (simple version)

Table of Contents

1. Project structure:

2. File opening || closing

3. Reading files –> Adding defensive measures

4. Determine height –> Array and printing

5. All code presentation:


1. Project structure:

1. File opening || closing

2. File reading –> Add defensive measures

3. Determine height –> Array and printing

4. Example:

(Save the text file to the source directory)

6 7
5039 5127 5238 5259 5248 5310 5299
5150 5392 5410 5401 5320 5820 5321
5290 5560 5490 5421 9999 5831 5210
5110 5429 5430 5411 5459 5630 5319
4920 5129 4921 5821 4722 4921 5129
5023 5129 4822 4872 4794 4862 4245

2. File opening || closing

ifstream (file input stream)

i: input input

f: file file

stream: stream

Included library files: #include

Basic file operations:
a. ofstream //File writing operation
b. ifstream //File reading operation
c. fstream //File read and write operations

In this project, we need to obtain a C-type file and obtain the pointer of the stored file:

file.open( filename.c_str() );

-.c_str(): Convert to C type file.

file.open(): Open the file

(Note: file —> Definition: ifstream file: read the file!)

Habit: Files must be opened before checking

Purpose: To prevent subsequent code from crashing!

file.fail(): File reading failed!

if (file.fail()) {
cout << "File opening failed!" << endl;
exit(1);
}

(Note: exit(1); —> Normal end: 0 || Abnormal end: 1)

Code rendering:

#include<iostream>
#include<fstream> //File operation library file-----------------
#include<string>
#include<Windows.h>

using namespace std;


int main(){

string filename;
ifstream file;

cout << "Please enter the file name:" << endl;
cin >> filename;

file.open(filename.c_str()); // Get the C type file and get the pointer to store the file
// Must check whether the file is opened successfully!
if (file.fail()) {
cout << "File opening failed!" << endl;
exit(1); // Normal end: 0 || Abnormal end: 1
}
    
    return 0;
}

exit(0);
}

Start reading: read data from the data file –> two-dimensional array: just traverse it in a for loop

for (int i = 0; i < rows; i + + ) {
for (int j = 0; j < cols; j + + ) {
file >> map[i][j];
}
}

Code rendering:

#define MAX_ROW 16
#defineMAX_COL 16

int main(){
    .
    .
    .
    .

int rows, cols;
int map[MAX_ROW][MAX_COL] = {0};

file >> rows >> cols;

if (rows > MAX_ROW || cols > MAX_COL) {
cout << "Data input error, too large!" << endl;
exit(0);
}
 
for (int i = 0; i < rows; i + + ) {
for (int j = 0; j < cols; j + + ) {
file >> map[i][j];
}
}
    .
    .
    .
    return 0;

}

5. All code and example implementation

Communicate promptly if you have any questions and grow together! (^_^)

6 7
5039 5127 5238 5259 5248 5310 5299
5150 5392 5410 5401 5320 5820 5321
5290 5560 5490 5421 9999 5831 5210
5110 5429 5430 5411 5459 5630 5319
4920 5129 4921 5821 4722 4921 5129
5023 5129 4822 4872 4794 4862 4245

#include<iostream>
#include<fstream> //File operation library file
#include<string>
#include<Windows.h>


using namespace std;

#defineMAX_ROW 16
#defineMAX_COL 16

bool IsMax(int arg[16][16],int row,int col) {
if (arg[row][col] > arg[row - 1][col]
& amp; & amp; arg[row][col] > arg[row + 1][col]
& amp; & amp; arg[row][col] > arg[row][col - 1]
& amp; & amp; arg[row][col] > arg[row][col + 1]
) {
return true;
}
return false;
}


int main() {

int rows, cols;
int map[MAX_ROW][MAX_COL] = {0};

string filename;
ifstream file;

cout << "Please enter the file name:" << endl;
cin >> filename;

file.open(filename.c_str()); // Get the C type file and get the pointer to store the file
// Must check whether the file is opened successfully!
if (file.fail()) {
cout << "File opening failed!" << endl;
exit(1); // Normal end: 0 || Abnormal end: 1
}

// Compare cin >> ... The method is similar!
file >> rows >> cols;

// Defensive processing: check whether the input is reasonable!
if (rows > MAX_ROW || cols > MAX_COL) {
cout << "Data input error, too large!" << endl;
exit(0);
}

// Start reading: read data from data file --> two-dimensional array
for (int i = 0; i < rows; i + + ) {
for (int j = 0; j < cols; j + + ) {
file >> map[i][j];
}
}

//Judge, print peak value --> 1.Judge, 2.Print
// bool IsMax();

for (int i = 1; i < rows-1; i + + ) {
// rows(cols)-1: remove borders
for (int j = 1; j < cols-1; j + + ) {
if (IsMax(map, i, j)) {
//cout << "The peak point is: ("<<i<<","<<j<<") " << map[i][j] << endl;
printf("Peak point at: (%d,%d)%d\
", i, j, map[i][j]);
}

}
}
system("pause");

// File is closed! Open the corresponding file! ! ! --> Avoid resource leakage!
file.close();

return 0;
}

(The harder you work, the luckier you get!)