设计Rinex O文件和Rinex N文件读取类,实现对Rinex文件内容的读取和显示.c++,.h.cppmain
时间: 2023-06-20 09:05:53 浏览: 155
基于C++实现读取文件【100012594】
下面是一个简单的实现,包含Rinex文件读取类RinexFile和读取示例:
RinexFile.h
```cpp
#ifndef RINEXFILE_H
#define RINEXFILE_H
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
class RinexFile {
public:
RinexFile();
~RinexFile();
bool readFile(const std::string& fileName);
void displayHeader() const;
void displayObservations() const;
private:
std::string version_;
std::string fileType_;
std::string programName_;
std::string date_;
std::vector<std::string> comments_;
std::vector<std::string> observationTypes_;
std::vector<std::vector<double>> observations_;
};
#endif // RINEXFILE_H
```
RinexFile.cpp
```cpp
#include "RinexFile.h"
RinexFile::RinexFile() {
}
RinexFile::~RinexFile() {
}
bool RinexFile::readFile(const std::string& fileName) {
std::ifstream file(fileName);
if (!file) {
std::cerr << "Error opening file " << fileName << std::endl;
return false;
}
std::string line;
while (std::getline(file, line)) {
if (line.substr(60, 20) == "RINEX VERSION / TYPE") {
version_ = line.substr(0, 9);
fileType_ = line.substr(20, 1);
programName_ = line.substr(40, 20);
} else if (line.substr(60, 20) == "PGM / RUN BY / DATE") {
programName_ = line.substr(0, 20);
std::string runBy = line.substr(20, 20);
date_ = line.substr(40, 20);
} else if (line.substr(60, 20) == "COMMENT") {
comments_.push_back(line.substr(0, 60));
} else if (line.substr(60, 20) == "END OF HEADER") {
break;
}
}
// Read observations
while (std::getline(file, line)) {
if (line.substr(60, 20) == "END OF FILE") {
break;
}
std::vector<double> obs;
for (int i = 0; i < 5; i++) {
std::string val = line.substr(i * 16, 14);
if (!val.empty()) {
obs.push_back(std::stod(val));
}
}
observations_.push_back(obs);
}
file.close();
return true;
}
void RinexFile::displayHeader() const {
std::cout << "RINEX VERSION / TYPE: " << version_ << " " << fileType_ << std::endl;
std::cout << "PGM / RUN BY / DATE : " << programName_ << " " << date_ << std::endl;
for (const auto& comment : comments_) {
std::cout << "COMMENT : " << comment << std::endl;
}
}
void RinexFile::displayObservations() const {
for (const auto& obs : observations_) {
for (const auto& val : obs) {
std::cout << val << " ";
}
std::cout << std::endl;
}
}
```
main.cpp
```cpp
#include "RinexFile.h"
int main() {
RinexFile file;
if (file.readFile("test.rnx")) {
file.displayHeader();
file.displayObservations();
}
return 0;
}
```
阅读全文