c++设计Rinex O文件和Rinex N文件两个类,并按行读取文件和显示出来,.h和.cpp
时间: 2024-05-16 07:14:22 浏览: 9
以下是RinexO文件类的头文件RinexO.h:
```c++
#ifndef RINEXO_H
#define RINEXO_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class RinexO {
public:
RinexO() {}
RinexO(std::string file_name);
void read_file(std::string file_name);
void display();
private:
std::vector<std::string> header;
std::vector<std::string> observations;
};
#endif // RINEXO_H
```
以下是RinexO文件类的实现文件RinexO.cpp:
```c++
#include "RinexO.h"
RinexO::RinexO(std::string file_name) {
read_file(file_name);
}
void RinexO::read_file(std::string file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
std::cerr << "Failed to open file" << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
if (line.empty()) {
continue;
}
if (line[0] == '>') {
header.push_back(line);
} else {
observations.push_back(line);
}
}
file.close();
}
void RinexO::display() {
for (const auto& h : header) {
std::cout << h << std::endl;
}
for (const auto& o : observations) {
std::cout << o << std::endl;
}
}
```
以下是RinexN文件类的头文件RinexN.h:
```c++
#ifndef RINEXN_H
#define RINEXN_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class RinexN {
public:
RinexN() {}
RinexN(std::string file_name);
void read_file(std::string file_name);
void display();
private:
std::vector<std::string> header;
std::vector<std::string> observations;
};
#endif // RINEXN_H
```
以下是RinexN文件类的实现文件RinexN.cpp:
```c++
#include "RinexN.h"
RinexN::RinexN(std::string file_name) {
read_file(file_name);
}
void RinexN::read_file(std::string file_name) {
std::ifstream file(file_name);
if (!file.is_open()) {
std::cerr << "Failed to open file" << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
if (line.empty()) {
continue;
}
if (line[0] == '>') {
header.push_back(line);
} else {
observations.push_back(line);
}
}
file.close();
}
void RinexN::display() {
for (const auto& h : header) {
std::cout << h << std::endl;
}
for (const auto& o : observations) {
std::cout << o << std::endl;
}
}
```
可以看出RinexO和RinexN类非常相似,只是文件类型不同。它们的read_file和display方法也非常相似。